Skip to content

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

backend: extract motion-filter builders into services/motion_filters

Nika Siradze63f1df4ab176
diff --git a/backend/services/motion_filters.py b/backend/services/motion_filters.py
new file mode 100644
index 0000000..a57ba82
--- /dev/null
+++ b/backend/services/motion_filters.py
@@ -0,0 +1,238 @@
+"""Pure helpers for building ffmpeg motion-aware filter expressions.
+
+All functions here are side-effect-free: they take keyframe / window
+lists and return strings or lists. No ffmpeg is actually invoked from
+this module — the caller passes the returned expressions into a
+filter_complex graph.
+
+Extracted from video_processor.py so the pure math can be unit-tested
+in isolation.
+"""
+
+from __future__ import annotations
+
+
+def build_cam_expr(
+ keyframes: list,
+ duration: float,
+ is_split: bool,
+ max_parts: int = 80,
+) -> str | None:
+ """Build an FFmpeg crop_x expression from SmoothedCameraman keyframes.
+
+ keyframes: [(time, crop_x), ...]
+ Returns the expression string, or None if too complex to express.
+ """
+ if not keyframes:
+ return None
+ # Ensure t=0 covered.
+ if keyframes[0][0] > 0.05:
+ keyframes = [(0, keyframes[0][1])] + keyframes
+ if len(keyframes) == 1:
+ return str(keyframes[0][1])
+
+ def _eased_move_expr(start_t: float, move_t: float, start_x: int, end_x: int) -> str:
+ # Smootherstep (6t^5 − 15t^4 + 10t^3) gives a softer start/landing
+ # than smoothstep while still finishing inside the bounded window.
+ progress = f"((t-{start_t:.3f})/{move_t:.3f})"
+ eased = (
+ f"((6*({progress})*({progress})*({progress})*({progress})*({progress}))"
+ f"-(15*({progress})*({progress})*({progress})*({progress}))"
+ f"+(10*({progress})*({progress})*({progress})))"
+ )
+ return f"{start_x}+(({end_x}-{start_x})*{eased})"
+
+ parts: list[str] = []
+ for i in range(len(keyframes) - 1):
+ t0, x0 = keyframes[i]
+ t1, x1 = keyframes[i + 1]
+ dt = max(0.01, t1 - t0)
+ jump = abs(x1 - x0)
+ # In split/mixed layouts, avoid both long pans and hard snaps.
+ # Force very short eased handoffs for virtually all jumps.
+ blurred_cut_jump = 100000 if is_split else 180
+ quick_reframe_jump = 24 if is_split else 90
+
+ if jump < 2:
+ # Negligible movement — hold.
+ parts.append(f"if(between(t\\,{t0:.3f}\\,{t1:.3f})\\,{x0}\\,")
+ elif jump >= blurred_cut_jump:
+ # Very large jumps in non-split layouts still read better as
+ # short blurred cuts than long animated slides.
+ parts.append(f"if(between(t\\,{t0:.3f}\\,{t1:.3f})\\,{x1}\\,")
+ elif jump >= quick_reframe_jump:
+ # Moderate jump: use a short bounded reframe, but keep it
+ # much tighter than a literal camera pan.
+ pan_t = min(0.08 if is_split else 0.18, dt)
+ pan_end_t = round(t0 + pan_t, 3)
+ parts.append(
+ f"if(between(t\\,{t0:.3f}\\,{pan_end_t:.3f})\\,"
+ f"{_eased_move_expr(t0, pan_t, x0, x1)}\\,"
+ )
+ if pan_end_t < t1:
+ parts.append(f"if(between(t\\,{pan_end_t:.3f}\\,{t1:.3f})\\,{x1}\\,")
+ else:
+ # Small pans still benefit from eased starts/stops so they
+ # read like a camera operator, not a value sliding on rails.
+ parts.append(
+ f"if(between(t\\,{t0:.3f}\\,{t1:.3f})\\,"
+ f"{_eased_move_expr(t0, dt, x0, x1)}\\,"
+ )
+
+ if len(parts) > max_parts:
+ return None
+
+ return "".join(parts) + str(keyframes[-1][1]) + ")" * len(parts)
+
+
+def motion_windows_from_keyframes(
+ keyframes: list,
+ min_jump: int = 60,
+ max_window_duration: float = 0.5,
+ max_windows: int = 16,
+) -> list[tuple[float, float]]:
+ """Find short reframe windows worth accenting with blur / zoom."""
+ windows: list[tuple[float, float]] = []
+ for i in range(len(keyframes) - 1):
+ t0, x0 = keyframes[i]
+ t1, x1 = keyframes[i + 1]
+ if abs(x1 - x0) < min_jump:
+ continue
+ dt = t1 - t0
+ if dt <= 0.01 or dt > max_window_duration:
+ continue
+ windows.append((t0, t1))
+
+ if not windows or len(windows) > max_windows:
+ return []
+ return windows
+
+
+def expand_motion_windows(
+ windows: list[tuple[float, float]],
+ pad_before: float = 0.03,
+ pad_after: float = 0.05,
+) -> list[tuple[float, float]]:
+ """Pad and merge adjacent motion windows so blur eases in and out."""
+ if not windows:
+ return []
+
+ expanded: list[tuple[float, float]] = []
+ for t0, t1 in windows:
+ start = max(0.0, t0 - pad_before)
+ end = t1 + pad_after
+ if expanded and start <= expanded[-1][1] + 0.01:
+ expanded[-1] = (expanded[-1][0], max(expanded[-1][1], end))
+ else:
+ expanded.append((start, end))
+ return expanded
+
+
+def build_motion_blur_filter(
+ keyframes: list,
+ min_jump: int = 60,
+ max_window_duration: float = 0.5,
+ max_windows: int = 16,
+ core_sigma: float = 3.8,
+ core_steps: int = 2,
+ core_pad_before: float = 0.02,
+ core_pad_after: float = 0.05,
+ outer_sigma: float = 1.6,
+ outer_steps: int = 1,
+ outer_pad_before: float = 0.05,
+ outer_pad_after: float = 0.08,
+) -> str:
+ """Build a layered full-frame blur filter for motion windows.
+
+ A soft outer blur eases the transition in and out, while a tighter
+ core blur covers the actual crop move. Empty string when no windows
+ qualify (caller concatenates safely).
+ """
+ base_windows = motion_windows_from_keyframes(
+ keyframes=keyframes,
+ min_jump=min_jump,
+ max_window_duration=max_window_duration,
+ max_windows=max_windows,
+ )
+ if not base_windows:
+ return ""
+
+ outer_windows = expand_motion_windows(
+ base_windows,
+ pad_before=outer_pad_before,
+ pad_after=outer_pad_after,
+ )
+ core_windows = expand_motion_windows(
+ base_windows,
+ pad_before=core_pad_before,
+ pad_after=core_pad_after,
+ )
+
+ outer_enable = "+".join(
+ f"between(t\\,{t0:.3f}\\,{t1:.3f})" for t0, t1 in outer_windows
+ )
+ core_enable = "+".join(
+ f"between(t\\,{t0:.3f}\\,{t1:.3f})" for t0, t1 in core_windows
+ )
+ return (
+ f",gblur=sigma={outer_sigma:.1f}:steps={outer_steps}:enable='{outer_enable}'"
+ f",gblur=sigma={core_sigma:.1f}:steps={core_steps}:enable='{core_enable}'"
+ )
+
+
+def build_motion_zoom_filter(
+ keyframes: list,
+ target_w: int,
+ target_h: int,
+ min_jump: int = 60,
+ max_window_duration: float = 0.5,
+ max_windows: int = 16,
+ max_zoom: float = 0.018,
+) -> str:
+ """Build a tiny center-zoom bump filter for motion windows.
+
+ The goal is a subliminal push that reads as an editorial beat, not
+ a visible punch-in. Empty string when no windows qualify.
+ """
+ windows = motion_windows_from_keyframes(
+ keyframes=keyframes,
+ min_jump=min_jump,
+ max_window_duration=max_window_duration,
+ max_windows=max_windows,
+ )
+ if not windows:
+ return ""
+
+ bumps: list[str] = []
+ for t0, t1 in windows:
+ dt = max(0.01, t1 - t0)
+ progress = f"((t-{t0:.3f})/{dt:.3f})"
+ bumps.append(
+ f"(16*pow({progress}\\,2)*pow((1-{progress})\\,2)*between(t\\,{t0:.3f}\\,{t1:.3f}))"
+ )
+
+ zoom_expr = "+".join(bumps)
+ return (
+ f",scale=w='iw*(1+{max_zoom:.4f}*({zoom_expr}))'"
+ f":h='ih*(1+{max_zoom:.4f}*({zoom_expr}))'"
+ f":eval=frame,crop={target_w}:{target_h}:(iw-{target_w})/2:(ih-{target_h})/2"
+ )
+
+
+def simplify_keyframes(keyframes: list, tolerance: int = 5) -> list:
+ """Remove intermediate keyframes that lie on a line between neighbours."""
+ if len(keyframes) <= 2:
+ return keyframes
+ result = [keyframes[0]]
+ for i in range(1, len(keyframes) - 1):
+ t_prev, x_prev = result[-1]
+ t_curr, x_curr = keyframes[i]
+ t_next, x_next = keyframes[i + 1]
+ dt_total = t_next - t_prev
+ if dt_total < 0.01:
+ continue
+ expected = x_prev + (x_next - x_prev) * (t_curr - t_prev) / dt_total
+ if abs(x_curr - expected) > tolerance:
+ result.append(keyframes[i])
+ result.append(keyframes[-1])
+ return result
diff --git a/backend/services/video_processor.py b/backend/services/video_processor.py
index ecc3ebd..384e729 100644
--- a/backend/services/video_processor.py
+++ b/backend/services/video_processor.py
@@ -27,6 +27,14 @@ from services.media_probe import (
)
from services.audio_normalize import normalize_audio
from services.video_cut import cut_segment, cut_multi_segment
+from services.motion_filters import (
+ build_cam_expr as _build_cam_expr,
+ build_motion_blur_filter as _build_motion_blur_filter,
+ build_motion_zoom_filter as _build_motion_zoom_filter,
+ expand_motion_windows as _expand_motion_windows,
+ motion_windows_from_keyframes as _motion_windows_from_keyframes,
+ simplify_keyframes as _simplify_keyframes,
+)
import sys
@@ -222,232 +230,6 @@ def _detect_split_screen(video_path: str, width: int, height: int) -> bool:
return False
-def _build_cam_expr(keyframes: list, duration: float, is_split: bool, max_parts: int = 80):
- """
- Build an FFmpeg crop_x expression from SmoothedCameraman keyframes.
-
- keyframes: [(time, crop_x), ...]
- Returns expression string, or None if too complex.
- """
- if not keyframes:
- return None
- # Ensure t=0 covered
- if keyframes[0][0] > 0.05:
- keyframes = [(0, keyframes[0][1])] + keyframes
- if len(keyframes) == 1:
- return str(keyframes[0][1])
-
- def _eased_move_expr(start_t: float, move_t: float, start_x: int, end_x: int) -> str:
- # Smootherstep gives a softer start/landing than smoothstep while still
- # finishing inside the bounded reframe window.
- progress = f"((t-{start_t:.3f})/{move_t:.3f})"
- eased = (
- f"((6*({progress})*({progress})*({progress})*({progress})*({progress}))"
- f"-(15*({progress})*({progress})*({progress})*({progress}))"
- f"+(10*({progress})*({progress})*({progress})))"
- )
- return f"{start_x}+(({end_x}-{start_x})*{eased})"
-
- parts = []
- for i in range(len(keyframes) - 1):
- t0, x0 = keyframes[i]
- t1, x1 = keyframes[i + 1]
- dt = max(0.01, t1 - t0)
- jump = abs(x1 - x0)
- # In split/mixed layouts, avoid both long pans and hard snaps.
- # Force very short eased handoffs for virtually all jumps.
- blurred_cut_jump = 100000 if is_split else 180
- quick_reframe_jump = 24 if is_split else 90
-
- if jump < 2:
- # Negligible movement — hold
- parts.append(f"if(between(t\\,{t0:.3f}\\,{t1:.3f})\\,{x0}\\,")
- elif jump >= blurred_cut_jump:
- # Very large jumps in non-split layouts still read better as short
- # blurred cuts than as long animated slides.
- parts.append(f"if(between(t\\,{t0:.3f}\\,{t1:.3f})\\,{x1}\\,")
- elif jump >= quick_reframe_jump:
- # Moderate jump: use a short bounded reframe, but keep it much
- # tighter than a literal camera pan.
- pan_t = min(0.08 if is_split else 0.18, dt)
- pan_end_t = round(t0 + pan_t, 3)
- parts.append(
- f"if(between(t\\,{t0:.3f}\\,{pan_end_t:.3f})\\,"
- f"{_eased_move_expr(t0, pan_t, x0, x1)}\\,"
- )
- if pan_end_t < t1:
- parts.append(f"if(between(t\\,{pan_end_t:.3f}\\,{t1:.3f})\\,{x1}\\,")
- else:
- # Small pans still benefit from eased starts/stops so they read like
- # a camera operator, not a value sliding on rails.
- parts.append(
- f"if(between(t\\,{t0:.3f}\\,{t1:.3f})\\,"
- f"{_eased_move_expr(t0, dt, x0, x1)}\\,"
- )
-
- if len(parts) > max_parts:
- return None
-
- return "".join(parts) + str(keyframes[-1][1]) + ")" * len(parts)
-
-
-def _motion_windows_from_keyframes(
- keyframes: list,
- min_jump: int = 60,
- max_window_duration: float = 0.5,
- max_windows: int = 16,
-) -> list[tuple[float, float]]:
- """Find short reframe windows worth accenting."""
- windows = []
- for i in range(len(keyframes) - 1):
- t0, x0 = keyframes[i]
- t1, x1 = keyframes[i + 1]
- if abs(x1 - x0) < min_jump:
- continue
- dt = t1 - t0
- if dt <= 0.01 or dt > max_window_duration:
- continue
- windows.append((t0, t1))
-
- if not windows or len(windows) > max_windows:
- return []
- return windows
-
-
-def _expand_motion_windows(
- windows: list[tuple[float, float]],
- pad_before: float = 0.03,
- pad_after: float = 0.05,
-) -> list[tuple[float, float]]:
- """Pad and merge motion windows so the blur eases in and out."""
- if not windows:
- return []
-
- expanded: list[tuple[float, float]] = []
- for t0, t1 in windows:
- start = max(0.0, t0 - pad_before)
- end = t1 + pad_after
- if expanded and start <= expanded[-1][1] + 0.01:
- expanded[-1] = (expanded[-1][0], max(expanded[-1][1], end))
- else:
- expanded.append((start, end))
- return expanded
-
-
-def _build_motion_blur_filter(
- keyframes: list,
- min_jump: int = 60,
- max_window_duration: float = 0.5,
- max_windows: int = 16,
- core_sigma: float = 3.8,
- core_steps: int = 2,
- core_pad_before: float = 0.02,
- core_pad_after: float = 0.05,
- outer_sigma: float = 1.6,
- outer_steps: int = 1,
- outer_pad_before: float = 0.05,
- outer_pad_after: float = 0.08,
-) -> str:
- """
- Add a layered full-frame blur while the camera is actively reframing.
-
- A soft outer blur eases the transition in and out, while a tighter core
- blur covers the actual crop move. This reads less like an abrupt on/off
- effect than a single blur window.
- """
- base_windows = _motion_windows_from_keyframes(
- keyframes=keyframes,
- min_jump=min_jump,
- max_window_duration=max_window_duration,
- max_windows=max_windows,
- )
- if not base_windows:
- return ""
-
- outer_windows = _expand_motion_windows(
- base_windows,
- pad_before=outer_pad_before,
- pad_after=outer_pad_after,
- )
- core_windows = _expand_motion_windows(
- base_windows,
- pad_before=core_pad_before,
- pad_after=core_pad_after,
- )
-
- outer_enable = "+".join(
- f"between(t\\,{t0:.3f}\\,{t1:.3f})"
- for t0, t1 in outer_windows
- )
- core_enable = "+".join(
- f"between(t\\,{t0:.3f}\\,{t1:.3f})"
- for t0, t1 in core_windows
- )
- return (
- f",gblur=sigma={outer_sigma:.1f}:steps={outer_steps}:enable='{outer_enable}'"
- f",gblur=sigma={core_sigma:.1f}:steps={core_steps}:enable='{core_enable}'"
- )
-
-
-def _build_motion_zoom_filter(
- keyframes: list,
- target_w: int,
- target_h: int,
- min_jump: int = 60,
- max_window_duration: float = 0.5,
- max_windows: int = 16,
- max_zoom: float = 0.018,
-) -> str:
- """
- Add a tiny center zoom bump during short reframes.
-
- The goal is not visible punch-in. It just helps the move read as an
- intentional editorial transition instead of a hard crop shove.
- """
- windows = _motion_windows_from_keyframes(
- keyframes=keyframes,
- min_jump=min_jump,
- max_window_duration=max_window_duration,
- max_windows=max_windows,
- )
- if not windows:
- return ""
-
- bumps = []
- for t0, t1 in windows:
- dt = max(0.01, t1 - t0)
- progress = f"((t-{t0:.3f})/{dt:.3f})"
- bumps.append(
- f"(16*pow({progress}\\,2)*pow((1-{progress})\\,2)*between(t\\,{t0:.3f}\\,{t1:.3f}))"
- )
-
- zoom_expr = "+".join(bumps)
- return (
- f",scale=w='iw*(1+{max_zoom:.4f}*({zoom_expr}))'"
- f":h='ih*(1+{max_zoom:.4f}*({zoom_expr}))'"
- f":eval=frame,crop={target_w}:{target_h}:(iw-{target_w})/2:(ih-{target_h})/2"
- )
-
-
-def _simplify_keyframes(keyframes: list, tolerance: int = 5) -> list:
- """Remove intermediate keyframes that lie on a line between neighbours."""
- if len(keyframes) <= 2:
- return keyframes
- result = [keyframes[0]]
- for i in range(1, len(keyframes) - 1):
- t_prev, x_prev = result[-1]
- t_curr, x_curr = keyframes[i]
- t_next, x_next = keyframes[i + 1]
- dt_total = t_next - t_prev
- if dt_total < 0.01:
- continue
- expected = x_prev + (x_next - x_prev) * (t_curr - t_prev) / dt_total
- if abs(x_curr - expected) > tolerance:
- result.append(keyframes[i])
- result.append(keyframes[-1])
- return result
-
-
def _resolve_speaker_sides(
segments: list,
detections: list,
diff --git a/tests/test_motion_filters.py b/tests/test_motion_filters.py
new file mode 100644
index 0000000..485e92a
--- /dev/null
+++ b/tests/test_motion_filters.py
@@ -0,0 +1,187 @@
+"""Tests for backend.services.motion_filters — pure string builders."""
+
+import os
+import sys
+import unittest
+
+ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
+BACKEND_ROOT = os.path.join(ROOT, "backend")
+if BACKEND_ROOT not in sys.path:
+ sys.path.insert(0, BACKEND_ROOT)
+
+from services import motion_filters as mf
+
+
+class BuildCamExprTests(unittest.TestCase):
+ def test_empty_returns_none(self):
+ self.assertIsNone(mf.build_cam_expr([], duration=5.0, is_split=False))
+
+ def test_single_keyframe_returns_constant(self):
+ # One keyframe → constant expression
+ self.assertEqual(
+ mf.build_cam_expr([(0.0, 420)], duration=5.0, is_split=False),
+ "420",
+ )
+
+ def test_t0_is_back_filled(self):
+ # First keyframe at t=0.5 → a (0, x) entry is prepended
+ expr = mf.build_cam_expr([(0.5, 420), (2.0, 420)], duration=5.0, is_split=False)
+ self.assertIsNotNone(expr)
+ self.assertIn("0.000", expr)
+
+ def test_returns_none_when_too_many_parts(self):
+ # Force more parts than max_parts → None (too complex)
+ keyframes = [(i * 0.1, i * 100) for i in range(20)]
+ self.assertIsNone(
+ mf.build_cam_expr(keyframes, duration=2.0, is_split=False, max_parts=5)
+ )
+
+ def test_negligible_movement_produces_hold(self):
+ # Jump < 2 → hold at x0
+ expr = mf.build_cam_expr(
+ [(0.0, 100), (1.0, 101)], duration=2.0, is_split=False
+ )
+ self.assertIsNotNone(expr)
+ self.assertIn("100", expr)
+
+
+class MotionWindowsFromKeyframesTests(unittest.TestCase):
+ def test_no_keyframes_returns_empty(self):
+ self.assertEqual(mf.motion_windows_from_keyframes([]), [])
+
+ def test_small_jumps_are_filtered(self):
+ # Jump = 10 < min_jump=60 → filtered out
+ keyframes = [(0.0, 100), (0.5, 110)]
+ self.assertEqual(mf.motion_windows_from_keyframes(keyframes), [])
+
+ def test_large_jump_creates_window(self):
+ keyframes = [(0.0, 100), (0.2, 200)]
+ windows = mf.motion_windows_from_keyframes(keyframes, min_jump=50)
+ self.assertEqual(windows, [(0.0, 0.2)])
+
+ def test_slow_jumps_are_excluded(self):
+ # A huge but slow transition > max_window_duration should not count
+ keyframes = [(0.0, 100), (2.0, 300)]
+ self.assertEqual(
+ mf.motion_windows_from_keyframes(keyframes, max_window_duration=0.5), []
+ )
+
+ def test_instant_jumps_are_excluded(self):
+ # dt <= 0.01 → excluded (not a real window)
+ keyframes = [(0.0, 100), (0.005, 200)]
+ self.assertEqual(mf.motion_windows_from_keyframes(keyframes), [])
+
+ def test_too_many_windows_returns_empty(self):
+ # If we exceed max_windows, fall back to an empty list
+ # (too noisy to blur-annotate)
+ keyframes = []
+ t = 0.0
+ for _ in range(20):
+ keyframes.append((t, 0))
+ keyframes.append((t + 0.1, 200))
+ t += 0.3
+ self.assertEqual(
+ mf.motion_windows_from_keyframes(keyframes, max_windows=5), []
+ )
+
+
+class ExpandMotionWindowsTests(unittest.TestCase):
+ def test_empty_returns_empty(self):
+ self.assertEqual(mf.expand_motion_windows([]), [])
+
+ def test_single_window_gets_padded(self):
+ result = mf.expand_motion_windows(
+ [(1.0, 2.0)], pad_before=0.1, pad_after=0.2
+ )
+ self.assertEqual(result, [(0.9, 2.2)])
+
+ def test_t0_padding_clamped_to_zero(self):
+ result = mf.expand_motion_windows(
+ [(0.05, 1.0)], pad_before=0.5, pad_after=0.1
+ )
+ # Start clamped to 0.0
+ self.assertEqual(result[0][0], 0.0)
+
+ def test_overlapping_windows_merge(self):
+ # Two windows with overlapping padded ranges should merge
+ result = mf.expand_motion_windows(
+ [(1.0, 2.0), (2.05, 3.0)], pad_before=0.05, pad_after=0.1
+ )
+ self.assertEqual(len(result), 1)
+ self.assertEqual(result[0][0], 0.95)
+ self.assertGreaterEqual(result[0][1], 3.1)
+
+ def test_non_overlapping_windows_stay_separate(self):
+ result = mf.expand_motion_windows(
+ [(1.0, 2.0), (5.0, 6.0)], pad_before=0.05, pad_after=0.1
+ )
+ self.assertEqual(len(result), 2)
+
+
+class BuildMotionBlurFilterTests(unittest.TestCase):
+ def test_no_motion_returns_empty_string(self):
+ self.assertEqual(mf.build_motion_blur_filter([(0.0, 100), (1.0, 100)]), "")
+
+ def test_motion_produces_gblur_chain(self):
+ keyframes = [(0.0, 100), (0.2, 200)]
+ result = mf.build_motion_blur_filter(keyframes, min_jump=50)
+ self.assertIn("gblur", result)
+ self.assertIn("sigma=", result)
+ # Should have both outer and core blur layers
+ self.assertEqual(result.count("gblur"), 2)
+
+ def test_enable_expressions_use_between(self):
+ keyframes = [(0.0, 100), (0.2, 200)]
+ result = mf.build_motion_blur_filter(keyframes, min_jump=50)
+ self.assertIn("between(t", result)
+
+
+class BuildMotionZoomFilterTests(unittest.TestCase):
+ def test_no_motion_returns_empty_string(self):
+ self.assertEqual(
+ mf.build_motion_zoom_filter(
+ [(0.0, 100), (1.0, 100)], target_w=1080, target_h=1920
+ ),
+ "",
+ )
+
+ def test_motion_produces_scale_plus_crop(self):
+ keyframes = [(0.0, 100), (0.2, 200)]
+ result = mf.build_motion_zoom_filter(
+ keyframes, target_w=1080, target_h=1920, min_jump=50
+ )
+ self.assertIn("scale=", result)
+ self.assertIn("crop=1080:1920", result)
+ self.assertIn("1+0.0180", result)
+
+
+class SimplifyKeyframesTests(unittest.TestCase):
+ """Parity check against the video_processor_helpers tests — ensures
+ the extraction kept the same behavior."""
+
+ def test_collinear_middle_dropped(self):
+ self.assertEqual(
+ mf.simplify_keyframes([(0, 0), (5, 500), (10, 1000)]),
+ [(0, 0), (10, 1000)],
+ )
+
+ def test_tolerance_loose(self):
+ self.assertEqual(
+ mf.simplify_keyframes([(0, 0), (5, 510), (10, 1000)], tolerance=50),
+ [(0, 0), (10, 1000)],
+ )
+
+
+class ReExportTests(unittest.TestCase):
+ def test_video_processor_reexports_motion_helpers(self):
+ from services import video_processor as vp
+ self.assertIs(vp._build_cam_expr, mf.build_cam_expr)
+ self.assertIs(vp._motion_windows_from_keyframes, mf.motion_windows_from_keyframes)
+ self.assertIs(vp._expand_motion_windows, mf.expand_motion_windows)
+ self.assertIs(vp._build_motion_blur_filter, mf.build_motion_blur_filter)
+ self.assertIs(vp._build_motion_zoom_filter, mf.build_motion_zoom_filter)
+ self.assertIs(vp._simplify_keyframes, mf.simplify_keyframes)
+
+
+if __name__ == "__main__":
+ unittest.main()