Rebuild Matroska chapters on remux and recompute Info Duration

MP42MkvRemuxer gains replacementChapters: when set, the Chapters element
is rebuilt from the model track instead of copied verbatim (chapterCount
0 removes the element and its SeekHead entry). Chapter timecodes are
written as absolute nanoseconds — libmatroska/mkvtoolnix/ffmpeg all
treat them as ns regardless of TimecodeScale, confirmed by hex-dumping
a mkvmerge-written file and reading it back with mkvinfo and ffprobe.
ChapterAtom is 0xB6 (0xB0 is PixelWidth), and mandatory EditionUID/
ChapterUID are derived deterministically from the content because
ffmpeg silently skips atoms without a ChapterUID. Starts are sorted
ascending with ties clamped to previous+1ms (mkvmerge rejects
non-ascending), empty titles get the importer's "Chapter N" fallback,
and the last atom's end rides the track duration only when known.

An existing Info Duration is recomputed as the greatest written block
start+1ms: the Info payload is rewritten with a fixed 8-byte double
slot patched after the clusters stream past, converted to TimecodeScale
units. Added blocks measure start+1 like source blocks so a second
no-op remux stays byte-identical (including BlockDuration broke
idempotence). Files without a source Duration keep having none.

The importer's chapter /1e6 needed no change (Start is already ns); a
comment now records that so it doesn't get "fixed" to scale-based math.

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
Scott GarrettandClaude Code committed 2026-11-26 12:54:43 -05:00
1 parent 8ba3f25a2c
commit 0cf79eca1c
4 files changed
+416 -22

No files matched your search

+24
View File
@@ -59,6 +59,30 @@ enum {
MP42MkvElementBlockDuration = 0x9B, // unsigned integer, BlockGroup child: display duration
// in TrackTimecodeScale units (default scale = 1 ms)
// Info children the remuxer recomputes; both are in TimecodeScale units
// (Duration is the only EBML *float* the remuxer touches — 8-byte double).
MP42MkvElementTimecodeScale = 0x2AD7B2, // unsigned integer, default 1000000 (1 ms in ns)
MP42MkvElementDuration = 0x4489, // float, TimecodeScale units
// Chapters children (MP42MkvElementChapters). DisplayUnitID (0x76) and
// ChapterLanguage (0x437C, default "eng") are omitted on write.
MP42MkvElementEditionEntry = 0x45B9,
MP42MkvElementEditionUID = 0x45BC, // unsigned integer, mandatory (1 edition 1 UID)
MP42MkvElementChapterUID = 0x73C4, // unsigned integer, mandatory — ffmpeg
// silently skips atoms without it
// ChapterAtom is 0xB6, NOT 0xB0 — 0xB0 is PixelWidth (TrackEntry scope;
// hex-dumping mkvmerge-written Chapters confirms 0xB6, and the vendored
// MatroskaParser.c has always parsed 0xB6 here).
MP42MkvElementChapterAtom = 0xB6,
// Chapter timecodes are absolute NANOSECONDS — unlike almost every
// other Matroska time, they do NOT follow TimecodeScale (libmatroska
// and ffmpeg both write/read them as ns; confirmed against a
// mkvmerge-written file).
MP42MkvElementChapterTimeStart = 0x91, // unsigned integer, ns
MP42MkvElementChapterTimeEnd = 0x92, // unsigned integer, ns; omit when unknown
MP42MkvElementChapterDisplay = 0x80,
MP42MkvElementChapterString = 0x85, // UTF-8 string
MP42MkvElementAttachments = 0x1941A469,
MP42MkvElementAttachedFile = 0x61A7,
MP42MkvElementFileDescription = 0x467E,