- 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>
239 lines
9.5 KiB
Objective-C
239 lines
9.5 KiB
Objective-C
//
|
|
// MP42MatroskaTagMapping.m
|
|
// MP42Foundation
|
|
//
|
|
|
|
#import "MP42MatroskaTagMapping.h"
|
|
|
|
#import "MP42MetadataFormat.h"
|
|
|
|
@implementation MP42MkvTagPair
|
|
|
|
- (instancetype)initWithSimpleTagName:(NSString *)name metadataKey:(NSString *)key
|
|
{
|
|
self = [super init];
|
|
if (self) {
|
|
_simpleTagName = [name copy];
|
|
_metadataKey = [key copy];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
@end
|
|
|
|
//
|
|
// Read maps. These were previously inline in MP42MkvImporter's
|
|
// readMatroskaMetadata; they live here now because the tag writer serializes
|
|
// its inverse and any drift between the two directions silently drops tags.
|
|
//
|
|
|
|
static NSDictionary<NSString *, NSString *> *MP42MkvDefaultMap(void)
|
|
{
|
|
static NSDictionary *map;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
map = @{
|
|
@"TITLE": MP42MetadataKeyName,
|
|
@"SUBTITLE": MP42MetadataKeyTrackSubTitle,
|
|
@"ALBUM": MP42MetadataKeyAlbum,
|
|
@"ARTIST": MP42MetadataKeyArtist,
|
|
|
|
@"COMMENT": MP42MetadataKeyUserComment,
|
|
@"GENRE": MP42MetadataKeyUserGenre,
|
|
@"DATE_RELEASED": MP42MetadataKeyReleaseDate,
|
|
|
|
@"BPM": MP42MetadataKeyBeatsPerMin,
|
|
|
|
@"KEYWORDS": MP42MetadataKeyKeywords,
|
|
@"THANKS_TO": MP42MetadataKeyThanks,
|
|
@"COPYRIGHT": MP42MetadataKeyCopyright,
|
|
|
|
@"DESCRIPTION": MP42MetadataKeyDescription,
|
|
@"SYNOPSIS": MP42MetadataKeyLongDescription,
|
|
|
|
@"DIRECTOR_OF_PHOTOGRAPHY": MP42MetadataKeyArtDirector,
|
|
@"COMPOSER": MP42MetadataKeyComposer,
|
|
@"ARRANGER": MP42MetadataKeyArranger,
|
|
@"WRITTEN_BY": MP42MetadataKeyAuthor,
|
|
@"LYRICS": MP42MetadataKeyLyrics,
|
|
@"CONDUCTOR": MP42MetadataKeyConductor,
|
|
@"PUBLISHER": MP42MetadataKeyPublisher,
|
|
@"SOUND_ENGINEER": MP42MetadataKeySoundEngineer,
|
|
@"LEAD_PERFORMER": MP42MetadataKeySoloist,
|
|
|
|
@"PRODUCTION_STUDIO": MP42MetadataKeyStudio,
|
|
@"DIRECTOR": MP42MetadataKeyDirector,
|
|
@"PRODUCER": MP42MetadataKeyProducer,
|
|
@"EXECUTIVE_PRODUCER": MP42MetadataKeyExecProducer,
|
|
@"SCREENPLAY_BY": MP42MetadataKeyScreenwriters,
|
|
@"CAST": MP42MetadataKeyCast,
|
|
|
|
// Movie-tab fields with no Matroska-spec equivalent; these names
|
|
// follow the community style (uppercase words, no separators) and
|
|
// exist mainly so values survive a Subler save/reopen round-trip.
|
|
@"RATING": MP42MetadataKeyRating, // parental advisory string
|
|
@"CONTENTRATING": MP42MetadataKeyContentRating, // numeric rating code
|
|
@"MEDIAKIND": MP42MetadataKeyMediaKind,
|
|
@"HD": MP42MetadataKeyHDVideo,
|
|
|
|
@"NETWORK": MP42MetadataKeyTVNetwork,
|
|
@"EPISODEID": MP42MetadataKeyTVEpisodeID,
|
|
|
|
@"ENCODER": MP42MetadataKeyEncodingTool,
|
|
};
|
|
});
|
|
return map;
|
|
}
|
|
|
|
static NSDictionary<NSString *, NSString *> *MP42MkvCollectionMap(void)
|
|
{
|
|
static NSDictionary *map;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
map = @{
|
|
@"TITLE": MP42MetadataKeyTVShow,
|
|
@"COPYRIGHT": MP42MetadataKeyCopyright,
|
|
@"DESCRIPTION": MP42MetadataKeySeriesDescription,
|
|
@"SYNOPSIS": MP42MetadataKeySeriesDescription,
|
|
};
|
|
});
|
|
return map;
|
|
}
|
|
|
|
static NSDictionary<NSString *, NSString *> *MP42MkvSeasonMap(void)
|
|
{
|
|
static NSDictionary *map;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
map = @{
|
|
@"PART_NUMBER": MP42MetadataKeyTVSeason,
|
|
@"DESCRIPTION": MP42MetadataKeySeriesDescription,
|
|
@"SYNOPSIS": MP42MetadataKeySeriesDescription,
|
|
@"DATE_RELEASED": MP42MetadataKeyReleaseDate,
|
|
};
|
|
});
|
|
return map;
|
|
}
|
|
|
|
NSDictionary<NSString *, NSString *> *MP42MkvSimpleTagMapForTargetLevel(NSInteger level)
|
|
{
|
|
switch (level) {
|
|
case MP42MkvTargetCollection: return MP42MkvCollectionMap();
|
|
case MP42MkvTargetSeason: return MP42MkvSeasonMap();
|
|
default: return MP42MkvDefaultMap();
|
|
}
|
|
}
|
|
|
|
BOOL MP42MkvTargetLevelIsModeled(NSInteger level)
|
|
{
|
|
// Chapter/scene/shot level tags are skipped by the importer and must be
|
|
// preserved verbatim by the writer. Everything else (global, part, movie,
|
|
// season, collection) feeds the metadata model.
|
|
return level != MP42MkvTargetChapter &&
|
|
level != MP42MkvTargetScene &&
|
|
level != MP42MkvTargetShot;
|
|
}
|
|
|
|
// Canonical SimpleTag name order per level, used to give the writer stable
|
|
// output. Every name must exist in the read map for its level; names added
|
|
// to a read map without being listed here are appended automatically (in
|
|
// unspecified order) rather than silently dropped.
|
|
static NSArray<NSString *> *MP42MkvWriteNameOrder(NSInteger level)
|
|
{
|
|
switch (level) {
|
|
case MP42MkvTargetCollection:
|
|
return @[ @"TITLE", @"SYNOPSIS", @"COPYRIGHT" ];
|
|
case MP42MkvTargetSeason:
|
|
return @[ @"PART_NUMBER", @"SYNOPSIS", @"DATE_RELEASED" ];
|
|
default:
|
|
return @[ @"TITLE", @"ALBUM", @"ARTIST", @"PART_NUMBER", @"TOTAL_PARTS",
|
|
@"DATE_RELEASED", @"GENRE", @"DESCRIPTION", @"SYNOPSIS",
|
|
@"COMMENT", @"COPYRIGHT", @"PRODUCTION_STUDIO",
|
|
@"DIRECTOR", @"PRODUCER", @"EXECUTIVE_PRODUCER",
|
|
@"SCREENPLAY_BY", @"WRITTEN_BY", @"COMPOSER", @"ARRANGER",
|
|
@"LYRICS", @"CONDUCTOR", @"PUBLISHER", @"SOUND_ENGINEER",
|
|
@"LEAD_PERFORMER", @"DIRECTOR_OF_PHOTOGRAPHY", @"CAST",
|
|
@"RATING", @"CONTENTRATING", @"MEDIAKIND", @"HD",
|
|
@"NETWORK", @"EPISODEID",
|
|
@"KEYWORDS", @"THANKS_TO", @"BPM", @"ENCODER", @"SUBTITLE" ];
|
|
}
|
|
}
|
|
|
|
// All metadata keys MP42MkvTagWriter can round-trip, cached for the UI's
|
|
// "can this field be saved?" hints. Kept here (next to the maps it derives
|
|
// from) so the UI can never drift from what the writer actually writes.
|
|
static NSSet<NSString *> *MP42MkvPersistedMetadataKeys(void)
|
|
{
|
|
static NSSet<NSString *> *keys;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
NSMutableSet<NSString *> *mutableKeys = [NSMutableSet setWithObjects:
|
|
// Regenerated as PART_NUMBER / TOTAL_PARTS
|
|
MP42MetadataKeyTrackNumber,
|
|
// Fallback source for movie-level PART_NUMBER
|
|
// when TrackNumber carries no index
|
|
MP42MetadataKeyTVEpisodeNumber,
|
|
// Regenerated as season-level PART_NUMBER
|
|
// (also a season-map pair key, listed for clarity)
|
|
MP42MetadataKeyTVSeason,
|
|
// Stored as an AttachedFile, not a SimpleTag
|
|
MP42MetadataKeyCoverArt,
|
|
nil];
|
|
for (NSNumber *level in @[ @(MP42MkvTargetMovie), @(MP42MkvTargetSeason), @(MP42MkvTargetCollection) ]) {
|
|
for (MP42MkvTagPair *pair in MP42MkvWritePairsForTargetLevel(level.integerValue)) {
|
|
[mutableKeys addObject:pair.metadataKey];
|
|
}
|
|
}
|
|
keys = [mutableKeys copy];
|
|
});
|
|
return keys;
|
|
}
|
|
|
|
BOOL MP42MkvMetadataKeyIsPersisted(NSString *metadataKey)
|
|
{
|
|
return metadataKey.length > 0 && [MP42MkvPersistedMetadataKeys() containsObject:metadataKey];
|
|
}
|
|
|
|
NSArray<MP42MkvTagPair *> *MP42MkvWritePairsForTargetLevel(NSInteger level)
|
|
{
|
|
static NSMutableDictionary<NSNumber *, NSArray<MP42MkvTagPair *> *> *cache;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
cache = [NSMutableDictionary dictionary];
|
|
});
|
|
|
|
NSNumber *levelKey = @(level);
|
|
@synchronized (cache) {
|
|
NSArray<MP42MkvTagPair *> *pairs = cache[levelKey];
|
|
if (pairs) {
|
|
return pairs;
|
|
}
|
|
|
|
NSDictionary<NSString *, NSString *> *readMap = MP42MkvSimpleTagMapForTargetLevel(level);
|
|
NSMutableArray<MP42MkvTagPair *> *result = [NSMutableArray array];
|
|
NSMutableSet<NSString *> *usedKeys = [NSMutableSet set];
|
|
|
|
for (NSString *name in MP42MkvWriteNameOrder(level)) {
|
|
NSString *key = readMap[name];
|
|
if (key && ![usedKeys containsObject:key]) {
|
|
[usedKeys addObject:key];
|
|
[result addObject:[[MP42MkvTagPair alloc] initWithSimpleTagName:name metadataKey:key]];
|
|
}
|
|
}
|
|
|
|
// Names present in the read map but missing from the order list still
|
|
// need to be written; append them so both directions stay in sync.
|
|
for (NSString *name in readMap) {
|
|
NSString *key = readMap[name];
|
|
if (![usedKeys containsObject:key]) {
|
|
[usedKeys addObject:key];
|
|
[result addObject:[[MP42MkvTagPair alloc] initWithSimpleTagName:name metadataKey:key]];
|
|
}
|
|
}
|
|
|
|
pairs = [result copy];
|
|
cache[levelKey] = pairs;
|
|
return pairs;
|
|
}
|
|
}
|