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
+26
-3
@@ -26,11 +26,25 @@
|
||||
// (merged by timestamp); pathologically long clusters, emptied clusters
|
||||
// and blocks outside every cluster's int16 range get their own
|
||||
// synthesized Cluster elements instead — no original cluster is split.
|
||||
// Chapters: by default the source Chapters element is copied verbatim.
|
||||
// Setting replacementChapters regenerates it from the model track —
|
||||
// EditionEntry{ChapterAtom{ChapterTimeStart/End, ChapterDisplay{
|
||||
// ChapterString}}} — with times converted from the model's milliseconds
|
||||
// to absolute nanoseconds (chapter timecodes are ns in the wild, not
|
||||
// TimecodeScale ticks), sorted ascending (ties clamped to previous + 1
|
||||
// ms, as mkvmerge requires), each atom's end being the next atom's start
|
||||
// and the last atom ending at the track duration when known.
|
||||
// A replacement track with zero chapters removes the element (and its
|
||||
// SeekHead entry) entirely. Chapters live before the Clusters, so any
|
||||
// size change shifts every following byte — full-rebuild territory, which
|
||||
// is why chapter writing lives here and not in MP42MkvTagWriter.
|
||||
//
|
||||
// Guarantees / edges:
|
||||
// - Blocks, CodecPrivate data, Chapters and Info (including Duration)
|
||||
// are copied byte-for-byte; a Duration may overstate the longest
|
||||
// dropped track, which players tolerate.
|
||||
// - Blocks, CodecPrivate data and Info are copied byte-for-byte, except
|
||||
// that an existing Info Duration is recomputed to the end of the last
|
||||
// written block (plus 1 ms) whenever the pass measured one, so a
|
||||
// removal no longer leaves an overstated duration behind. Files whose
|
||||
// Info never had a Duration keep having none.
|
||||
// - Dangling references to removed tracks (DefaultTrackUID,
|
||||
// ChapterTrackUID) are preserved; readers ignore unknown UIDs.
|
||||
// - In-place writes go through a temp file in the destination directory
|
||||
@@ -51,6 +65,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class MP42Metadata;
|
||||
@class MP42Track;
|
||||
@class MP42ChapterTrack;
|
||||
|
||||
@interface MP42MkvRemuxer : NSObject
|
||||
|
||||
@@ -65,6 +80,14 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
// Called on the remuxing thread with a 0...1 estimate; may be nil.
|
||||
@property (nonatomic, copy, nullable) void (^progressHandler)(double progress);
|
||||
|
||||
// nil (default): the source Chapters element is copied verbatim. Non-nil:
|
||||
// the Chapters element is rebuilt from this track's chapters (see the
|
||||
// header notes); a track with no chapters removes the element. Timestamps
|
||||
// are model milliseconds (timescale 1000); they are written as absolute
|
||||
// nanoseconds (x1e6) — Matroska chapter timecodes are ns regardless of
|
||||
// the segment's TimecodeScale, as mkvtoolnix and ffmpeg both read them.
|
||||
@property (nonatomic, strong, nullable) MP42ChapterTrack *replacementChapters;
|
||||
|
||||
- (instancetype)initWithSourceURL:(NSURL *)sourceURL;
|
||||
|
||||
// sourceTracks must be the tracks exactly as the importer produced them
|
||||
|
||||
Reference in new issue
Block a user