feat: add short_side resize to build_ffmpeg_command
This commit is contained in:
@@ -30,18 +30,26 @@ def format_time(seconds: float) -> str:
|
|||||||
return f"{m}:{s:04.1f}"
|
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
|
# -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.
|
# (libx264/aac), so there is no keyframe-alignment issue from pre-input seek.
|
||||||
return [
|
cmd = [
|
||||||
"ffmpeg", "-y",
|
"ffmpeg", "-y",
|
||||||
"-ss", str(start),
|
"-ss", str(start),
|
||||||
"-i", input_path,
|
"-i", input_path,
|
||||||
"-t", "8",
|
"-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:
|
def _normalize_filename(filename: str) -> str:
|
||||||
|
|||||||
+10
-1
@@ -25,7 +25,7 @@ def test_format_time_no_sixty_rollover():
|
|||||||
assert format_time(59.95) == "0:59.9"
|
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")
|
cmd = build_ffmpeg_command("/in/video.mp4", 12.5, "/out/clip_001.mp4")
|
||||||
assert cmd[0] == "ffmpeg"
|
assert cmd[0] == "ffmpeg"
|
||||||
assert "-y" in cmd
|
assert "-y" in cmd
|
||||||
@@ -34,6 +34,15 @@ def test_ffmpeg_command():
|
|||||||
assert "-t" in cmd
|
assert "-t" in cmd
|
||||||
assert "8" in cmd
|
assert "8" in cmd
|
||||||
assert cmd[-1] == "/out/clip_001.mp4"
|
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 ---
|
# --- _normalize_filename ---
|
||||||
|
|||||||
Reference in New Issue
Block a user