MP42MkvRemuxer previously refused any track present in currentTracks
but not in sourceTracks; it now accepts added MP42SubtitleTracks whose
URL extension is srt/ssa/ass and muxes them into the rebuilt file as
S_TEXT/UTF8. The extension gate mirrors the app-side
DocumentWindowController.matroskaSubtitleExtensions list — the two
must stay in sync.
- Each added track gets an appended TrackEntry: TrackNumber = max of
all entries + 1 (refuse past 0x3FFF, the 2-byte unmarked-vint
limit), TrackType 17, FlagEnabled 1, FlagLacing 0, CodecID
S_TEXT/UTF8, ISO 0x22B59C + IETF 0x22B59D language, Name only when
it differs from the codec default.
- The model never holds subtitle text, so lines are re-parsed from
track.URL at save time: SRT via LoadSRTFromURL + serializer drain,
SSA/ASS via MP42SSAParser/MP42SSAConverter (styling stripped, same
lossy path the tx3g muxer takes). Zero parsed lines refuses.
- Blocks are written as BlockGroup{Block(unmarked-vint track number,
int16 BE relative timecode, UTF-8 text), BlockDuration(>=1 ms)}.
The BlockGroup wrapper is mandatory: a bare Block as a direct
Cluster child is silently ignored by libebml readers even though
ffmpeg tolerates it.
- Lines are assigned to the last existing Cluster whose Timecode is
<= the line's start time (inheriting missing cluster timecodes,
negative relative timecodes are legal); lines whose relative
timecode would exceed +32767 flush as synthesized Clusters right
after that cluster. Original clusters are copied verbatim and
never split.
- Added tracks join the existing per-track CueTrackPositions
bookkeeping, and the drop-all-remaining guard still fires before
additions, so drop-all-plus-add stays refused.
Adds TrackType/FlagLacing/CodecID/BlockDuration element ids to
MP42MatroskaTagMapping.h.
Co-Authored-By: Claude Code <noreply@anthropic.com>
92 lines
4.2 KiB
Objective-C
92 lines
4.2 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, when the user adds a subtitle file to one, 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).
|
|
//
|
|
// Added tracks: a model track absent from sourceTracks (by identity) must
|
|
// be an MP42SubtitleTrack whose URL ends in .srt/.ssa/.ass; its text is
|
|
// re-parsed from the URL at save time (the model never holds it) and
|
|
// written as S_TEXT/UTF8 BlockGroup{Block, BlockDuration} elements, past
|
|
// a fresh appended TrackEntry numbered past every number in the file.
|
|
// Blocks land in the original cluster whose Timecode window contains them
|
|
// (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.
|
|
//
|
|
// 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,
|
|
// remuxes that would leave zero tracks, and added tracks that are not
|
|
// parseable .srt/.ssa/.ass subtitle files.
|
|
// - SSA styling is stripped to plain text (same conversion the MP4 mux
|
|
// path applies); per-line SRT "!!!" forced markers and top-position
|
|
// markers are not carried into the Matroska blocks.
|
|
//
|
|
|
|
#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 edits; entries whose paired sourceTracks object
|
|
// is no longer present (compared by object identity) are removed, and
|
|
// model tracks with no sourceTracks counterpart are appended as new
|
|
// S_TEXT/UTF8 subtitle tracks (see the header notes).
|
|
//
|
|
// 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
|