从PyCharm启动脚本时出现FileNotFoundError

时间:2019-07-13 17:41:18

标签: python file path pycharm pydub

我正在使用pydub将mp3文件转换为wav。我从Atom切换到PyCharm,现在出现以下错误。

C:\Users\BlakkM9\AppData\Local\Programs\Python\Python37\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 "speechrec.py", line 102, in <module>
    main()
  File "speechrec.py", line 36, in main
    recognize()
  File "speechrec.py", line 51, in recognize
    current = AudioSegment.from_mp3("./rec/ready.mp3")
  File "C:\Users\BlakkM9\AppData\Local\Programs\Python\Python37\lib\site-packages\pydub\audio_segment.py", line 716, in from_mp3
    return cls.from_file(file, 'mp3', parameters=parameters)
  File "C:\Users\BlakkM9\AppData\Local\Programs\Python\Python37\lib\site-packages\pydub\audio_segment.py", line 665, in from_file
    info = mediainfo_json(orig_file)
  File "C:\Users\BlakkM9\AppData\Local\Programs\Python\Python37\lib\site-packages\pydub\utils.py", line 263, in mediainfo_json
    res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
  File "C:\Users\BlakkM9\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Users\BlakkM9\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden

切换之前,我只是在PowerShell的项目根目录中键入python start.py。这仍然可以正常工作。

相应的代码:

from pydub import AudioSegment

AudioSegment.converter = os.path.dirname(os.path.abspath(__file__)) + "\\ffmpeg\\bin\\ffmpeg"

current = AudioSegment.from_mp3("./rec/ready.mp3")
current.export("./rec/current.wav", format="wav")

PyCharm中的启动选项 enter image description here

谢谢

布拉克

编辑:

使用mp3文件的绝对路径也无法正常工作(即使当我使用open(...)以上两行对其进行测试时也可以使用。

打印AudioSegment.converter在PyCharm和PowerShell中也相同。

1 个答案:

答案 0 :(得分:0)

我现在仅通过自己实施即可解决问题。不知道是什么问题。 使用代码需要您自担风险,因为我是python的新手。

import os
import subprocess

curr_path = os.path.abspath(".")

def ffmpeg(input_file, output_file):
    # convert to absolute paths if necessary
    if os.path.isabs(input_file):
        input_path = input_file
    else:
        input_path = curr_path + input_file

    if os.path.isabs(output_file):
        output_path = output_file
    else:
        output_path = curr_path + output_file

    subprocess.check_call(curr_path + "/ffmpeg/bin/ffmpeg.exe -i " +
                          input_path + " " +
                          output_path,
                          stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)

禁用ffmpeg输出,如果不更改,则ffmpeg文件夹必须与函数所在的脚本放在同一目录中。