From 798c5ff4f4b40580c3f870cb24d4137478def69a Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Mon, 6 Apr 2026 13:31:58 +0200 Subject: [PATCH] feat: add short_side resize to build_ffmpeg_command --- main.py | 18 +++++++++++++----- tests/test_utils.py | 11 ++++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 3cf9b69..51ff4d9 100644 --- a/main.py +++ b/main.py @@ -30,18 +30,26 @@ def format_time(seconds: float) -> str: return f"{m}:{s:04.1f}" -def build_ffmpeg_command(input_path: str, start: float, output_path: str) -> list[str]: +def build_ffmpeg_command( + input_path: str, start: float, output_path: str, + short_side: int | None = None, +) -> list[str]: # -ss before -i: fast input-seeking. Safe here because we always re-encode # (libx264/aac), so there is no keyframe-alignment issue from pre-input seek. - return [ + cmd = [ "ffmpeg", "-y", "-ss", str(start), "-i", input_path, "-t", "8", - "-c:v", "libx264", - "-c:a", "aac", - output_path, ] + if short_side is not None: + # Scale so the shorter dimension equals short_side. + # if(lt(iw,ih),...) → portrait: fix width; landscape: fix height. + # -2 keeps aspect ratio with even-pixel rounding (libx264 requirement). + scale = f"scale='if(lt(iw,ih),{short_side},-2)':'if(lt(iw,ih),-2,{short_side})'" + cmd += ["-vf", scale] + cmd += ["-c:v", "libx264", "-c:a", "aac", output_path] + return cmd def _normalize_filename(filename: str) -> str: diff --git a/tests/test_utils.py b/tests/test_utils.py index d99d7cb..3b283b8 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -25,7 +25,7 @@ def test_format_time_no_sixty_rollover(): assert format_time(59.95) == "0:59.9" -def test_ffmpeg_command(): +def test_ffmpeg_command_no_resize(): cmd = build_ffmpeg_command("/in/video.mp4", 12.5, "/out/clip_001.mp4") assert cmd[0] == "ffmpeg" assert "-y" in cmd @@ -34,6 +34,15 @@ def test_ffmpeg_command(): assert "-t" in cmd assert "8" in cmd assert cmd[-1] == "/out/clip_001.mp4" + assert "-vf" not in cmd + +def test_ffmpeg_command_with_resize(): + cmd = build_ffmpeg_command("/in/video.mp4", 0.0, "/out/clip_001.mp4", short_side=256) + assert "-vf" in cmd + vf_value = cmd[cmd.index("-vf") + 1] + assert "256" in vf_value + assert "scale" in vf_value + assert cmd[-1] == "/out/clip_001.mp4" # --- _normalize_filename ---