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>
This commit is contained in:
1 parent
bfa477b799
commit
1040f47542
18 files changed
+4397
-86
No files matched your search
+55
-80
@@ -15,6 +15,7 @@
|
||||
|
||||
#import "MatroskaParser.h"
|
||||
#import "MatroskaFile.h"
|
||||
#import "MP42MatroskaTagMapping.h"
|
||||
#include "avutil.h"
|
||||
|
||||
#import "MP42PrivateUtilities.h"
|
||||
@@ -79,6 +80,11 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
}
|
||||
|
||||
- (instancetype)initWithURL:(NSURL *)fileURL error:(NSError * __autoreleasing *)outError
|
||||
{
|
||||
return [self initWithURL:fileURL tagsOnly:NO error:outError];
|
||||
}
|
||||
|
||||
- (instancetype)initWithURL:(NSURL *)fileURL tagsOnly:(BOOL)tagsOnly error:(NSError * __autoreleasing *)outError
|
||||
{
|
||||
if ((self = [super initWithURL:fileURL])) {
|
||||
_ioStream = calloc(1, sizeof(StdIoStream));
|
||||
@@ -263,6 +269,15 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
}
|
||||
|
||||
if (newTrack) {
|
||||
if (tagsOnly) {
|
||||
// Tags-only documents are diffed against the file on save
|
||||
// (see MP42MkvTagWriter): the model must mirror the
|
||||
// file's own TrackFlagEnabled, otherwise the MP4-oriented
|
||||
// heuristic below would make untouched tracks look
|
||||
// edited and the first save would rewrite every flag.
|
||||
newTrack.enabled = mkvTrack->Enabled != 0;
|
||||
}
|
||||
|
||||
newTrack.format = CodecIDToFourCC(mkvTrack);
|
||||
|
||||
if ([newTrack isKindOfClass:[MP42VideoTrack class]] && ((MP42VideoTrack *)newTrack).dolbyVision.profile == 5) {
|
||||
@@ -343,7 +358,7 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
[self addTrack:newTrack];
|
||||
}
|
||||
|
||||
[self.metadata mergeMetadata:[self readMatroskaMetadata] overwrite:true];
|
||||
[self.metadata mergeMetadata:[self readMatroskaMetadataTagsOnly:tagsOnly] overwrite:true];
|
||||
}
|
||||
|
||||
return self;
|
||||
@@ -356,24 +371,24 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
|
||||
#pragma mark - Metadata
|
||||
|
||||
#define COLLECTION 70
|
||||
#define SEASON 60
|
||||
#define MOVIE 50
|
||||
#define PART 40
|
||||
#define CHAPTER 30
|
||||
#define SCENE 20
|
||||
#define SHOT 10
|
||||
|
||||
- (void)addMetadataItem:(id)value identifier:(NSString *)identifier metadata:(MP42Metadata *)metadata
|
||||
{
|
||||
[self addMetadataItem:value identifier:identifier language:nil metadata:metadata];
|
||||
}
|
||||
|
||||
- (void)addMetadataItem:(id)value
|
||||
identifier:(NSString *)identifier
|
||||
language:(NSString *)language
|
||||
metadata:(MP42Metadata *)metadata
|
||||
{
|
||||
MP42MetadataItem *item = [MP42MetadataItem metadataItemWithIdentifier:identifier
|
||||
value:value
|
||||
dataType:MP42MetadataItemDataTypeUnspecified
|
||||
extendedLanguageTag:nil];
|
||||
extendedLanguageTag:language];
|
||||
[metadata addMetadataItem:item];
|
||||
}
|
||||
|
||||
- (MP42Metadata *)readMatroskaMetadata
|
||||
- (MP42Metadata *)readMatroskaMetadataTagsOnly:(BOOL)tagsOnly
|
||||
{
|
||||
MP42Metadata *metadata = [[MP42Metadata alloc] init];
|
||||
|
||||
@@ -385,57 +400,8 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
metadata:metadata];
|
||||
}
|
||||
|
||||
NSDictionary<NSString *, NSString *> *simpleMap = @{
|
||||
@"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,
|
||||
|
||||
@"ENCODER": MP42MetadataKeyEncodingTool,
|
||||
};
|
||||
|
||||
NSDictionary<NSString *, NSString *> *collectionMap = @{
|
||||
@"TITLE": MP42MetadataKeyTVShow,
|
||||
@"COPYRIGHT": MP42MetadataKeyCopyright,
|
||||
@"DESCRIPTION": MP42MetadataKeySeriesDescription,
|
||||
@"SYNOPSIS": MP42MetadataKeySeriesDescription,
|
||||
};
|
||||
|
||||
NSDictionary<NSString *, NSString *> *seasonMap = @{
|
||||
@"PART_NUMBER": MP42MetadataKeyTVSeason,
|
||||
@"DESCRIPTION": MP42MetadataKeySeriesDescription,
|
||||
@"SYNOPSIS": MP42MetadataKeySeriesDescription,
|
||||
@"DATE_RELEASED": MP42MetadataKeyReleaseDate
|
||||
};
|
||||
// The SimpleTag maps live in MP42MatroskaTagMapping.h, shared verbatim
|
||||
// with MP42MkvTagWriter so reading and writing cannot drift apart.
|
||||
|
||||
unsigned count;
|
||||
Tag *tag_groups;
|
||||
@@ -448,8 +414,8 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
for (unsigned int xi = 0; xi < count; xi++) {
|
||||
Tag *tags = &tag_groups[xi];
|
||||
|
||||
ulonglong UID = 0;
|
||||
NSDictionary<NSString *, NSString *> *map = simpleMap;
|
||||
NSInteger level = MP42MkvTargetGlobal;
|
||||
NSDictionary<NSString *, NSString *> *map = nil;
|
||||
|
||||
if (tags->nTargetsSize) {
|
||||
struct Target *target = &tags->Targets[0];
|
||||
@@ -458,21 +424,15 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
continue;
|
||||
}
|
||||
|
||||
UID = target->UID;
|
||||
level = (NSInteger)target->UID;
|
||||
|
||||
if (UID == CHAPTER ||
|
||||
UID == SCENE ||
|
||||
UID == SHOT) {
|
||||
if (!MP42MkvTargetLevelIsModeled(level)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (UID == COLLECTION) {
|
||||
map = collectionMap;
|
||||
} else if (UID == SEASON) {
|
||||
map = seasonMap;
|
||||
}
|
||||
}
|
||||
|
||||
map = MP42MkvSimpleTagMapForTargetLevel(level);
|
||||
|
||||
for (unsigned int yi = 0; yi < tags->nSimpleTags; yi++) {
|
||||
struct SimpleTag *tag = &tags->SimpleTags[yi];
|
||||
|
||||
@@ -480,11 +440,19 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
NSString *simpleTagKey = @(tag->Name);
|
||||
NSString *key = map[simpleTagKey];
|
||||
|
||||
// Preserve non-default tag languages for the round trip
|
||||
// back through MP42MkvTagWriter.
|
||||
NSString *language = nil;
|
||||
if (tag->Language[0] && strcasecmp(tag->Language, "und") != 0) {
|
||||
language = @(tag->Language);
|
||||
}
|
||||
|
||||
if (key && tag->Value) {
|
||||
[self addMetadataItem:@(tag->Value)
|
||||
identifier:key
|
||||
language:language
|
||||
metadata:metadata];
|
||||
} else if (UID == SEASON &&
|
||||
} else if (level == MP42MkvTargetSeason &&
|
||||
[simpleTagKey isEqualToString:@"TOTAL_PARTS"]) {
|
||||
total_parts = tag->Value;
|
||||
} else if ([simpleTagKey isEqualToString:@"PART_NUMBER"]) {
|
||||
@@ -510,11 +478,18 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
identifier:MP42MetadataKeyTVEpisodeNumber
|
||||
metadata:metadata];
|
||||
|
||||
int season = [metadata metadataItemsFilteredByIdentifier:MP42MetadataKeyTVSeason].firstObject.numberValue.intValue;
|
||||
if (season) {
|
||||
[self addMetadataItem:[NSString stringWithFormat:@"%d%02d", season, track]
|
||||
identifier:MP42MetadataKeyTVEpisodeID
|
||||
metadata:metadata];
|
||||
// Synthesize a "season x episode y" ID for the MP4 conversion path.
|
||||
// In tags-only mode this is skipped: the writer now round-trips
|
||||
// MP42MetadataKeyTVEpisodeID as EPISODEID, so a synthesized value
|
||||
// would leak into the file as a tag the user never set (and would
|
||||
// never converge). An explicit EPISODEID tag maps in normally.
|
||||
if (!tagsOnly) {
|
||||
int season = [metadata metadataItemsFilteredByIdentifier:MP42MetadataKeyTVSeason].firstObject.numberValue.intValue;
|
||||
if (season) {
|
||||
[self addMetadataItem:[NSString stringWithFormat:@"%d%02d", season, track]
|
||||
identifier:MP42MetadataKeyTVEpisodeID
|
||||
metadata:metadata];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user