From 10dacd840a24af196bbfb2a2f23b8faceab76d06 Mon Sep 17 00:00:00 2001 From: Benjiyaya Date: Sat, 29 Aug 2026 01:03:25 +0800 Subject: [PATCH] feat: parse clip fps and vote export target rate --- .../src/calliope/export/runner.py | 39 ++++++++++++++++++- calliope-backend/tests/test_export.py | 29 +++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/calliope-backend/src/calliope/export/runner.py b/calliope-backend/src/calliope/export/runner.py index 0c941649..2f3f8f26 100644 --- a/calliope-backend/src/calliope/export/runner.py +++ b/calliope-backend/src/calliope/export/runner.py @@ -22,12 +22,18 @@ from calliope.db import get_db, row_to_dict logger = logging.getLogger("calliope.export") # Normalization targets — every clip is conformed to these before stitching. +# The fps target is the clips' own majority fps (see target_fps), not a fixed +# rate: 24 fps generated clips must export at 24 fps, not be retimed to 30. EXPORT_WIDTH = 1920 EXPORT_HEIGHT = 1080 -EXPORT_FPS = 30 +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] = {} @@ -55,6 +61,37 @@ def slugify(title: str) -> str: return slug or "film" +def parse_rate(rate: str | None) -> float: + """ffprobe r_frame_rate ("24000/1001") as a float; 0.0 when unusable.""" + if not rate or "/" not in rate: + return 0.0 + num, _, den = rate.partition("/") + try: + d = float(den) + if d == 0: + return 0.0 + return float(num) / d + except ValueError: + return 0.0 + + +def target_fps(probes: list[dict[str, Any]]) -> float: + """Majority rounded fps across clips; tie → lower; none → default. + + xfade needs one common timebase, so a mixed 24/30 fps project is conformed + to whichever rate most clips already use. + """ + votes = [round(p["fps"]) for p in probes if p.get("fps")] + if not votes: + return DEFAULT_EXPORT_FPS + counts: dict[int, int] = {} + for v in votes: + counts[v] = counts.get(v, 0) + 1 + best = max(counts.values()) + winners = sorted(v for v, c in counts.items() if c == best) + return float(winners[0]) + + def collect_clips(project_id: int) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]: """Scenes in story order, split into (with video_path, without).""" conn = get_db(config.settings.db_path) diff --git a/calliope-backend/tests/test_export.py b/calliope-backend/tests/test_export.py index b87ff372..433bc03b 100644 --- a/calliope-backend/tests/test_export.py +++ b/calliope-backend/tests/test_export.py @@ -6,7 +6,7 @@ from pathlib import Path import pytest from calliope.comfyui.client import ComfyUIClient -from calliope.export.runner import build_ffmpeg_cmd, collect_clips +from calliope.export.runner import DEFAULT_EXPORT_FPS, build_ffmpeg_cmd, collect_clips, parse_rate, target_fps def _make_project_with_clips(client, title: str = "My Film") -> int: @@ -205,3 +205,30 @@ def test_cancel_comfy_job_calls_interrupt(client, monkeypatch): assert calls["kill"] == 0 finally: client.post("/api/jobs/resume") + + +def test_parse_rate(): + assert parse_rate("24/1") == 24.0 + assert abs(parse_rate("24000/1001") - 23.976023976) < 1e-6 + assert parse_rate("30000/1001") > 29.9 + assert parse_rate("0/0") == 0.0 + assert parse_rate("0/1") == 0.0 + assert parse_rate("garbage") == 0.0 + assert parse_rate(None) == 0.0 + assert parse_rate("") == 0.0 + + +def test_target_fps_majority(): + probes = [{"fps": 24.0}, {"fps": 24.0}, {"fps": 30.0}] + assert target_fps(probes) == 24.0 + + +def test_target_fps_tie_prefers_lower(): + probes = [{"fps": 30.0}, {"fps": 24.0}] + assert target_fps(probes) == 24.0 + + +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