Windows下Conda环境Python脚本无法识别FFmpeg的问题求助
解决Windows Conda环境中Python脚本找不到ffmpeg的问题
你的FileNotFoundError并非真的找不到ffmpeg,而是Windows系统没有Linux/macOS下的which命令,脚本里的which_ffmpeg()函数调用了subprocess.run(['which', 'ffmpeg']),导致subprocess无法找到which.exe报错。以下是可行解决办法:
方案1:修改which_ffmpeg()为跨平台实现
直接修改D:\Work\DIff-Foley\Diff-Foley\inference\demo_util.py中的which_ffmpeg()函数,用Python内置的shutil.which(跨平台,推荐)替代which命令:
import shutil def which_ffmpeg() -> str: '''Determines the path to ffmpeg library Returns: str -- path to the library ''' ffmpeg_path = shutil.which('ffmpeg') return ffmpeg_path if ffmpeg_path else ''
或者用Windows自带的where命令:
import subprocess def which_ffmpeg() -> str: '''Determines the path to ffmpeg library Returns: str -- path to the library ''' try: # Windows下需要shell=True才能正确执行where命令 result = subprocess.run(['where', 'ffmpeg'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) # 取第一个匹配的路径 ffmpeg_path = result.stdout.decode('utf-8').split('\n')[0].strip() return ffmpeg_path if ffmpeg_path else '' except Exception: return ''
方案2:直接指定ffmpeg绝对路径
如果你明确知道conda环境中ffmpeg的路径(比如D:\Software\Anaconda\envs\diff_foley\Library\bin\ffmpeg.exe),可以直接硬编码返回该路径,跳过检测逻辑:
def which_ffmpeg() -> str: # 替换为你实际的ffmpeg.exe路径 return r'D:\Software\Anaconda\envs\diff_foley\Library\bin\ffmpeg.exe'
方案3:确认脚本运行环境
确保运行脚本的终端已激活目标conda环境:
conda activate diff_foley
避免脚本使用系统默认Python解释器,导致无法读取conda环境内的ffmpeg路径。
错误栈关键定位:
File D:\Work\DIff-Foley\Diff-Foley\inference\demo_util.py:26, in which_ffmpeg() ---> 26 result = subprocess.run(['which', 'ffmpeg'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) FileNotFoundError: [WinError 2] The system cannot find the file specified这里的“找不到文件”指的是
which命令,而非ffmpeg本身。
内容的提问来源于stack exchange,提问作者shiftyscales
相关产品推荐
相关产品推荐

