Skip to content Skip to sidebar Skip to footer

How To Fix Filenotfounderror: [winerror 2] The System Cannot Find The File Specified With Audiosegment.from_mp3()

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 trie

Solution 1:

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.

Solution 2:

I encountered the same problem but apparently even after adding ffmpeg path it still gives the same error. I tried this in Linuxwithout extra commands like AudioSegment.converter = 'path\to\ffmpeg' it works fine, the problem is with Windows IDEs (pycharm, spyder, etc). Try running script directly from your prompt(anaconda, cmd, etc.) in windows. It should work.

Refrence: https://github.com/jiaaro/pydub/issues/319

Solution 3:

I met with the same problem.

pydub.AudioSegment.converter = os.getcwd()+ "\\ffmpeg.exe"pydub.AudioSegment.ffprobe   = os.getcwd()+ "\\ffprobe.exe"sound = pydub.AudioSegment.from_mp3(os.getcwd()+"\\sample.mp3")

And everything is OK

Post a Comment for "How To Fix Filenotfounderror: [winerror 2] The System Cannot Find The File Specified With Audiosegment.from_mp3()"