脚本从 PyCharm 开始时出现 FileNotFoundError

FileNotFoundError when Script is started from PyCharm

我正在使用 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 中的启动选项

谢谢

布莱克

编辑:

使用 mp3 文件的绝对路径也不起作用(即使当我用上面两行 open(...) 测试它时它起作用了。

打印 AudioSegment.converter 在 PyCharm 和 PowerShell 中也是一样的。

我现在通过自己实施解决了我的问题。不知道出了什么问题。 使用代码需要您自担风险,因为我是 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文件夹需要和函数所在的脚本放在同一个目录下。