Files
MP42Foundation/MP42/MP42MkvRemuxer.h
T
Scott GarrettandClaude Code 1040f47542 Add Matroska tagging and track-removal support
- MP42Ebml: EBML vint/ID primitives, flat Segment scanner, block track
  number decoding, and element builders shared by all Matroska writers.
- MP42MkvImporter / MP42FileImporter: tagsOnly mode that mirrors the
  file's own track flags instead of the MP4 conversion heuristics.
- MP42MatroskaTagMapping: read/write SimpleTag maps shared by importer
  and writer, including community names for MP4-only movie fields.
- MP42MkvTagWriter: mkvpropedit-style in-place rewrite of Tags, cover
  art, and per-track name/language/enabled/forced fields with a
  size+mtime staleness guard; an untouched save is a byte-for-byte
  no-op.
- MP42MkvRemuxer: stream-copy rebuild that drops the blocks of removed
  tracks (TrackNumbers keep their gaps; block bytes are never
  rewritten), regenerates SeekHead and Cues, carries metadata edits in
  the same pass, and writes via temp file + atomic rename.
- MP42MkvTagWriter+Internal.h exposes the tag writer's scanning and
  payload builders so the remuxer reuses them unchanged.

Verified with the external matrix harness: 241 assertions green across
layout fixtures (ffmpeg/mkvmerge/streamed), removal scenarios, cue
parity, and idempotency.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-25 19:48:09 -05:00

75 lines
3.1 KiB
Objective-C

//
// MP42MkvRemuxer.h
// MP42Foundation
//
// Rebuilds a whole Matroska file, stream-copying every block verbatim
// (no decode/re-encode) except the blocks of tracks the model dropped —
// the job mkvpropedit cannot do (it corresponds to mkvmerge's copy mode
// with tracks deselected). Used when the user deletes tracks in a
// tags-only document, and for MKV Save-As to another URL.
//
// The Tracks, Tags and Attachments payloads are built with the exact
// model-diff builders MP42MkvTagWriter uses, so a remux save applies
// metadata edits, cover art and the four per-track fields in the same
// pass. SeekHead entries and (when the source had Cues) the Cues element
// are regenerated with fresh positions; no source block header is ever
// modified, so the remaining tracks' TrackNumber values may be gapped
// after a removal (valid Matroska, and what avoids any block rewrite).
//
// 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.
// - 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
// and an atomic rename; the staleness guard checks the *source* size
// and mtime as usual.
// - Refuses: multiple Segments, unknown-size elements other than the
// trailing cluster, unknown-size elements inside Tracks or clusters,
// and remuxes that would leave zero tracks.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@class MP42Metadata;
@class MP42Track;
@interface MP42MkvRemuxer : NSObject
@property (nonatomic, readonly) NSURL *sourceURL;
// When set, the remux refuses to touch a source file whose size or
// modification date no longer matches (callers snapshot these when the
// document is read). Leave unset for Save-As onto a fresh destination.
@property (nonatomic) uint64_t expectedFileSize;
@property (nonatomic, copy, nullable) NSDate *expectedModificationDate;
// Called on the remuxing thread with a 0...1 estimate; may be nil.
@property (nonatomic, copy, nullable) void (^progressHandler)(double progress);
- (instancetype)initWithSourceURL:(NSURL *)sourceURL;
// sourceTracks must be the tracks exactly as the importer produced them
// (file order, chapter pseudo-track filtered out) — TrackEntry elements
// are paired with them by position. currentTracks is the document model's
// track list after user deletions; entries whose paired sourceTracks
// object is no longer present (compared by object identity) are removed.
//
// Safe to call from another thread while -cancel may fire; the write
// aborts at the next chunk boundary and the partial temp file is
// discarded.
- (BOOL)remuxToURL:(NSURL *)destURL
metadata:(MP42Metadata *)metadata
sourceTracks:(NSArray<MP42Track *> *)sourceTracks
currentTracks:(NSArray<MP42Track *> *)currentTracks
error:(NSError * _Nullable *)outError;
- (void)cancel;
@end
NS_ASSUME_NONNULL_END