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:
1 parent
8ba3f25a2c
commit
0cf79eca1c
4 files changed
+416
-22
No files matched your search
@@ -339,6 +339,11 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
UInt64 scaledDuration = (UInt64)segInfo->Duration / SCALE_FACTOR;
|
||||
[newTrack setDuration:scaledDuration];
|
||||
|
||||
// Chapter Start/End are absolute *nanoseconds* regardless of the
|
||||
// segment's TimecodeScale — libmatroska writes and reads them that
|
||||
// way (verified against mkvmerge-written files read back by both
|
||||
// mkvinfo and ffprobe), so ns -> ms is a plain /1e6. The parser's
|
||||
// Seg.Duration, by contrast, is already ns-scaled.
|
||||
for (unsigned int xi = 0; xi < chapters->nChildren; xi++) {
|
||||
uint64_t timestamp = (chapters->Children[xi].Start) / SCALE_FACTOR;
|
||||
if (!xi) {
|
||||
|
||||
Reference in new issue
Block a user