python-3.x 如何修复FileNotFoundError:[WinError 2]系统找不到使用AudioSegment.from_mp3()指定的文件

rjee0c15  于 2023-06-25  发布在  Python
关注(0)|答案(4)|浏览(526)

我一直在尝试在视频的音频中找到音频静音的位置,但是我无法通过在python3中使用pydub导入音频文件
我已经尝试将pydub检查ffmpeg的目录更改为项目中的一个目录,该文件位于我运行脚本的目录中,但它似乎仍然返回相同的错误。

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)

我希望它将mp3音频片段存储到变量audi中,但它返回:

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)

按预期返回
C:\Users\ratee\PycharmProjects\untitled\ffmpeg\bin\ffmpeg.exe

print(my_file) returns

按预期返回
audio.mp3
代码在我尝试导入音频时停止运行
audio = AudioSegment.from_mp3(“audio.mp3”)

gijlo24d

gijlo24d1#

答案的第一部分试图重现OP的错误比较的原因。此后,通过更新1和最后更新2找到解决方案。文件夹名称x、y用于缩短路径长度。
复制它给我带来了以下错误:
错误一:

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)

错误二:

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

以下代码用于检查是否可以找到ffmpeg和ffprobe:

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

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

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

给出:
2)audio.mp3

建议方案

包括以下特定的代码线以指定ffmpeg位于何处:
pydub.AudioSegment.converter = r"C:\\path\\to\\ffmpeg.exe"其中**path\to**是实际路径
你应该会没事的。

    • 更新1**:

你提出的错误是由于使用的文件名在“我的文件”,而不是“filepath”所要求的AudioSegment.from_mp3(my_file)。通过提供文件路径,这修复了出现的***[WinError2]***问题。
当您运行下面的脚本时,会发生AttributeError: 'WindowsPath' object has no attribute 'read'错误。这个错误与pathlib有关,应该在pydub 0.22版本中得到修复,正如在github上讨论的here。我在Github上提出了issue
引发的file.read()问题与python版本有关(2.7与3.5),因为它nolonger在其内置库.因此,.read()触发AttributeError: 'WindowsPath' object has no attribute 'read'错误。

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.
    • 更新2**:

由于更新1解决了平台版本问题,如果更新1中的解决方案1尚未解决问题,则更新2同时解决错误1和错误2中的问题。
直接在脚本中的import语句后包含以下行:

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

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

打印输出显示您是否包含FFmpeg文件的位置。如果没有,你需要重新启动windows,因为当你当前在python环境/编辑器中时,windows环境路径没有更新。
在我的情况下,重新启动后,c:\y\FFmpeg\显示在“路径”下,错误1和2中的所有警告都消失了。

fnx2tebb

fnx2tebb2#

我遇到了同样的问题。

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

一切都很好

f2uvfpb9

f2uvfpb93#

我遇到了同样的问题,但显然即使在添加ffmpeg路径后,它仍然给出了同样的错误。我在Linux中尝试了这个,没有额外的命令,如AudioSegment.converter = 'path\to\ffmpeg',它工作正常,问题是Windows IDE(pycharm,spyder等)。尝试在windows中直接从提示符(anaconda,cmd等)运行脚本。应该能用
参考:https://github.com/jiaaro/pydub/issues/319

vqlkdk9b

vqlkdk9b4#

帮助我的是在Windows中使用WSL而不是bash,并使用WSL中创建的venv运行此应用程序。还需要安装ffmpeg

相关问题