clipwright/tests/test_reframe_v2.py
juancarlos.cavero ea78445953 feat(clips): four quality fixes found by auditing real user output
Captions must always be the LAST layer. Editing or hooking a clip that already
had them burned in produced `edited_subtitled_<...>`, and the next subtitle pass
stacked a second caption layer on top of the first — visibly doubled and
unreadable in a real user clip found in prod today. /api/edit and /api/hook now
derive from the clean file and put captions back afterwards, so a restyle always
has an un-captioned base to burn onto. The latent bug predates auto-captions but
shipping them by default made it the normal path. The walk-back is now one
helper instead of being open-coded in the subtitle endpoint.

Loudness was uncontrolled. A clip inherited whatever its source was mastered at,
measured across real delivered clips from -13.8 LUFS on a loud upload down to
-28 on a quiet talk, while TikTok/Reels/Shorts all normalise playback to around
-14 — so the quiet ones just sound thin in the feed. loudnorm at the clip cut,
where the audio is already being encoded, so it costs nothing. Verified to pull
both extremes to target: -29.7 -> -13.7 and -10.1 -> -13.5. AUDIO_NORMALIZE=0.

The GENERAL layout wasted the frame. Fitting a 16:9 source to full width left
it 608px tall in a 1920 frame: 32% content, 68% blurred filler. Now scaled by
height to 42%, trimming the overflow. Deliberately conservative — GENERAL is
chosen for groups and landscapes, exactly where cropping the sides cuts someone
out, so 0.55 was tried and rejected for throwing away 42% of the width. 0.42
keeps ~76%. GENERAL_CONTENT_HEIGHT_RATIO=0.32 restores the old behaviour.

The TRACK/GENERAL classifier only counted faces, so a screencast that happened
to contain one face was cropped to it — real prod clips came out with headlines
cut mid-word and unreadable. A cheap Canny pass over the side thirds (the part a
9:16 crop discards), with face boxes blanked first, now routes text-dense scenes
to GENERAL instead. GRAPHIC_DENSITY_THRESHOLD=1.0 disables it.
2026-07-26 20:59:04 +02:00

127 lines
4.9 KiB
Python

