Heuristic:869413421 Ai moive studio FFmpeg Check Caching Tip
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Video, Infrastructure |
| Last Updated | 2026-10-23 14:00 GMT |
Overview
Cache the FFmpeg installation check result globally and assume installed on timeout, to avoid blocking video synthesis tasks under high system load.
Description
The check_ffmpeg_installed() function uses a module-level global cache (_ffmpeg_installed_cache) to avoid repeated FFmpeg version checks. The first call runs shutil.which("ffmpeg") and ffmpeg -version with a 20-second timeout. If the version check times out (indicating high system load), the function assumes FFmpeg is installed and returns True — preferring to attempt the operation rather than block the business workflow. Subsequent calls return the cached result immediately.
Usage
Use this heuristic when:
- Checking for external tool availability before batch operations.
- The system may be under high load where tool checks time out but the tool is actually installed.
- You want to avoid repeated subprocess calls for the same check.
The Insight (Rule of Thumb)
- Action: Cache the result of check_ffmpeg_installed() in a global variable. On timeout, assume installed (return True).
- Value: Timeout increased from 5s to 20s. On timeout: return True (optimistic). Cache result permanently.
- Trade-off: If FFmpeg is genuinely not installed, the actual FFmpeg command will fail later with a clear error. The optimistic timeout approach avoids blocking business tasks under transient load.
- Compatibility: Only caches after a definitive result (True or False). On timeout, caches True to reduce future interference.
Reasoning
FFmpeg version check via subprocess can take longer than expected under high CPU/IO load (many concurrent video synthesis tasks). Blocking the check for too long delays the entire video synthesis pipeline. Since:
- The check is only a pre-flight validation — the actual FFmpeg command will fail clearly if not installed.
- Under high load, the tool is almost certainly installed (it was working before).
- The 20s timeout is generous; exceeding it means the system is truly overloaded.
The cache avoids calling ffmpeg -version on every video synthesis task, reducing subprocess overhead.
Code Evidence
Caching and timeout logic from `ffmpeg_utils.py:19-66`:
_ffmpeg_installed_cache = None
def check_ffmpeg_installed() -> bool:
global _ffmpeg_installed_cache
if _ffmpeg_installed_cache is not None:
return _ffmpeg_installed_cache
if not shutil.which("ffmpeg"):
_ffmpeg_installed_cache = False
return False
try:
result = subprocess.run(
["ffmpeg", "-version"], capture_output=True, text=True, timeout=20
)
if result.returncode == 0:
_ffmpeg_installed_cache = True
return True
else:
_ffmpeg_installed_cache = False
return False
except subprocess.TimeoutExpired:
# 如果超时但 shutil.which 过了,可能只是运行慢,返回 True 以避免阻塞业务
_ffmpeg_installed_cache = True
return True
Usage in video synthesis from `video_synthesis.py:342-343`:
if not check_ffmpeg_installed():
raise BusinessLogicError("FFmpeg未安装或不可用")