Skip to content

Open-source AI podcast clipper. Generate vertical clips with face tracking and burned-in captions. CLI, MCP server, web app.

Never let a crop failure lose the clip (2.7.5) (#175)

Nika Siradze8c2185f4b9b3
diff --git a/backend/services/video_processor.py b/backend/services/video_processor.py
index 1b64f3c..b7a5451 100644
--- a/backend/services/video_processor.py
+++ b/backend/services/video_processor.py
@@ -203,7 +203,15 @@ def crop_to_vertical(
and len(face_map.get("clusters") or []) == 2
)
if is_pure_split:
- plan = _detect_local_speaker_reframe_plan(input_path, face_map)
+ # Same reasoning as _track_and_crop: this is one of several ways to
+ # find a crop, and the caller can do without it. An exception here
+ # would fail the clip instead of trying the next one.
+ try:
+ plan = _detect_local_speaker_reframe_plan(input_path, face_map)
+ except Exception as exc:
+ log_event("crop", "local_reframe_failed", level="warn",
+ error=f"{type(exc).__name__}: {exc}")
+ plan = None
if plan and plan["mode"] == "pan":
crop_y = max(0, (height - plan["crop_h"]) // 2)
vf = (
@@ -1171,9 +1179,42 @@ def _track_and_crop(
height: int,
target_w: int,
target_h: int,
- transcript_words: list = None,
+ transcript_words: Optional[list] = None,
clip_start: float = 0,
- face_map: dict = None,
+ face_map: Optional[dict] = None,
+) -> Optional[str]:
+ """Adaptive face tracking, with any failure reported as "no crop".
+
+ Every caller already treats None as "this did not work, try something
+ simpler", and the paths behind that are a plain face-map crop and a centre
+ crop. Letting an exception out instead skips all of them and fails the
+ clip: proc.run raises on a timeout even with check=False, so one ffmpeg
+ that hung took a whole render down and left every moment in it marked as
+ never rendered. A badly framed clip is recoverable, a missing one is not.
+ """
+ try:
+ return _track_and_crop_inner(
+ input_path, output_path, width, height, target_w, target_h,
+ transcript_words, clip_start, face_map,
+ )
+ except Exception as exc:
+ log_event(
+ "crop", "track_failed", level="warn",
+ error=f"{type(exc).__name__}: {exc}",
+ )
+ return None
+
+
+def _track_and_crop_inner(
+ input_path: str,
+ output_path: str,
+ width: int,
+ height: int,
+ target_w: int,
+ target_h: int,
+ transcript_words: Optional[list] = None,
+ clip_start: float = 0,
+ face_map: Optional[dict] = None,
) -> Optional[str]:
"""
Adaptive face tracking with sticky visual tracks and heavy-tripod movement.
@@ -1584,7 +1625,14 @@ def _track_and_crop(
cmd += [
"-c:v", "libx264", "-crf", "18", "-preset", "fast",
"-movflags", "+faststart",
- "-shortest",
+ # An explicit length, not -shortest. The audio comes
+ # from a plain input while the video comes out of a
+ # filtergraph, and -shortest has to wait for both to
+ # agree on where the end is; on some ffmpeg builds it
+ # simply never does. A hung ffmpeg is worse than a
+ # wrong one here, because proc.run turns a timeout into
+ # an exception that takes the whole render down with it.
+ "-t", f"{duration:.3f}",
output_path,
]
r = proc_run(cmd, timeout=_FFMPEG_TIMEOUT, check=False)
diff --git a/cli/VERSION b/cli/VERSION
index a4dd9db..a603bb5 100644
--- a/cli/VERSION
+++ b/cli/VERSION
@@ -1 +1 @@
-2.7.4
+2.7.5
diff --git a/package.json b/package.json
index c1d5892..ac014b5 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "podcli",
- "version": "2.7.4",
+ "version": "2.7.5",
"private": true,
"description": "AI-powered podcast clip generator for TikTok/YouTube Shorts. Transcribe, find viral moments, export vertical clips with burned captions.",
"type": "module",
diff --git a/tests/test_crop_path_golden.py b/tests/test_crop_path_golden.py
index 18ba2bc..c20d3b0 100644
--- a/tests/test_crop_path_golden.py
+++ b/tests/test_crop_path_golden.py
@@ -258,8 +258,6 @@ class DumpCropPathTests(unittest.TestCase):
vp._dump_crop_path(**self.KWARGS) # must not raise
-if __name__ == "__main__":
- unittest.main()
class UseFaceMapSpeakerGuardTests(unittest.TestCase):
@@ -401,3 +399,41 @@ class MixedLayoutDetectionTests(unittest.TestCase):
# A clip that sits entirely inside one layout still belongs to a mixed
# episode; the episode-wide flag remains a valid hint.
self.assertTrue(self._decide(self._frames(80, 0), {"is_mixed_layout": True}))
+
+
+class CropFailuresNeverFailTheClipTests(unittest.TestCase):
+ """A crop that cannot be computed must report "no crop", never raise.
+
+ Every caller reads None as "try something simpler" and falls through to a
+ face-map crop and then a centre crop. An exception skips all of that and
+ fails the clip. utils.proc.run raises on a timeout even with check=False,
+ so a single ffmpeg that hung took a whole render down and left every
+ moment in it showing "Did not render".
+ """
+
+ def test_an_exception_inside_tracking_becomes_none(self):
+ with mock.patch.object(
+ vp, "_track_and_crop_inner", side_effect=RuntimeError("ffmpeg hung"),
+ ):
+ self.assertIsNone(
+ vp._track_and_crop("in.mp4", "out.mp4", 1920, 1080, 1080, 1920)
+ )
+
+ def test_a_proc_timeout_becomes_none(self):
+ from utils.proc import ProcError
+ boom = ProcError(["ffmpeg"], -1, "timeout after 600s", 600.0)
+ with mock.patch.object(vp, "_track_and_crop_inner", side_effect=boom):
+ self.assertIsNone(
+ vp._track_and_crop("in.mp4", "out.mp4", 1920, 1080, 1080, 1920)
+ )
+
+ def test_a_working_track_is_returned_untouched(self):
+ with mock.patch.object(vp, "_track_and_crop_inner", return_value="out.mp4"):
+ self.assertEqual(
+ vp._track_and_crop("in.mp4", "out.mp4", 1920, 1080, 1080, 1920),
+ "out.mp4",
+ )
+
+
+if __name__ == "__main__":
+ unittest.main()