from reframe_v2 import (
DELIVERY_MIN_WIDTH,
concat_list_content,
dedupe_sendcmd_lines,
delivery_size,
general_filtergraph,
scene_frame_ranges,
)
VERTICAL = 9 / 16
def test_sendcmd_lines_dedupe_to_change_points():
xs = [100, 100, 100, 104, 104, 110]
lines = dedupe_sendcmd_lines(xs, fps=30.0)
assert lines == [
"0.0000 crop@c x 100;",
"0.1000 crop@c x 104;",
"0.1667 crop@c x 110;",
]
def test_sendcmd_static_camera_is_single_command():
assert len(dedupe_sendcmd_lines([250] * 300, fps=30.0)) == 1
def test_scene_ranges_clamped_and_keep_strategy():
ranges = scene_frame_ranges(
[(0, 100), (100, 250), (250, 400)],
['TRACK', 'GENERAL', 'TRACK'],
total_frames=200,
)
# Third scene starts past the decoded frame count -> dropped, and the
# GENERAL strategy stays attached to its own range.
assert ranges == [(0, 100, 'TRACK'), (100, 200, 'GENERAL')]
def test_scene_ranges_missing_strategy_defaults_to_track():
assert scene_frame_ranges([(0, 10)], [], 10) == [(0, 10, 'TRACK')]
def test_concat_list_format():
content = concat_list_content(["/tmp/a/seg_000.mp4", "/tmp/a/seg_001.mp4"])
assert content == "file '/tmp/a/seg_000.mp4'\nfile '/tmp/a/seg_001.mp4'\n"
class TestDeliverySize:
"""A 720p source must not produce a 406x720 clip (prod audit, 25-jul-2026)."""
def test_720p_source_is_upscaled_to_the_delivery_floor(self):
assert delivery_size(1280, 720, VERTICAL) == (1080, 1920)
def test_1080p_source_lands_on_the_floor_exactly(self):
assert delivery_size(1920, 1080, VERTICAL) == (1080, 1920)
def test_tiny_source_still_reaches_the_floor(self):
# 360x640 sources were shipping as-is; platforms treat that as junk.
assert delivery_size(640, 360, VERTICAL) == (1080, 1920)
def test_high_res_source_is_never_downscaled(self):
# 4K source: the native crop is already well past the floor, so keep it.
assert delivery_size(3840, 2160, VERTICAL) == (1216, 2160)
def test_already_vertical_source_keeps_its_full_height(self):
assert delivery_size(1080, 1920, VERTICAL) == (1080, 1920)
def test_square_output_uses_the_same_floor(self):
assert delivery_size(1280, 720, 1.0) == (1080, 1080)
def test_dimensions_are_always_even(self):
for w, h in [(1280, 720), (1920, 1080), (854, 480), (3840, 2160), (641, 361)]:
out_w, out_h = delivery_size(w, h, VERTICAL)
assert out_w % 2 == 0 and out_h % 2 == 0, (w, h)
def test_never_returns_below_the_floor(self):
for w, h in [(320, 180), (640, 360), (1280, 720), (1920, 1080)]:
assert delivery_size(w, h, VERTICAL)[0] >= DELIVERY_MIN_WIDTH
def test_aspect_ratio_is_preserved(self):
for w, h in [(1280, 720), (640, 360), (3840, 2160)]:
out_w, out_h = delivery_size(w, h, VERTICAL)
assert abs(out_w / out_h - VERTICAL) < 0.01, (w, h)
def test_general_filtergraph_targets_output_geometry():
graph = general_filtergraph(1080, 1920)
assert "gblur" in graph
assert "overlay=x=(W-w)/2:y=(H-h)/2" in graph
class TestGeneralLayout:
"""GENERAL used to leave the content at 32% of the frame, floating in blur."""
def test_content_is_scaled_by_height_not_width(self):
from reframe_v2 import GENERAL_CONTENT_HEIGHT_RATIO
graph = general_filtergraph(1080, 1920)
expected = int(1920 * GENERAL_CONTENT_HEIGHT_RATIO)
expected += expected % 2
assert f"scale=-2:{expected}" in graph
def test_content_height_is_even(self):
# Odd dimensions are rejected by the encoder. Read the FOREGROUND
# scale — the background line also contains a scale=-2: term.
for out_h in (1920, 1080, 1078, 976):
graph = general_filtergraph(1080, out_h)
fg = graph.split("[fga]scale=-2:")[1]
h = int(fg.split(",")[0])
assert h % 2 == 0, (out_h, h)
def test_overflow_is_trimmed_never_padded(self):
# min() keeps it a no-op for sources narrower than the frame.
graph = general_filtergraph(1080, 1920)
assert "crop=w=min(iw\\,1080):h=ih" in graph
def test_content_fills_more_than_the_old_full_width_fit(self):
# A 16:9 source fitted to full width was 9/16 of it — 0.5625 * 1080
# = 608px, i.e. 32% of a 1920 frame. The new layout must beat that.
from reframe_v2 import GENERAL_CONTENT_HEIGHT_RATIO
old_ratio = (1080 * 9 / 16) / 1920
assert GENERAL_CONTENT_HEIGHT_RATIO > old_ratio
def test_keeps_most_of_the_source_width(self):
# GENERAL is for groups and landscapes: cropping the sides too hard
# cuts people out of frame, which is the whole thing it exists to avoid.
from reframe_v2 import GENERAL_CONTENT_HEIGHT_RATIO
scaled_w = 1920 * GENERAL_CONTENT_HEIGHT_RATIO * (16 / 9)
assert 1080 / scaled_w > 0.70, "keeps less than 70% of the source width"