From f76c5a326f1a468acc877cbfd7fb57404a0dc069 Mon Sep 17 00:00:00 2001 From: eric Date: Fri, 14 Aug 2026 21:41:32 -0500 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=8A=96=E9=9F=B3?= =?UTF-8?q?=E7=9F=AD=E5=BD=95=20ffmpeg=20reconnect=20=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=20Invalid=20argument?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- backend/app/shorts_clip.py | 105 +++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 46 deletions(-) diff --git a/backend/app/shorts_clip.py b/backend/app/shorts_clip.py index eddcfcb..2bcba21 100644 --- a/backend/app/shorts_clip.py +++ b/backend/app/shorts_clip.py @@ -189,61 +189,63 @@ async def record_clip( out_path.unlink() except OSError: pass - # ffmpeg flags aligned with DouyinLiveRecorder start_record (sans -re for short clips) + is_hls = ".m3u8" in url.lower() or "/playlist" in url.lower() + # Keep input options conservative: bad reconnect flags caused EINVAL on Douyin CDN. + # Short clips don't need long-lived reconnect like DouyinLiveRecorder full captures. cmd = [ settings.ffmpeg_bin, "-y", "-hide_banner", "-loglevel", "error", - "-user_agent", - _user_agent(), "-headers", _headers_arg(), - "-protocol_whitelist", - "file,http,https,tcp,tls,crypto,rtmp,httpproxy,ipv4", - "-thread_queue_size", - "1024", - "-analyzeduration", - "20000000", - "-probesize", - "20000000", - "-fflags", - "+discardcorrupt", "-rw_timeout", - "30000000", - "-reconnect", - "1", - "-reconnect_streamed", - "-reconnect_at_eof", - "-reconnect_delay_max", - "15", - "-i", - url, - "-t", - str(sec), - "-vf", - vf, - "-c:v", - "libx264", - "-preset", - "veryfast", - "-crf", - "23", - "-c:a", - "aac", - "-b:a", - "128k", - "-movflags", - "+faststart", - "-avoid_negative_ts", - "1", - str(out_path), + "20000000", ] + if not is_hls: + # HTTP FLV reconnect (must pass explicit 1 — bare flags break avformat_open_input) + cmd.extend( + [ + "-reconnect", + "1", + "-reconnect_streamed", + "1", + "-reconnect_at_eof", + "1", + "-reconnect_delay_max", + "8", + ] + ) + cmd.extend( + [ + "-i", + url, + "-t", + str(sec), + "-vf", + vf, + "-c:v", + "libx264", + "-preset", + "veryfast", + "-crf", + "23", + "-c:a", + "aac", + "-b:a", + "128k", + "-movflags", + "+faststart", + "-avoid_negative_ts", + "make_zero", + str(out_path), + ] + ) - def _run() -> dict[str, Any]: + def _run(cmd_local: list[str] = cmd, used_url: str = url) -> dict[str, Any]: try: - proc = subprocess.run(cmd, capture_output=True, text=True, timeout=sec + 90) + proc = subprocess.run(cmd_local, capture_output=True, text=True, timeout=sec + 90) except subprocess.TimeoutExpired: return {"ok": False, "error": "ffmpeg 录制超时"} except Exception as exc: @@ -258,7 +260,7 @@ async def record_clip( "path": str(out_path), "bytes": out_path.stat().st_size, "seconds": sec, - "stream_url": url, + "stream_url": used_url, "candidate_index": idx, } @@ -268,10 +270,21 @@ async def record_clip( last_err = str(outcome.get("error") or last_err or "录制失败") # Only cascade on CDN / open failures; keep other errors low = last_err.lower() - if not any(x in low for x in ("404", "403", "error opening", "server returned", "input/output", "timed out", "timeout")): + if not any( + x in low + for x in ( + "404", + "403", + "error opening", + "server returned", + "input/output", + "timed out", + "timeout", + "invalid argument", + ) + ): if len(urls) == 1: break - # still try next line for transient open errors continue return {"ok": False, "error": last_err or "录制失败"}