chore(util): 优化项目启动时检测ffmpeg环境时在某些场景下的提示

此次变动主要针对 Linux (Ubuntu 22.04) 环境下根据官方提供的README文档以及conda安装 ffmpeg 环境后出现缺失动态库libiconv.so.2的问题 报错完整信息如下:
`ffmpeg: error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory` 基于以上报错信息 优化了util.py中的check_ffmpeg_installed函数
- 保留了原有命令行的提示信息 [定位问题更加友好]
- 抓取报错中的 libiconv.so.2 文本内容 进行提示用户通过conda安装 libiconv 动态库
This commit is contained in:
Hanabi
2025-10-20 12:28:22 +08:00
committed by GitHub
parent 1736e10189
commit d081803434
+51 -18
View File
@@ -176,31 +176,64 @@ def parse_string_to_list(value, separator=";"):
return []
def check_ffmpeg_installed():
ffmpeg_installed = False
def check_ffmpeg_installed() -> bool:
"""
检查当前环境中是否已正确安装并可执行 ffmpeg。
Returns:
bool: 如果 ffmpeg 正常可用,返回 True;否则抛出 ValueError 异常。
Raises:
ValueError: 当检测到 ffmpeg 未安装或依赖缺失时,抛出详细的提示信息。
"""
try:
# 执行ffmpeg -version命令,并捕获输出
# 尝试执行 ffmpeg 命令
result = subprocess.run(
["ffmpeg", "-version"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True, # 如果返回码非零则抛出异常
check=True, # 非零退出码会触发 CalledProcessError
)
# 检查输出中是否包含版本信息(可选)
output = result.stdout + result.stderr
if "ffmpeg version" in output.lower():
ffmpeg_installed = True
return False
except (subprocess.CalledProcessError, FileNotFoundError):
# 命令执行失败或未找到
ffmpeg_installed = False
if not ffmpeg_installed:
error_msg = "您的电脑还没正确安装ffmpeg\n"
error_msg += "\n建议您:\n"
error_msg += "1、按照项目的安装文档,正确进入conda环境\n"
error_msg += "2、查阅安装文档,如何在conda环境中安装ffmpeg\n"
raise ValueError(error_msg)
output = (result.stdout + result.stderr).lower()
if "ffmpeg version" in output:
return True
# 如果未检测到版本信息,也视为异常情况
raise ValueError("未检测到有效的 ffmpeg 版本输出。")
except (subprocess.CalledProcessError, FileNotFoundError) as e:
# 提取错误输出
stderr_output = ""
if isinstance(e, subprocess.CalledProcessError):
stderr_output = (e.stderr or "").strip()
else:
stderr_output = str(e).strip()
# 构建基础错误提示
error_msg = [
"❌ 检测到 ffmpeg 无法正常运行。\n",
"建议您:",
"1. 确认已正确激活 conda 环境;",
"2. 查阅项目安装文档,了解如何在 conda 环境中安装 ffmpeg。\n",
]
# 🎯 针对具体错误信息提供额外提示
if "libiconv.so.2" in stderr_output:
error_msg.append("⚠️ 发现缺少依赖库:libiconv.so.2")
error_msg.append("解决方法:在当前 conda 环境中执行:")
error_msg.append(" conda install -c conda-forge libiconv\n")
elif "no such file or directory" in stderr_output and "ffmpeg" in stderr_output.lower():
error_msg.append("⚠️ 系统未找到 ffmpeg 可执行文件。")
error_msg.append("解决方法:在当前 conda 环境中执行:")
error_msg.append(" conda install -c conda-forge ffmpeg\n")
else:
error_msg.append("错误详情:")
error_msg.append(stderr_output or "未知错误。")
# 抛出详细异常信息
raise ValueError("\n".join(error_msg)) from e
def extract_json_from_string(input_string):