feat(tracks): carry every audio track, subtitle and font attachment
MP4 (default) keeps the 'opens anywhere' promise: all audio tracks become AAC at the source's own channel layout (5.1 stays 5.1), text subtitles become mov_text, bitmap subtitles are skipped with a logged note, font attachments are not carried. container=mkv is the explicit fidelity mode: every audio/subtitle/attachment stream is copied bit-for-bit, chapters and tags ride along. New scripts/tracks.py holds the pure plan + the container-aware gate; library_run.py uses it for mapping, per-track codecs, container extension, install-path names and the hvc1 check (Matroska has no codec tag). Capability report gains container/audio_downmix/ subs and 'containers' (contract 3, version 1.2.0). Verified: scripts/test_tracks.py 55/55 (both containers end to end, per-packet md5), plus two real library_run.py runs (MP4 3x AAC incl. 6ch + 2x mov_text; MKV bit-exact).
This commit is contained in:
1 parent
72a8971d3f
commit
f088ae78e3
3 files changed
+868
-30
No files matched your search
@@ -0,0 +1,258 @@
|
||||
#!/usr/bin/env python3
|
||||
"""tracks.py gate: the carry-over plan, its ffmpeg arguments and its verdicts.
|
||||
|
||||
Two layers, deliberately separated:
|
||||
|
||||
* plan/args/verify arithmetic runs against SYNTHETIC ffprobe JSON - every branch is
|
||||
reachable on any machine, and a change in policy shows up as a failed expectation
|
||||
instead of as a surprising file.
|
||||
* one end-to-end pair (MP4 + Matroska) runs only when the fixture from the 2026-10-24
|
||||
research exists (`<control-plane>/cache/research/fixture/src_full.mkv`), because it
|
||||
needs a real source with 3 audio tracks, 2 subtitles and 2 font attachments.
|
||||
|
||||
Run: python scripts/test_tracks.py
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(0, HERE)
|
||||
import tracks as TR # noqa: E402
|
||||
|
||||
checks = {}
|
||||
|
||||
|
||||
def check(name, ok, detail=""):
|
||||
checks[name] = bool(ok)
|
||||
print("%-58s %s%s" % (name, "PASS" if ok else "FAIL", (" " + str(detail)) if detail and not ok else ""))
|
||||
|
||||
|
||||
def field(typ, index, codec, **kw):
|
||||
d = {"index": index, "codec_type": typ, "codec_name": codec,
|
||||
"tags": kw.pop("tags", {}), "disposition": kw.pop("disp", {})}
|
||||
d.update(kw)
|
||||
return d
|
||||
|
||||
|
||||
def fixture_json(n_audio=None):
|
||||
"""A synthetic version of the research fixture: 3 audio, 2 subtitles, 2 attachments."""
|
||||
streams = [
|
||||
field("video", 0, "h264", width=640, height=360, pix_fmt="yuv420p", codec_tag_string="avc1"),
|
||||
field("audio", 1, "aac", channels=2, bit_rate="128000",
|
||||
tags={"language": "eng", "title": "English Original"}, disp={"default": 1}),
|
||||
field("audio", 2, "ac3", channels=6, bit_rate="384000",
|
||||
tags={"language": "jpn", "title": "日本語 コメンタリー"}),
|
||||
field("audio", 3, "flac", channels=2, bit_rate="700000",
|
||||
tags={"language": "zho", "title": "中文配音"}),
|
||||
field("subtitle", 4, "ass", tags={"language": "eng", "title": "English ASS"},
|
||||
disp={"default": 1}),
|
||||
field("subtitle", 5, "subrip", tags={"language": "zho", "title": "中文字幕"},
|
||||
disp={"forced": 1}),
|
||||
field("attachment", 6, "ttf", tags={"filename": "arial.ttf",
|
||||
"mimetype": "application/x-truetype-font"}),
|
||||
field("attachment", 7, "ttf", tags={"filename": "msyh.ttc",
|
||||
"mimetype": "application/x-truetype-font"}),
|
||||
]
|
||||
if n_audio is not None:
|
||||
streams = [s for s in streams if s["codec_type"] != "audio"]
|
||||
streams += [field("audio", 10 + i, "aac", channels=2, bit_rate="128000",
|
||||
tags={"language": "eng"}) for i in range(n_audio)]
|
||||
return {
|
||||
"streams": streams,
|
||||
"format": {"duration": "10.037", "size": "23783193",
|
||||
"tags": {"title": "Carry-over Source", "artist": "Hoelee",
|
||||
"CUSTOM_KEY": "custom value 42",
|
||||
"creation_time": "2021-07-03T10:00:00.000000Z"}},
|
||||
"chapters": [{"start_time": "0", "end_time": "3", "tags": {"title": "Intro"}},
|
||||
{"start_time": "3", "end_time": "7", "tags": {"title": "Mid"}},
|
||||
{"start_time": "7", "end_time": "10", "tags": {"title": "End"}}],
|
||||
}
|
||||
|
||||
|
||||
JS = fixture_json()
|
||||
|
||||
# ---------------------------------------------------------------- MP4 policy
|
||||
p_mp4 = TR.plan(JS, "mp4")
|
||||
check("mp4: all 3 audio tracks are planned", len(p_mp4["audio"]) == 3, p_mp4["audio"])
|
||||
check("mp4: AAC source is copied", p_mp4["audio"][0]["codec"] == "copy", p_mp4["audio"][0])
|
||||
check("mp4: AC-3 becomes AAC", p_mp4["audio"][1]["codec"] == "aac")
|
||||
check("mp4: 5.1 keeps 6 channels (no silent downmix)",
|
||||
p_mp4["audio"][1]["target_channels"] == 6, p_mp4["audio"][1])
|
||||
check("mp4: AAC bitrate follows the source (384k)",
|
||||
p_mp4["audio"][1]["kbps"] == 384, p_mp4["audio"][1])
|
||||
check("mp4: FLAC becomes AAC", p_mp4["audio"][2]["codec"] == "aac")
|
||||
check("mp4: both text subtitles become mov_text",
|
||||
[s["codec"] for s in p_mp4["subtitles"]] == ["mov_text", "mov_text"], p_mp4["subtitles"])
|
||||
check("mp4: font attachments are NOT carried", p_mp4["attachments_carried"] is False)
|
||||
check("mp4: the font loss is warned about",
|
||||
any("attachment" in w for w in p_mp4["warnings"]), p_mp4["warnings"])
|
||||
check("mp4: chapters are counted", p_mp4["chapters"] == 3)
|
||||
check("mp4: per-track titles survive the plan",
|
||||
[a["title"] for a in p_mp4["audio"]] == ["English Original", "日本語 コメンタリー", "中文配音"])
|
||||
|
||||
args = TR.ffmpeg_args(p_mp4)
|
||||
check("args: video is mapped once", args[0:2] == ["-map", "0:v:0"])
|
||||
check("args: all three audio tracks are mapped",
|
||||
[i for i in range(len(args) - 1) if args[i] == "-map" and args[i + 1].startswith("0:a:")]
|
||||
and args.count("0:a:0") == 1 and "0:a:2" in args, args)
|
||||
check("args: never `-map 0` (chapter/text tracks must stay out)", "-map" in args and "0" not in
|
||||
[args[i + 1] for i in range(len(args) - 1) if args[i] == "-map"])
|
||||
check("args: per-track AAC channels are explicit", "-ac:a:1" in args and "6" in args)
|
||||
check("args: track labels are written twice for mp4 (title + handler_name)",
|
||||
"title=English Original" in args and "handler_name=English Original" in args, args)
|
||||
check("args: mp4 subtitle codec is mov_text", "mov_text" in args)
|
||||
check("args: no attachments map for mp4", "0:t" not in args)
|
||||
mux = TR.ffmpeg_mux_args(p_mp4)
|
||||
check("mux: faststart + metadata tags", "+faststart+use_metadata_tags" in mux, mux)
|
||||
check("video tag: hvc1 for mp4", TR.video_tag_args(p_mp4) == ["-tag:v", "hvc1"])
|
||||
|
||||
# ---------------------------------------------------------------- MKV (fidelity)
|
||||
p_mkv = TR.plan(JS, "mkv")
|
||||
check("mkv: every audio track is COPIED",
|
||||
[a["codec"] for a in p_mkv["audio"]] == ["copy", "copy", "copy"], p_mkv["audio"])
|
||||
check("mkv: every subtitle is COPIED",
|
||||
[s["codec"] for s in p_mkv["subtitles"]] == ["copy", "copy"], p_mkv["subtitles"])
|
||||
check("mkv: attachments are carried", p_mkv["attachments_carried"] is True)
|
||||
check("mkv: no lossy warnings", not p_mkv["warnings"], p_mkv["warnings"])
|
||||
margs = TR.ffmpeg_args(p_mkv)
|
||||
check("mkv: attachment map is present", "0:t" in margs)
|
||||
check("mkv: no mov_text", "mov_text" not in margs)
|
||||
check("mkv: no faststart / metadata-whitelist flags", TR.ffmpeg_mux_args(p_mkv) == [])
|
||||
check("mkv: no Apple codec tag", TR.video_tag_args(p_mkv) == [])
|
||||
|
||||
# ---------------------------------------------------------------- downmix opt-out
|
||||
p_dm = TR.plan(JS, "mp4", {"downmix_51": True})
|
||||
check("downmix flag: 5.1 track becomes 2 channels",
|
||||
p_dm["audio"][1]["target_channels"] == 2, p_dm["audio"][1])
|
||||
check("downmix flag: the downmix is announced",
|
||||
any("downmixed" in w for w in p_dm["warnings"]), p_dm["warnings"])
|
||||
|
||||
# ---------------------------------------------------------------- bitmap subtitles
|
||||
JS_BM = fixture_json()
|
||||
JS_BM["streams"].append(field("subtitle", 8, "hdmv_pgs_subtitle", tags={"language": "eng"}))
|
||||
p_bm_mp4 = TR.plan(JS_BM, "mp4")
|
||||
check("PGS: skipped in mp4 (it cannot be carried at all)",
|
||||
len(p_bm_mp4["subtitles"]) == 2 and len(p_bm_mp4["subs_skipped"]) == 1,
|
||||
(p_bm_mp4["subtitles"], p_bm_mp4["subs_skipped"]))
|
||||
check("PGS: the skip names the reason", "graphic" in p_bm_mp4["subs_skipped"][0]["reason"])
|
||||
p_bm_mkv = TR.plan(JS_BM, "mkv")
|
||||
check("PGS: copied in matroska", len(p_bm_mkv["subtitles"]) == 3, p_bm_mkv["subtitles"])
|
||||
p_bm_txt = TR.plan(JS_BM, "mkv", {"subs": "text_only"})
|
||||
check("subs=text_only: PGS skipped even in matroska",
|
||||
len(p_bm_txt["subtitles"]) == 2 and len(p_bm_txt["subs_skipped"]) == 1)
|
||||
|
||||
# ---------------------------------------------------------------- other source shapes
|
||||
JS_MUTE = fixture_json()
|
||||
JS_MUTE["streams"] = [s for s in JS_MUTE["streams"] if s["codec_type"] != "audio"]
|
||||
p_mute = TR.plan(JS_MUTE, "mp4")
|
||||
check("no audio: -an is emitted", "-an" in TR.ffmpeg_args(p_mute))
|
||||
JS_NOSUB = fixture_json()
|
||||
JS_NOSUB["streams"] = [s for s in JS_NOSUB["streams"] if s["codec_type"] != "subtitle"]
|
||||
p_nosub = TR.plan(JS_NOSUB, "mp4")
|
||||
check("no subtitles: -sn is emitted", "-sn" in TR.ffmpeg_args(p_nosub))
|
||||
check("audio-only-in-MP4 policy refuses `copy`", TR.plan(JS, "mp4", {"audio": "copy"})["audio"][1]["codec"] == "aac")
|
||||
|
||||
# ---------------------------------------------------------------- paths
|
||||
check("dst: mp4 source stays in place under mp4 policy",
|
||||
TR.out_path_for("D:/v/a.mp4", "mp4") == "D:/v/a.mp4")
|
||||
check("dst: mp4 source becomes .mkv under the fidelity policy",
|
||||
TR.out_path_for("D:/v/a.mp4", "mkv") == "D:/v/a.mkv")
|
||||
check("dst: mkv stays in place under mkv", TR.out_path_for("D:/v/a.mkv", "mkv") == "D:/v/a.mkv")
|
||||
|
||||
# ---------------------------------------------------------------- verify verdicts
|
||||
def out_json_of(plan_, **over):
|
||||
streams = [field("video", 0, "hevc", width=640, height=360, pix_fmt="yuv420p",
|
||||
codec_tag_string="hvc1" if plan_["container"] == "mp4" else "[0][0][0][0]")]
|
||||
for n, a in enumerate(plan_["audio"]):
|
||||
streams.append(field("audio", 1 + n, "aac" if a["codec"] == "aac" else a["src_codec"],
|
||||
channels=a["target_channels"],
|
||||
tags={"language": a["language"], "title": a["title"]}))
|
||||
for n, s in enumerate(plan_["subtitles"]):
|
||||
streams.append(field("subtitle", 10 + n,
|
||||
"mov_text" if s["codec"] == "mov_text" else s["src_codec"],
|
||||
tags={"language": s["language"], "title": s["title"]}))
|
||||
for n, a in enumerate(plan_["attachments"]):
|
||||
streams.append(field("attachment", 20 + n, "ttf",
|
||||
tags={"filename": a["filename"], "mimetype": a["mimetype"]}))
|
||||
js = {"streams": streams,
|
||||
"format": {"duration": "10.037", "tags": dict(JS["format"]["tags"])},
|
||||
"chapters": list(JS["chapters"])}
|
||||
js.update(over)
|
||||
return js
|
||||
|
||||
|
||||
ok, msg = TR.verify(p_mp4, JS, out_json_of(p_mp4))
|
||||
check("verify: a faithful mp4 plan passes", ok, msg)
|
||||
ok, msg = TR.verify(p_mkv, JS, out_json_of(p_mkv))
|
||||
check("verify: a faithful mkv plan passes", ok, msg)
|
||||
|
||||
bad = out_json_of(p_mp4)
|
||||
bad["streams"] = [s for s in bad["streams"] if not (s["codec_type"] == "audio" and s["index"] == 3)]
|
||||
ok, msg = TR.verify(p_mp4, JS, bad)
|
||||
check("verify: a dropped audio track is caught", not ok and "audio tracks" in msg, msg)
|
||||
|
||||
bad = out_json_of(p_mp4)
|
||||
for s in bad["streams"]:
|
||||
if s["codec_type"] == "video":
|
||||
s["codec_tag_string"] = "hev1"
|
||||
ok, msg = TR.verify(p_mp4, JS, bad)
|
||||
check("verify: hev1 (Apple refuses it) is caught", not ok and "hvc1" in msg, msg)
|
||||
|
||||
bad = out_json_of(p_mkv)
|
||||
bad["format"]["duration"] = "20.0"
|
||||
ok, msg = TR.verify(p_mkv, JS, bad)
|
||||
check("verify: a duration change is caught", not ok and "duration" in msg, msg)
|
||||
|
||||
bad = out_json_of(p_mkv)
|
||||
bad["chapters"] = []
|
||||
ok, msg = TR.verify(p_mkv, JS, bad)
|
||||
check("verify: lost chapters are caught", not ok and "chapters" in msg, msg)
|
||||
|
||||
bad = out_json_of(p_mkv)
|
||||
bad["format"]["tags"].pop("creation_time")
|
||||
ok, msg = TR.verify(p_mkv, JS, bad)
|
||||
check("verify: a lost creation_time is caught", not ok and "creation_time" in msg, msg)
|
||||
|
||||
bad = out_json_of(p_mp4)
|
||||
bad["streams"] = [s for s in bad["streams"] if s["codec_type"] != "subtitle"]
|
||||
ok, msg = TR.verify(p_mp4, JS, bad)
|
||||
check("verify: silently dropped subtitles are caught", not ok and "subtitle" in msg, msg)
|
||||
|
||||
bad = out_json_of(p_mkv)
|
||||
bad["streams"] = [s for s in bad["streams"] if s["codec_type"] != "attachment"]
|
||||
ok, msg = TR.verify(p_mkv, JS, bad)
|
||||
check("verify: lost font attachments are caught", not ok and "attachment" in msg, msg)
|
||||
|
||||
# ---------------------------------------------------------------- end-to-end (fixture only)
|
||||
FIXTURE = os.path.join(os.path.dirname(HERE), "..", "hevc-transcode", "cache", "research",
|
||||
"fixture", "src_full.mkv")
|
||||
FIXTURE = os.path.normpath(FIXTURE)
|
||||
if os.path.exists(FIXTURE) and subprocess.run(["ffmpeg", "-version"], capture_output=True).returncode == 0:
|
||||
tmp = tempfile.mkdtemp(prefix="tracks_e2e_")
|
||||
src = os.path.join(tmp, "src.mkv")
|
||||
subprocess.run(["ffmpeg", "-y", "-v", "error", "-i", FIXTURE, "-map", "0", "-c", "copy", src],
|
||||
check=True, capture_output=True)
|
||||
js = TR.probe(src)
|
||||
for container in ("mkv", "mp4"):
|
||||
out = os.path.join(tmp, "out" + TR.suffix_for(container))
|
||||
pl = TR.plan(js, container)
|
||||
cmd = (["ffmpeg", "-y", "-v", "error", "-i", src] + TR.ffmpeg_args(pl)
|
||||
+ ["-c:v", "copy"] + TR.video_tag_args(pl, "h264") + TR.ffmpeg_mux_args(pl) + [out])
|
||||
r = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", errors="replace")
|
||||
check("e2e %s: ffmpeg accepted the plan" % container, r.returncode == 0,
|
||||
(r.stderr or "").strip()[-300:])
|
||||
if r.returncode == 0:
|
||||
ojs = TR.probe(out)
|
||||
ok, msg = TR.verify(pl, js, ojs, "h264")
|
||||
check("e2e %s: plan verified against the produced file" % container, ok, msg)
|
||||
same, smsg = TR.copy_streams_intact(pl, src, out)
|
||||
check("e2e %s: copied streams are byte-identical" % container, same, smsg)
|
||||
print("\n(e2e fixture used: %s)" % FIXTURE)
|
||||
else:
|
||||
print("\n(e2e skipped: fixture or ffmpeg not available)")
|
||||
|
||||
print("\n%d/%d checks passed" % (sum(checks.values()), len(checks)))
|
||||
sys.exit(0 if all(checks.values()) else 1)
|
||||
Reference in new issue
Block a user