Files
MP42Foundation/MP42/MP42MkvTagWriter.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

70 lines
2.8 KiB
Objective-C

//
// MP42MkvTagWriter.h
// MP42Foundation
//
// Rewrites the Tags element (and, when the cover art changed, the
// Attachments element) of an existing Matroska file in place, leaving all
// track payloads and clusters untouched. Conceptually the same job as
// `mkvpropedit --tags`.
//
// When model tracks are supplied, the per-track fields the model exposes —
// Name, TrackLanguage/LanguageIETF, TrackFlagEnabled and (for subtitles)
// TrackFlagForced — are diffed against each TrackEntry's effective values
// and the Tracks element is rebuilt with only the changed fields
// substituted. Conceptually `mkvpropedit --edit track:… --set …`; all other
// TrackEntry children (CodecPrivate, timings, …) are copied byte-for-byte.
//
// When the new content fits into the old element's extent plus the Void
// padding that follows it, the region is rewritten without shifting any
// other byte (fast path, SeekHead/Cues/Segment stay valid). Otherwise the
// file tail is rebuilt through a temporary file, SeekHead and Cues position
// entries are recomputed, and the result atomically replaces the original.
//
// Tag groups Subler does not model (chapter/scene/shot targets, track/
// edition/attachment targets, unmapped SimpleTag names such as ACTOR,
// binary-valued tags) and non-cover attachments are preserved verbatim from
// the file on disk, so saving never silently drops data the UI never showed.
//
#import <Foundation/Foundation.h>
#import <MP42Foundation/MP42Ebml.h>
@class MP42Metadata;
@class MP42Track;
NS_ASSUME_NONNULL_BEGIN
@interface MP42MkvTagWriter : NSObject
@property (nonatomic, readonly) NSURL *fileURL;
// When set, the write refuses to touch a file whose size or modification
// date no longer matches (callers snapshot these when the document is read).
@property (nonatomic) uint64_t expectedFileSize;
@property (nonatomic, copy, nullable) NSDate *expectedModificationDate;
// Called on the writing thread with a 0...1 estimate; may be nil.
@property (nonatomic, copy, nullable) void (^progressHandler)(double progress);
- (instancetype)initWithURL:(NSURL *)URL;
- (BOOL)writeMetadata:(MP42Metadata *)metadata error:(NSError * _Nullable *)outError;
// Also apply per-track edits (name, language, enabled, subtitle forced)
// taken from the model tracks. The array must hold the document's tracks in
// file order; chapter tracks are ignored (they are not Matroska tracks).
// Pass nil to leave the Tracks element untouched.
- (BOOL)writeMetadata:(MP42Metadata *)metadata
tracks:(nullable NSArray<MP42Track *> *)tracks
error:(NSError * _Nullable *)outError;
// Safe to call from another thread while -writeMetadata:error: is running;
// the write aborts at the next chunk boundary leaving the original file
// untouched.
- (void)cancel;
@end
NS_ASSUME_NONNULL_END