Handle ffmpeg probe non-zero exits separately

This commit is contained in:
2026-05-24 19:59:12 +00:00
parent eeb28e991f
commit 93d89e32e2
+18 -3
View File
@@ -51,8 +51,16 @@ def _log_ffmpeg_not_found_once(cmd: list[str]) -> None:
) )
async def _run_ffmpeg(cmd: list[str], timeout: float) -> tuple[bytes, bytes] | None: async def _run_ffmpeg(
"""Run ffmpeg with consistent timeout/crash/not-found handling and logging.""" cmd: list[str],
timeout: float,
allow_nonzero_exit: bool = False,
) -> tuple[bytes, bytes] | None:
"""Run ffmpeg with consistent timeout/crash/not-found handling and logging.
Set ``allow_nonzero_exit`` for probe-style commands where ffmpeg may return
a non-zero code while still emitting useful metadata on stderr.
"""
proc: asyncio.subprocess.Process | None = None proc: asyncio.subprocess.Process | None = None
try: try:
logger.debug(" $ %s", shlex.join(cmd)) logger.debug(" $ %s", shlex.join(cmd))
@@ -77,6 +85,13 @@ async def _run_ffmpeg(cmd: list[str], timeout: float) -> tuple[bytes, bytes] | N
return None return None
if proc.returncode != 0: if proc.returncode != 0:
if allow_nonzero_exit:
logger.debug(
"ffmpeg command exited non-zero as expected for probe. cmd=%s returncode=%s",
shlex.join(cmd),
proc.returncode,
)
return stdout, stderr
logger.error( logger.error(
"ffmpeg command failed. cmd=%s stderr=%s", "ffmpeg command failed. cmd=%s stderr=%s",
shlex.join(cmd), shlex.join(cmd),
@@ -428,7 +443,7 @@ async def probe_media_info(video_path: str) -> MediaProbeInfo:
info = MediaProbeInfo() info = MediaProbeInfo()
cmd = ["ffmpeg", "-hide_banner", "-i", video_path] cmd = ["ffmpeg", "-hide_banner", "-i", video_path]
ffmpeg_result = await _run_ffmpeg(cmd, timeout=30) ffmpeg_result = await _run_ffmpeg(cmd, timeout=30, allow_nonzero_exit=True)
if ffmpeg_result is None: if ffmpeg_result is None:
_media_probe_cache[video_path] = info _media_probe_cache[video_path] = info
return info return info