#!/usr/bin/env python3 """Write a golden's supplied floor in the CMAF form (extensions/packaging.md §7.7) from another golden's supplied floor in the OBU form (§7.3). A CMAF floor is a ladder's own initialization segment and media segments, as a media muxer wrote them, so this golden's are written by one: each `.obu` of the source golden's floor is muxed, without re-encoding (`-c copy`), by ffmpeg's HLS muxer into fMP4 segments, cutting at every key frame (`-hls_time` of one frame), so each media segment holds one piece of the plan; its `init.mp4` and `seg_.m4s` files, `` four digits, are `floor//`. The policy's `floor.supplied_listing` is set to the SHA-256 of the floor directory's listing (SPEC §3.1), which `wai-pack listing` prints. A muxer is not a pure function of its input across versions, so the files are committed and pinned by the listing: a packer reads them and checks them against the plan, and never writes them. This script is how the committed files were written, not a step of verification. python3 supplied_cmaf.py > > """ import hashlib import os import re import shutil import subprocess import sys import tempfile def main(argv): if len(argv) != 6: print(__doc__) return 2 ffmpeg, wai_pack, src, dst, rate = argv[1:] num, den = (int(x) for x in rate.split("/")) floor_in, floor_out = os.path.join(src, "floor"), os.path.join(dst, "floor") if os.path.isdir(floor_out): shutil.rmtree(floor_out) os.makedirs(floor_out) for f in sorted(os.listdir(floor_in)): if not f.endswith(".obu"): continue name = f[:-len(".obu")] with tempfile.TemporaryDirectory() as tmp: subprocess.run([ffmpeg, "-nostdin", "-v", "error", "-f", "obu", "-framerate", f"{num}/{den}", "-i", os.path.join(floor_in, f), "-c", "copy", "-f", "hls", "-hls_time", f"{den / num:.6f}", "-hls_segment_type", "fmp4", "-hls_fmp4_init_filename", "init.mp4", "-hls_segment_filename", os.path.join(tmp, "seg_%04d.m4s"), "-hls_playlist_type", "vod", os.path.join(tmp, "index.m3u8")], check=True) out = os.path.join(floor_out, name) os.makedirs(out) for g in sorted(os.listdir(tmp)): if g == "init.mp4" or g.endswith(".m4s"): shutil.copyfile(os.path.join(tmp, g), os.path.join(out, g)) print(f"{out}: {sorted(os.listdir(out))}") listing = hashlib.sha256(subprocess.run([wai_pack, "listing", floor_out], check=True, capture_output=True).stdout).hexdigest() p = os.path.join(dst, "policy.toml") policy = open(p, encoding="utf-8").read() open(p, "w", encoding="utf-8").write(re.sub(r'supplied_listing = "[0-9a-f]{64}"', f'supplied_listing = "{listing}"', policy)) print(f"listing {listing}") return 0 if __name__ == "__main__": sys.exit(main(sys.argv))