我遇到一个问题,即我的项目无法找到 ffmpeg,即使它已安装在 Python 库中。详情如下:
When running the script, I get the following error message:
D:\Python\Lib\site-packages\pydub\utils.py:170: 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)
Additionally, I receive this error message when trying to prepare the dataset:
from pydub import AudioSegment
import os
# Specify the path to ffmpeg
ffmpeg_path = "D:\\Python-code\\ffmpeg-master-latest-win64-gpl\\bin\\ffmpeg.exe"
AudioSegment.ffmpeg = ffmpeg_path
def convert_flac_to_wav(flac_file, wav_file):
sound = AudioSegment.from_file(flac_file, format="flac")
sound.export(wav_file, format="wav")
if __name__ == "__main__":
# Get the current working directory
current_dir = os.getcwd()
print(f"Current directory: {current_dir}")
# Get the parent directory of the current working directory
root_dir = os.path.dirname(current_dir)
print(f"Root directory: {root_dir}")
# Construct the path
voice_wavs = os.path.join(root_dir, 'data', 'voice', 'wav')
print(f"Voice WAVs directory: {voice_wavs}")
# Ensure the directory exists
if not os.path.exists(voice_wavs):
os.makedirs(voice_wavs)
print(f"Created directory: {voice_wavs}")
else:
print(f"Directory exists: {voice_wavs}")
# Get all .flac files
# Ensure .flac files are located in the current working directory
flac_files = [f for f in os.listdir(current_dir) if f.endswith('.flac')]
print(f"FLAC files found: {flac_files}")
# Convert each .flac file
for flac_file in flac_files:
input_flac = os.path.join(current_dir, flac_file)
output_wav = os.path.join(current_dir, flac_file.replace('.flac', '.wav'))
convert_flac_to_wav(input_flac, output_wav)
尽管执行了这些步骤,项目仍然报告找不到 ffmpeg。有人可以帮助我确定可能导致此问题的原因以及如何解决它吗?
AudioSegment
似乎在所有不好的方面都有问题。如果您只想使用 ffmpeg 将 flac 文件转换为 wav,我建议使用内置的 subprocess
模块来调用 ffmpeg.exe
。修改后的代码应如下所示:
import os
import subprocess
# Specify the path to ffmpeg
ffmpeg_path = r"D:\Python-code\ffmpeg-master-latest-win64-gpl\bin\ffmpeg.exe"
def convert_flac_to_wav(flac_file, wav_file):
subprocess.call([ffmpeg_path, '-i', flac_file, wav_file])
if __name__ == "__main__":
# Unchanged