You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python项目已安装ffmpeg仍无法找到的问题排查求助

问题分析与解决:Python项目无法找到FFmpeg(已指定路径仍报错)

报错信息

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)

用户已尝试在代码中手动指定FFmpeg路径,但依然触发上述警告,代码如下:

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)

可能的原因

  • 路径配置时机太晚,pydub在导入时已完成FFmpeg检测,后续赋值不生效
  • 仅指定了FFmpeg路径,但pydub同时依赖ffprobe,未配置该工具路径
  • 路径格式存在转义字符处理不当的问题
  • FFmpeg文件本身不可用(损坏、权限不足、版本与系统不兼容)

解决方法

1. 提前配置路径并补充ffprobe设置

把路径配置放在导入AudioSegment之前,同时指定ffprobe的路径,确保pydub初始化时就能找到工具:

import os
# 先配置路径,再导入pydub
ffmpeg_dir = r"D:\Python-code\ffmpeg-master-latest-win64-gpl\bin"
ffmpeg_path = os.path.join(ffmpeg_dir, "ffmpeg.exe")
ffprobe_path = os.path.join(ffmpeg_dir, "ffprobe.exe")

# 将FFmpeg目录加入临时环境变量,避免其他依赖找不到
os.environ["PATH"] += os.pathsep + ffmpeg_dir

from pydub import AudioSegment
AudioSegment.ffmpeg = ffmpeg_path
AudioSegment.ffprobe = ffprobe_path

# 后续转换函数和主逻辑不变
def convert_flac_to_wav(flac_file, wav_file):
    sound = AudioSegment.from_file(flac_file, format="flac")
    sound.export(wav_file, format="wav")

# ... 其余代码 ...

2. 验证FFmpeg路径有效性

打开命令提示符,执行以下命令:

"D:\Python-code\ffmpeg-master-latest-win64-gpl\bin\ffmpeg.exe" -version

如果能正常输出FFmpeg版本信息,说明文件没问题;如果报错,重新下载对应系统版本的FFmpeg(确保是64位版本匹配你的Windows系统),并检查文件是否完整。

3. 加入系统环境变量(永久解决)

把FFmpeg的bin目录添加到系统Path变量:

  1. 右键「此电脑」→ 属性 → 高级系统设置 → 环境变量
  2. 在系统变量的Path中点击「新建」,添加D:\Python-code\ffmpeg-master-latest-win64-gpl\bin
  3. 重启你的IDE或终端,让环境变量生效,之后代码中无需手动指定路径

4. 更新pydub版本

执行命令更新pydub,避免旧版本的路径检测bug:

pip install --upgrade pydub

内容的提问来源于stack exchange,提问作者Jieyu Zhang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 05:42:38