Files
BiliNote/backend/ffmpeg_helper.py
Jefferyhcool 0e0b8da317 first commit
2025-04-13 17:44:54 +08:00

34 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
import subprocess
from dotenv import load_dotenv
load_dotenv()
def check_ffmpeg_exists() -> bool:
"""
检查 ffmpeg 是否可用。优先使用 FFMPEG_BIN_PATH 环境变量指定的路径。
"""
ffmpeg_bin_path = os.getenv("FFMPEG_BIN_PATH")
print(f"FFMPEG_BIN_PATH: {ffmpeg_bin_path}")
if ffmpeg_bin_path and os.path.isdir(ffmpeg_bin_path):
os.environ["PATH"] = ffmpeg_bin_path + os.pathsep + os.environ.get("PATH", "")
try:
subprocess.run(["ffmpeg", "-version"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
return True
except (FileNotFoundError, OSError, subprocess.CalledProcessError):
return False
def ensure_ffmpeg_or_raise():
"""
校验 ffmpeg 是否可用,否则抛出异常并提示安装方式。
"""
if not check_ffmpeg_exists():
raise EnvironmentError(
"❌ 未检测到 ffmpeg请先安装后再使用本功能。\n"
"👉 下载地址https://ffmpeg.org/download.html\n"
"🪟 Windows 推荐https://www.gyan.dev/ffmpeg/builds/\n"
"💡 如果你已安装,请将其路径写入 `.env` 文件,例如:\n"
"FFMPEG_BIN_PATH=/your/custom/ffmpeg/bin"
)