【问题标题】:How to fix FileNotFoundError: [WinError 2] The system cannot find the file specified with AudioSegment.from_mp3()如何修复 FileNotFoundError: [WinError 2] 系统找不到 AudioSegment.from_mp3() 指定的文件
【发布时间】:2019-04-13 19:38:25
【问题描述】:

我一直试图在视频的音频中找到音频静音空间的位置,但我无法在 python 3 中使用 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

按预期返回

音频.mp3

并且代码在我尝试导入音频时停止运行

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

【问题讨论】:

  • 您是否执行过print(AudioSegment.ffmpeg)print(my_file) 检查,如果是,它们是否符合您的预期?
  • 另外请发布print(os.getcwd()+"\\ffmpeg\\bin\\ffmpeg.exe") 的输出。是否符合您的预期。
  • 脚本抛出:NameError: name 'vid' is not defined。 Idf 放了一个名为 audio,mp3 的随机 mp3 文件,我能够重现我将发布的错误作为答案,但显然不是答案。这我们可以验证出了什么问题。
  • 我已经添加了必要的编辑 :))

标签: python-3.x pydub


【解决方案1】:

出于比较原因,答案的第一部分试图重现 OPs 错误。之后通过更新 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) 音频.mp3

建议的解决方案

包含以下特定代码行以指定 ffmpeg 所在的位置:

pydub.AudioSegment.converter = r"C:\\path\\to\\ffmpeg.exe" 其中 path\to\ 是实际路径

你应该没事的。

更新 1

您出现错误是由于使用了“my_file”中的文件名,而不是AudioSegment.from_mp3(my_file) 要求的“filepath”。通过提供文件路径,可以修复引发的 [WinError2] 问题。

当您在下面运行脚本时,会发生AttributeError: 'WindowsPath' object has no attribute 'read' 错误。该错误与pathlib 相关,应该已在 pydub 0.22 版本中修复,如 github 上的here 所述。我在 Github 上提出了issue

提出的file.read() 问题与 python 版本相关(2.7 与 3.5),因为它不再在其内置库中。因此.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 同时解决错误一和错误二的问题。

在导入语句之后直接在脚本中包含以下几行:


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\ 出现在“PATH”下,并且错误一和二中的所有警告都消失了。

【讨论】:

  • 感谢大家的澄清。我尝试了您提出的解决方案,但恐怕它不起作用
【解决方案2】:

我遇到了同样的问题,但显然即使在添加 ffmpeg 路径之后它仍然会给出同样的错误。我在Linux 没有 额外的命令(如AudioSegment.converter = 'path\to\ffmpeg')中尝试了这个工作正常,问题出在 Windows IDE(pycharm、spyder 等)上。尝试直接从 Windows 中的提示符(anaconda、cmd 等)运行脚本。它应该可以工作。

参考:https://github.com/jiaaro/pydub/issues/319

【讨论】:

    【解决方案3】:

    我遇到了同样的问题。

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

    一切正常

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      • 1970-01-01
      相关资源
      最近更新 更多