From 2f5e3dce12a5e4c22bfe64ee07453bf7d086141c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 19:58:09 -0800 Subject: [PATCH 1/2] fix(demo): retime bbb.mp4's repeated timestamps and check they strictly increase Co-Authored-By: Claude Opus 5.5 --- demo/pub/bbb.py | 112 ++++++++++++++++++++++++++++++++++++++++++---- demo/pub/justfile | 7 ++- 2 files changed, 109 insertions(+), 10 deletions(-) diff --git a/demo/pub/bbb.py b/demo/pub/bbb.py index 2a489fb4e06..efdf9f03658 100644 --- a/demo/pub/bbb.py +++ b/demo/pub/bbb.py @@ -1,7 +1,8 @@ -"""Encode and verify the demo's pre-encoded SD rendition.""" +"""Retime the demo's source, then encode and verify its pre-encoded SD rendition.""" import argparse import json +import os import subprocess from fractions import Fraction from pathlib import Path @@ -17,7 +18,7 @@ def probe(path): "-show_streams", "-show_packets", "-show_entries", - "stream=index,codec_type,codec_name,width,height,time_base,duration_ts:packet=stream_index,pts,duration,flags", + "stream=index,codec_type,codec_name,width,height,time_base,duration_ts:packet=stream_index,pts,dts,duration,flags", "-of", "json", str(path), @@ -26,11 +27,102 @@ def probe(path): ) +def packets(media, stream): + return [p for p in media["packets"] if p["stream_index"] == stream["index"]] + + def video(media): stream = next(s for s in media["streams"] if s["codec_type"] == "video") - # Fragmented BBB contains duplicate packets marked discard, not visible frames. - packets = [p for p in media["packets"] if p["stream_index"] == stream["index"] and "D" not in p["flags"]] - return stream, packets + return stream, packets(media, stream) + + +def strict(path, media): + # A muxer nudges a repeated timestamp by a tick, onto a clock too coarse for another muxer + # to tell apart, and a demuxer marks the repeat discard. + for stream in media["streams"]: + stream_packets = packets(media, stream) + assert all("D" not in p["flags"] for p in stream_packets), f"{path} stream {stream['index']} discards packets" + assert all(a["pts"] < b["pts"] for a, b in zip(stream_packets, stream_packets[1:])), ( + f"{path} stream {stream['index']} timestamps do not strictly increase" + ) + + +def retime(source, target): + """Give each packet that repeats its predecessor's timestamp a slot of its own. + + The first of the pair takes the frame slot missing just before it, if there is one; + otherwise the second moves halfway to the next packet, or to its stream's end. + """ + media = probe(source) + moves = {} + for stream in media["streams"]: + stream_packets = packets(media, stream) + assert all(p["pts"] == p["dts"] for p in stream_packets), "Retiming assumes no reordering" + pts = [p["pts"] for p in stream_packets] + moved = moves[stream["index"]] = {} + for index in range(1, len(pts)): + if pts[index] != pts[index - 1]: + continue + duration = stream_packets[index - 1]["duration"] + if index >= 2 and pts[index - 1] - pts[index - 2] == 2 * duration and index - 1 not in moved: + moved[index - 1] = (pts[index - 1] - duration, duration) + else: + end = pts[index + 1] if index + 1 < len(pts) else pts[index] + stream_packets[index]["duration"] + assert end - pts[index] >= 2, "No room to retime a repeated timestamp" + middle = pts[index] + (end - pts[index]) // 2 + moved[index] = (middle, end - middle) + for index, (value, _) in moved.items(): + print(f"stream {stream['index']} packet {index}: {pts[index]} -> {value} ({stream['time_base']})") + if not any(moves.values()): + print("Timestamps already strictly increase") + return + + filters = [] + for index, moved in moves.items(): + ts, duration = "PTS", "DURATION" + for packet, (value, length) in sorted(moved.items(), reverse=True): + ts = f"if(eq(N\\,{packet})\\,{value}\\,{ts})" + duration = f"if(eq(N\\,{packet})\\,{length}\\,{duration})" + filters += [f"-bsf:{index}", f"setts=ts={ts}:duration={duration}"] + partial = target.with_suffix(".tmp.mp4") + subprocess.run( + [ + "ffmpeg", + "-hide_banner", + "-loglevel", + "warning", + "-nostdin", + "-y", + "-copyts", + "-i", + str(source), + "-map", + "0", + "-c", + "copy", + *filters, + "-f", + "mp4", + "-movflags", + "cmaf+separate_moof+delay_moov+skip_trailer", + "-frag_duration", + "1000", + str(partial), + ], + check=True, + ) + retimed = probe(partial) + strict(partial, retimed) + for stream in media["streams"]: + before = [p["pts"] for p in packets(media, stream)] + after = [p["pts"] for p in packets(retimed, stream)] + changed = {index: b for index, (a, b) in enumerate(zip(before, after)) if a != b} + expected = {index: value for index, (value, _) in moves[stream["index"]].items()} + assert len(before) == len(after) and changed == expected, "Retiming moved other packets" + assert [s["duration_ts"] for s in retimed["streams"]] == [s["duration_ts"] for s in media["streams"]], ( + "Retiming changed a stream's duration" + ) + os.replace(partial, target) def check(source, target): @@ -40,8 +132,8 @@ def check(source, target): target_stream, target_packets = video(target_media) assert len(target_media["streams"]) == 1, "SD must be video only" assert (target_stream["codec_name"], target_stream["width"], target_stream["height"]) == ("h264", 640, 360) - for stream, packets in [(source_stream, source_packets), (target_stream, target_packets)]: - assert all(a["pts"] < b["pts"] for a, b in zip(packets, packets[1:])), "Non-increasing frame timestamps" + strict(source, source_media) + strict(target, target_media) source_frames = [(p["pts"] * Fraction(source_stream["time_base"]), "K" in p["flags"]) for p in source_packets] target_frames = [(p["pts"] * Fraction(target_stream["time_base"]), "K" in p["flags"]) for p in target_packets] assert source_frames == target_frames, "SD frame timestamps/keyframes differ from source" @@ -155,11 +247,13 @@ def encode(source, target): if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("action", choices=["encode", "check"]) + parser.add_argument("action", choices=["retime", "encode", "check"]) args = parser.parse_args() source = Path("media/bbb.mp4") target = Path("media/bbb-sd.mp4") - if args.action == "encode": + if args.action == "retime": + retime(source, source) + elif args.action == "encode": encode(source, target) else: check(source, target) diff --git a/demo/pub/justfile b/demo/pub/justfile index b601e9714a5..94a7fcd312d 100644 --- a/demo/pub/justfile +++ b/demo/pub/justfile @@ -42,12 +42,17 @@ bbb url='http://localhost:4443' *args: -map 0 -map 1:v -c copy -f mpegts -pes_payload_size 0 -muxdelay 0 - \ | cargo run --bin moq -- "${@:2}" --connect "{{ url }}" --broadcast bbb.hang import ts +# Retime the source's repeated timestamps in place, before encoding the SD rendition from it. +retime-bbb: + just download bbb + python3 bbb.py retime + # Encode the video-only SD rendition, preserving source timestamps and loop duration. encode-bbb-sd: just download bbb python3 bbb.py encode -# Verify the hosted rendition's frames, keyframes, and three complete loops. +# Verify the hosted renditions' strictly increasing timestamps, frames, keyframes, and three complete loops. check-bbb: just download bbb just download bbb-sd From 9dcf9e019b89456ce8c19a7cdab06ffbd8079143 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 21:15:14 -0800 Subject: [PATCH 2/2] fix(demo): remove a rejected retime so pub sync cannot upload it Co-Authored-By: Claude Opus 5.5 --- demo/pub/bbb.py | 81 ++++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/demo/pub/bbb.py b/demo/pub/bbb.py index efdf9f03658..a4b32bce830 100644 --- a/demo/pub/bbb.py +++ b/demo/pub/bbb.py @@ -85,44 +85,49 @@ def retime(source, target): duration = f"if(eq(N\\,{packet})\\,{length}\\,{duration})" filters += [f"-bsf:{index}", f"setts=ts={ts}:duration={duration}"] partial = target.with_suffix(".tmp.mp4") - subprocess.run( - [ - "ffmpeg", - "-hide_banner", - "-loglevel", - "warning", - "-nostdin", - "-y", - "-copyts", - "-i", - str(source), - "-map", - "0", - "-c", - "copy", - *filters, - "-f", - "mp4", - "-movflags", - "cmaf+separate_moof+delay_moov+skip_trailer", - "-frag_duration", - "1000", - str(partial), - ], - check=True, - ) - retimed = probe(partial) - strict(partial, retimed) - for stream in media["streams"]: - before = [p["pts"] for p in packets(media, stream)] - after = [p["pts"] for p in packets(retimed, stream)] - changed = {index: b for index, (a, b) in enumerate(zip(before, after)) if a != b} - expected = {index: value for index, (value, _) in moves[stream["index"]].items()} - assert len(before) == len(after) and changed == expected, "Retiming moved other packets" - assert [s["duration_ts"] for s in retimed["streams"]] == [s["duration_ts"] for s in media["streams"]], ( - "Retiming changed a stream's duration" - ) - os.replace(partial, target) + # `just pub sync` uploads all of media/, so a rejected remux must not stay there. + try: + subprocess.run( + [ + "ffmpeg", + "-hide_banner", + "-loglevel", + "warning", + "-nostdin", + "-y", + "-copyts", + "-i", + str(source), + "-map", + "0", + "-c", + "copy", + *filters, + "-f", + "mp4", + "-movflags", + "cmaf+separate_moof+delay_moov+skip_trailer", + "-frag_duration", + "1000", + str(partial), + ], + check=True, + ) + retimed = probe(partial) + strict(partial, retimed) + for stream in media["streams"]: + before = [p["pts"] for p in packets(media, stream)] + after = [p["pts"] for p in packets(retimed, stream)] + changed = {index: b for index, (a, b) in enumerate(zip(before, after)) if a != b} + expected = {index: value for index, (value, _) in moves[stream["index"]].items()} + assert len(before) == len(after) and changed == expected, "Retiming moved other packets" + assert [s["duration_ts"] for s in retimed["streams"]] == [s["duration_ts"] for s in media["streams"]], ( + "Retiming changed a stream's duration" + ) + os.replace(partial, target) + except BaseException: + partial.unlink(missing_ok=True) + raise def check(source, target):