How to fix FileNotFoundError: [WinError 2] The system cannot find the file specified with AudioSegment.from_mp3()

jah picture jah · Apr 13, 2019 · Viewed 9.3k times · Source

I've been trying to find the position of spaces of audio silence in the audio of a video, but I can't get past just importing an audio file with pydub in python 3

I've already tried changing the directory that pydub is checking for ffmpeg to one within the project, and the file is in the directory I'm running the script from but it still seems to return the same error.

from moviepy import editor
from pydub import silence, AudioSegment
from pathlib import Path
import os
AudioSegment.converter = r"C:\\Users\\ratee\\PycharmProjects\\untitled\\ffmpeg\\bin\\ffmpeg.exe"
vid = editor.VideoFileClip("video.mp4")
print(AudioSegment.ffmpeg)
my_file = Path("audio.mp3")
if not my_file.is_file():
    vid.audio.write_audiofile("audio.mp3")
audio = AudioSegment.from_mp3("audio.mp3")
print(audio)

I expect it to store the mp3 audio segment into the variable audi but it returns:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1741, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1735, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1135, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/ratee/PycharmProjects/untitled/fuc.py", line 12, in <module>
    song = AudioSegment.from_mp3("audio.mp3")
  File "C:\Users\ratee\PycharmProjects\untitled\venv\lib\site-packages\pydub\audio_segment.py", line 716, in from_mp3
    return cls.from_file(file, 'mp3', parameters=parameters)
  File "C:\Users\ratee\PycharmProjects\untitled\venv\lib\site-packages\pydub\audio_segment.py", line 665, in from_file
    info = mediainfo_json(orig_file)
  File "C:\Users\ratee\PycharmProjects\untitled\venv\lib\site-packages\pydub\utils.py", line 263, in mediainfo_json
    res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
  File "C:\Users\ratee\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 756, in __init__
    restore_signals, start_new_session)
  File "C:\Users\ratee\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1155, in _execute_child
    startupinfo)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\_pydev_bundle\pydev_monkey.py", line 452, in new_CreateProcess
    return getattr(_subprocess, original_name)(app_name, patch_arg_str_win(cmd_line), *args)
FileNotFoundError: [WinError 2] The system cannot find the file specified
print(AudioSegment.ffmpeg)

returns as expected

C:\Users\ratee\PycharmProjects\untitled\ffmpeg\bin\ffmpeg.exe

print(my_file) returns

returns as expected

audio.mp3

and the code stops running at the point where I try to import audio

audio = AudioSegment.from_mp3("audio.mp3")

Answer

ZF007 picture ZF007 · Apr 13, 2019

First part of the answer tries to reproduce OPs error for comparison reason. Thereafter finding a solution via update 1 and finally update 2. The folder-names x, y are used to shorten pathlengths.

Reproducing it threw me the following errors:

Error One:

c:\x\lib\site-packages\pydub\utils.py:165: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
      warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
c:\x\lib\site-packages\pydub\utils.py:193: RuntimeWarning: Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work
      warn("Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work", RuntimeWarning)

Error Two:

    Traceback (most recent call last):
      File "C:\y\lol.py", line 16, in <module>
        audi = AudioSegment.from_mp3("audio.mp3")
      File "c:\x\lib\site-packages\pydub\audio_segment.py", line 716, in from_mp3
        return cls.from_file(file, 'mp3', parameters=parameters)
      File "c:\x\lib\site-packages\pydub\audio_segment.py", line 665, in from_file
        info = mediainfo_json(orig_file)
      File "c:\x\lib\site-packages\pydub\utils.py", line 263, in mediainfo_json
        res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
      File "c:\x\lib\subprocess.py", line 676, in __init__
        restore_signals, start_new_session)
      File "c:\x\lib\subprocess.py", line 957, in _execute_child
        startupinfo)
    FileNotFoundError: [WinError 2] The system cannot find the file specified

The following code is to check if ffmpeg and ffprobe can be found:

AudioSegment.ffmpeg = os.getcwd()+"\\ffmpeg\\bin\\ffmpeg.exe"
print (AudioSegment.ffmpeg)

1) C:\y\ffmpeg\bin\ffmpeg.exe

And:

my_file = Path("audio.mp3")
print (my_file) 

gives:

2) audio.mp3

Proposed solution:

Include the following specific codeline to specify where the ffmpeg is located:

pydub.AudioSegment.converter = r"C:\\path\\to\\ffmpeg.exe" where path\to\ is the actual path

And you should be fine.

Update 1:

You're raised error is due to the use of the filename at "my_file" and not "filepath" as required by AudioSegment.from_mp3(my_file). By providing the filepath this fixes the raised [WinError2] issue.

When you run below script the AttributeError: 'WindowsPath' object has no attribute 'read' error occurs. The error is related to pathlib and should have be been fixed in pydub 0.22 version as discussed here at github. I've raised the issue at Github.

The file.read() issue raised is python version related (2.7 vs. 3.5) because its nolonger in its build-in library. Therefore the .read() triggers the AttributeError: 'WindowsPath' object has no attribute 'read' error.

from pydub import silence, AudioSegment
from pathlib import Path

import os, sys

print (sys.version)

#AudioSegment.ffmpeg = os.getcwd()+"\\ffmpeg\\bin\\ffmpeg.exe"

AudioSegment.converter = r"C:\\x\\build\\win\\64\\ffmpeg.exe"
AudioSegment.ffprobe   = r"C:\\x\\build\\win\\64\\ffprobe.exe"

#print (AudioSegment.converter)
#print (AudioSegment.ffprobe)

my_file = Path("C:\\y\\audio.mp3")

print ('ID1 : %s' % my_file) 

audio = AudioSegment.from_mp3(my_file)    # solves ***[WinError2]*** issue.

Update 2:

As update 1 solves a platform version issue update 2 solves the issue in error one and two at the same time if solution 1 in update 1 doesn't solve it yet.

Include in your script directly after the import statements the following lines:


mypaths = os.getenv('PATH').split(';')  # replace 'PATH' by 'your search path' if needed.

 for i in mypaths:
     if i.find('python'):
         print(i)

The printout shows you whether you have included the location of FFmpeg files or not. If not you need to reboot windows due to the fact the windows environment paths are not updated while you are currently in a python environment/editor.

In my case after reboot c:\y\FFmpeg\ showed up under 'PATH' and all warnings in error one and two were gone.