feat: export conforms to clip majority fps
This commit is contained in:
@@ -30,10 +30,6 @@ DEFAULT_EXPORT_FPS = 30.0
|
||||
XFADE_SEC = 0.5
|
||||
PROGRESS_MIN_INTERVAL_SEC = 1.0
|
||||
|
||||
# Retained for the pre-B2 v_chain reference; removed when build_ffmpeg_cmd
|
||||
# switches to the voted rate.
|
||||
EXPORT_FPS = DEFAULT_EXPORT_FPS
|
||||
|
||||
_running: dict[int, asyncio.subprocess.Process] = {}
|
||||
|
||||
|
||||
@@ -125,7 +121,7 @@ async def probe(path: str | Path) -> dict[str, Any]:
|
||||
"-v",
|
||||
"error",
|
||||
"-show_entries",
|
||||
"format=duration:stream=codec_type",
|
||||
"format=duration:stream=codec_type,r_frame_rate",
|
||||
"-of",
|
||||
"json",
|
||||
str(path),
|
||||
@@ -138,20 +134,34 @@ async def probe(path: str | Path) -> dict[str, Any]:
|
||||
raise RuntimeError(f"ffprobe failed for {path}: {tail}")
|
||||
data = json.loads(out.decode("utf-8", "replace") or "{}")
|
||||
duration = float((data.get("format") or {}).get("duration") or 0.0)
|
||||
has_audio = any(s.get("codec_type") == "audio" for s in data.get("streams") or [])
|
||||
return {"duration": duration, "has_audio": has_audio}
|
||||
streams = data.get("streams") or []
|
||||
has_audio = any(s.get("codec_type") == "audio" for s in streams)
|
||||
video_fps = 0.0
|
||||
for s in streams:
|
||||
if s.get("codec_type") == "video":
|
||||
video_fps = parse_rate(s.get("r_frame_rate"))
|
||||
break
|
||||
return {"duration": duration, "has_audio": has_audio, "fps": video_fps}
|
||||
|
||||
|
||||
def build_ffmpeg_cmd(
|
||||
clips: list[dict[str, Any]],
|
||||
probes: list[dict[str, Any]],
|
||||
dest: str | Path,
|
||||
*,
|
||||
fps: float | None = None,
|
||||
) -> list[str]:
|
||||
"""Assemble the validated normalize → xfade → loudnorm ffmpeg command."""
|
||||
"""Assemble the validated normalize → xfade → loudnorm ffmpeg command.
|
||||
|
||||
fps=None conforms every clip to target_fps(probes) — the clips' own
|
||||
majority rate. Pass an explicit fps to override the vote.
|
||||
"""
|
||||
if not clips:
|
||||
raise RuntimeError("No clips to export")
|
||||
if len(clips) != len(probes):
|
||||
raise ValueError("clips and probes must align")
|
||||
rate = float(fps) if fps else target_fps(probes)
|
||||
rate_str = f"{rate:.6g}"
|
||||
|
||||
n = len(clips)
|
||||
ffmpeg = shutil.which("ffmpeg") or "ffmpeg"
|
||||
@@ -171,7 +181,7 @@ def build_ffmpeg_cmd(
|
||||
v_chain = (
|
||||
f"scale={EXPORT_WIDTH}:{EXPORT_HEIGHT}:force_original_aspect_ratio=decrease,"
|
||||
f"pad={EXPORT_WIDTH}:{EXPORT_HEIGHT}:(ow-iw)/2:(oh-ih)/2,"
|
||||
f"setsar=1,fps={EXPORT_FPS},settb=AVTB"
|
||||
f"setsar=1,fps={rate_str},settb=AVTB"
|
||||
)
|
||||
a_chain = "aformat=sample_rates=48000:channel_layouts=stereo"
|
||||
|
||||
|
||||
@@ -232,3 +232,37 @@ def test_target_fps_missing_falls_back_to_default():
|
||||
assert target_fps([]) == DEFAULT_EXPORT_FPS == 30.0
|
||||
assert target_fps([{"fps": 0.0}, {"fps": None}]) == 30.0
|
||||
assert target_fps([{"duration": 5.0, "has_audio": True}]) == 30.0
|
||||
|
||||
|
||||
def test_build_ffmpeg_cmd_explicit_fps_24():
|
||||
clips = [{"video_path": "a.mp4"}]
|
||||
probes = [{"duration": 5.0, "has_audio": True, "fps": 30.0}]
|
||||
cmd = build_ffmpeg_cmd(clips, probes, "out.mp4", fps=24.0)
|
||||
joined = " ".join(str(c) for c in cmd)
|
||||
assert "fps=24" in joined
|
||||
assert "fps=30" not in joined
|
||||
|
||||
|
||||
def test_build_ffmpeg_cmd_defaults_to_target_fps_vote():
|
||||
clips = [{"video_path": "a.mp4"}, {"video_path": "b.mp4"}]
|
||||
probes = [
|
||||
{"duration": 5.0, "has_audio": True, "fps": 24.0},
|
||||
{"duration": 5.0, "has_audio": True, "fps": 24.0},
|
||||
]
|
||||
cmd = build_ffmpeg_cmd(clips, probes, "out.mp4")
|
||||
joined = " ".join(str(c) for c in cmd)
|
||||
assert "fps=24" in joined
|
||||
assert "fps=30" not in joined
|
||||
|
||||
|
||||
def test_build_ffmpeg_cmd_fractional_fps_formatting():
|
||||
clips = [{"video_path": "a.mp4"}]
|
||||
probes = [{"duration": 5.0, "has_audio": True, "fps": 23.976023976}]
|
||||
# Explicit fractional rate: target_fps votes on rounded fps (24), so :.6g
|
||||
# formatting is only reachable via the explicit override.
|
||||
cmd = build_ffmpeg_cmd(clips, probes, "out.mp4", fps=23.976023976)
|
||||
joined = " ".join(str(c) for c in cmd)
|
||||
# :.6g keeps six significant digits — a valid ffmpeg fps value
|
||||
assert "fps=23.976" in joined
|
||||
assert "fps=23.976023976" not in joined
|
||||
assert "fps=24" not in joined
|
||||
|
||||
Reference in New Issue
Block a user