【问题标题】:Can't find the specified file找不到指定的文件
【发布时间】:2017-08-13 16:07:15
【问题描述】:

我正在尝试使用库 pydub 将文件夹中的每个文件从 mp4 转换为 mp3,但是当我这样做时返回错误 FileNotFoundError: [WinError 2] The system cannot find the file specified。我不确定如何更具体地了解该文件。

这是我的代码:

from pydub import AudioSegment
import os
import os.path

folder = 'C:/Users/Magnus/Desktop/test/'

videos = []

for file in [f for f in os.listdir(folder) \
            if os.path.isfile(os.path.join(folder, f))]:
    AudioSegment.from_file(folder + file).export(folder + file, format="mp3")

C:/Users/Magnus/Desktop/test/ 内部,我有类似M83_-_Bibi_The_Dog_(Audio).mp4 的文件

完整的追溯:

Traceback (most recent call last):
  File "C:\Users\Magnus\Desktop\youtube-playlist-downloader.py", line 64, in <module>
    AudioSegment.from_file(os.path.join(folder, file)).export(os.path.join(folder, file), format="mp3")
  File "C:\Users\Magnus\AppData\Local\Programs\Python\Python35\lib\site-packages\pydub\audio_segment.py", line 505, in from_file
    p = subprocess.Popen(conversion_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "C:\Users\Magnus\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "C:\Users\Magnus\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 1224, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

os.path.join(folder, file) 替换folder + file 也不起作用

【问题讨论】:

  • 上一行正确加入时,为什么还要连接?
  • 您能粘贴完整的堆栈跟踪吗?
  • 您似乎没有正确安装 ffmpeg。 FileNotFound 错误不是针对您要转换的音频文件,而是针对 pydub 试图通过 subprocess.Popen调用的外部工具。
  • 做一件事,把最后一条语句分成两部分,找出错误的根源!像 z= AudioSegment.from_file(folder + file) 然后在新行中 z.export(folder + file, format="mp3") 然后找出究竟是哪一行导致了这个错误?
  • 我不知道,您应该尝试自己调试(或分析)pydub/audio_segment.py 并尝试寻找运气,或者尝试在没有此操作系统的情况下单独运行此音频片段命令.file junk 试试该命令是否单独工作?

标签: python-3.x operating-system


【解决方案1】:

我遇到了类似的错误。我使用的是 anaconda。

这个错误看起来不像是文件错误,而是由于:

  • ffmpeg 未安装。 (或)

  • ffmpeg 未添加到环境变量中。(与 FFmpeg 一起,甚至您的 python.exe 和 conda.exe 也应该在 env 变量中)

执行以下步骤:

在 Windows 上:

  • 从 Windows 二进制文件中下载并提取 libav/ffmpeg。
  • 将 libav /bin 文件夹添加到您的 PATH 环境变量中。
  • pip install pydub

您可以参考:https://github.com/jiaaro/pydub

【讨论】:

猜你喜欢
  • 2016-03-17
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-28
  • 2020-02-20
  • 2021-04-19
相关资源
最近更新 更多