Recognize subtitle language and type tokens in filenames
getFilenameLanguage now tokenizes the name and scans right-to-left the way mkvmerge's own heuristics do, so a language code survives behind a trailing flag token ("...eng.hearing_impaired.srt" was read as "und"). Three-char tokens try ISO 639-2/T and the library's B codes; the dash stays out of the separator set so region subtags ("pt-BR") are not split into a bogus ISO 639-1 hit.
getFilenameSubtitlesTrackName maps community type tokens (sdh / hearing_impaired / deaf, cc / closed caption, forced, commentary, lyrics) to a bare conventional track name ("SDH"); the language rides its own element. MP4 has no SDH flag and the name-keyword pass in MP42File organizeMediaCharacteristicsForMediaType: turns these names into the real QuickTime tags (public.subtitles.forced-only, public.accessibility.*).
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
1 parent
7ff1ed4d0b
commit
30dd96ff79
4 files changed
+125
-49
No files matched your search
@@ -51,6 +51,9 @@ FourCharCode getTrackMediaSubType(MP4FileHandle fileHandle, MP4TrackId Id, uint3
|
||||
|
||||
NSString * getTrackLanguage(MP4FileHandle fileHandle, MP4TrackId Id);
|
||||
NSString * getFilenameLanguage(CFStringRef filename);
|
||||
// Community-convention track name ("SDH") from subtitle type tokens in the
|
||||
// filename (sdh/cc/forced/commentary/lyrics), or nil when absent
|
||||
NSString * getFilenameSubtitlesTrackName(CFStringRef filename);
|
||||
NSString * guessStringLanguage(NSString *string);
|
||||
|
||||
MP4Duration getTrackDuration(MP4FileHandle fileHandle, MP4TrackId trackId);
|
||||
|
||||
+114
-49
@@ -299,64 +299,129 @@ NSString * getTrackLanguage(MP4FileHandle fileHandle, MP4TrackId Id)
|
||||
return @(lang);
|
||||
}
|
||||
|
||||
// if the subtitle filename is something like title.en.srt or movie.fre.srt
|
||||
// this function detects it and returns the subtitle language
|
||||
// If the subtitle filename is something like title.en.srt or movie.fre.srt,
|
||||
// this function detects it and returns the subtitle language.
|
||||
//
|
||||
// The name is tokenized and scanned right-to-left, the way mkvmerge's own
|
||||
// filename heuristics work: community naming puts the language right before
|
||||
// the extension (movie.eng.sdh.srt, movie.eng.hearing_impaired.srt), and the
|
||||
// old "whatever sits between the last two dots" rule lost the language as
|
||||
// soon as a trailing type token shadowed it. Dashes are deliberately NOT a
|
||||
// separator here, so region-script subtags (pt-BR, zh-Hans) survive as one
|
||||
// token and their region part can't be misread as a 2-letter ISO 639-1 code
|
||||
// ("br" is Breton). Returns "und" when nothing decodes — every caller already
|
||||
// treats that as "no hint" (SRT/SSA fall back to guessing from the file's
|
||||
// text, the VobSub sup path keeps the converter's idx default).
|
||||
NSString * getFilenameLanguage(CFStringRef filename)
|
||||
{
|
||||
CFRange findResult;
|
||||
CFStringRef baseName = NULL;
|
||||
CFStringRef langStr = NULL;
|
||||
NSString *lang = @"en";
|
||||
NSString *baseName = [(__bridge NSString *)filename lastPathComponent].stringByDeletingPathExtension;
|
||||
NSArray<NSString *> *tokens =
|
||||
[baseName componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@". _"]];
|
||||
|
||||
MP42Languages *langManager = MP42Languages.defaultManager;
|
||||
|
||||
// find and strip the extension
|
||||
findResult = CFStringFind(filename, CFSTR("."), kCFCompareBackwards);
|
||||
findResult.length = findResult.location;
|
||||
findResult.location = 0;
|
||||
baseName = CFStringCreateWithSubstring(NULL, filename, findResult);
|
||||
for (NSString *token in [tokens reverseObjectEnumerator]) {
|
||||
NSString *lowercaseToken = token.lowercaseString;
|
||||
|
||||
// then find the previous period
|
||||
findResult = CFStringFind(baseName, CFSTR("."), kCFCompareBackwards);
|
||||
findResult.location++;
|
||||
findResult.length = CFStringGetLength(baseName) - findResult.location;
|
||||
|
||||
// check for 3 char language code
|
||||
if (findResult.length == 3) {
|
||||
char langCStr[4] = "";
|
||||
|
||||
langStr = CFStringCreateWithSubstring(NULL, baseName, findResult);
|
||||
CFStringGetCString(langStr, langCStr, 4, kCFStringEncodingASCII);
|
||||
lang = [langManager extendedTagForISO_639_2:@(langCStr)];
|
||||
|
||||
CFRelease(langStr);
|
||||
|
||||
// and for a 2 char language code
|
||||
} else if (findResult.length == 2) {
|
||||
char langCStr[3] = "";
|
||||
|
||||
langStr = CFStringCreateWithSubstring(NULL, baseName, findResult);
|
||||
CFStringGetCString(langStr, langCStr, 3, kCFStringEncodingASCII);
|
||||
lang = [langManager extendedTagForISO_639_1:@(langCStr)];
|
||||
|
||||
CFRelease(langStr);
|
||||
}
|
||||
else if (findResult.length) {
|
||||
char langCStr[40] = "";
|
||||
|
||||
langStr = CFStringCreateWithSubstring(NULL, baseName, findResult);
|
||||
CFStringGetCString(langStr, langCStr, 40, kCFStringEncodingASCII);
|
||||
lang = [langManager extendedTagForLang:@(langCStr)];
|
||||
|
||||
if ([lang isEqualToString:@"und"] && [langManager validateExtendedTag:@(langCStr)]) {
|
||||
lang = @(langCStr);
|
||||
if (lowercaseToken.length == 3) {
|
||||
// ISO 639-2/T through the code cache first, then the library
|
||||
// lookup which also matches the B codes (fre, ger, dut, …) that
|
||||
// mkvmerge-style naming still uses.
|
||||
NSString *tag = [langManager extendedTagForISO_639_2:lowercaseToken];
|
||||
if (tag.length && ![tag isEqualToString:@"und"]) {
|
||||
return tag;
|
||||
}
|
||||
tag = [langManager extendedTagForISO_639_2b:lowercaseToken];
|
||||
if (tag.length && ![tag isEqualToString:@"und"]) {
|
||||
return tag;
|
||||
}
|
||||
} else if (lowercaseToken.length == 2) {
|
||||
// ISO 639-1; unknown codes come back empty/und from the library
|
||||
NSString *tag = [langManager extendedTagForISO_639_1:lowercaseToken];
|
||||
if (tag.length && ![tag isEqualToString:@"und"]) {
|
||||
return tag;
|
||||
}
|
||||
} else if (lowercaseToken.length) {
|
||||
// A full language name ("english") or a locale-identifier style
|
||||
// extended tag ("pt-BR") as before
|
||||
NSString *tag = [langManager extendedTagForLang:token];
|
||||
if (tag.length && ![tag isEqualToString:@"und"]) {
|
||||
return tag;
|
||||
}
|
||||
if ([langManager validateExtendedTag:token]) {
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
CFRelease(langStr);
|
||||
}
|
||||
|
||||
CFRelease(baseName);
|
||||
return lang;
|
||||
return @"und";
|
||||
}
|
||||
|
||||
// Community subtitle-type tokens: the flags release naming puts next to the
|
||||
// language (movie.eng.sdh.srt, movie.eng.forced.srt). Neither container has a
|
||||
// real SDH/CC flag — MP4 only has per-sample forced flags — so the community
|
||||
// convention is to put the type in the track *name*, which is what players
|
||||
// like VLC display. Returns nil when the filename carries no such token.
|
||||
// Dashes are separators here (unlike getFilenameLanguage) so
|
||||
// "hearing-impaired" splits into hit-able tokens too.
|
||||
static NSString * subtitleTypeTokenFromFilename(CFStringRef filename)
|
||||
{
|
||||
NSString *baseName = [(__bridge NSString *)filename lastPathComponent].stringByDeletingPathExtension;
|
||||
NSArray<NSString *> *tokens =
|
||||
[baseName componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@". _-"]];
|
||||
|
||||
BOOL forced = NO, lyrics = NO, commentary = NO, closedCaptions = NO, hearingImpaired = NO;
|
||||
for (NSUInteger i = 0; i < tokens.count; i++) {
|
||||
NSString *lowercaseToken = tokens[i].lowercaseString;
|
||||
if ([lowercaseToken isEqualToString:@"sdh"] || [lowercaseToken isEqualToString:@"hearingimpaired"] ||
|
||||
[lowercaseToken isEqualToString:@"impaired"] || [lowercaseToken isEqualToString:@"deaf"]) {
|
||||
hearingImpaired = YES;
|
||||
} else if ([lowercaseToken isEqualToString:@"cc"]) {
|
||||
closedCaptions = YES;
|
||||
} else if ([lowercaseToken isEqualToString:@"closed"] && i + 1 < tokens.count &&
|
||||
[tokens[i + 1].lowercaseString hasPrefix:@"caption"]) {
|
||||
// "closed_caption"/"closed-caption" — the separator splits the
|
||||
// pair, so match it across two tokens. (Bare "captions" is NOT a
|
||||
// CC marker; naming like Movie.eng.Captions.srt stays unnamed.)
|
||||
closedCaptions = YES;
|
||||
} else if ([lowercaseToken isEqualToString:@"commentary"] || [lowercaseToken isEqualToString:@"commentaries"]) {
|
||||
commentary = YES;
|
||||
} else if ([lowercaseToken isEqualToString:@"forced"]) {
|
||||
forced = YES;
|
||||
} else if ([lowercaseToken isEqualToString:@"lyrics"] || [lowercaseToken isEqualToString:@"lyric"]) {
|
||||
lyrics = YES;
|
||||
}
|
||||
}
|
||||
|
||||
// SDH outranks CC (they overlap in naming), and both outrank the
|
||||
// forced-only flag (SDH files typically flag many lines forced too).
|
||||
if (hearingImpaired) {
|
||||
return @"SDH";
|
||||
}
|
||||
if (closedCaptions) {
|
||||
return @"CC";
|
||||
}
|
||||
if (commentary) {
|
||||
return @"Commentary";
|
||||
}
|
||||
if (lyrics) {
|
||||
return @"Lyrics";
|
||||
}
|
||||
if (forced) {
|
||||
return @"Forced";
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
// Track name for a subtitle file, following the community convention of
|
||||
// naming the track after the type token the filename carries ("SDH", "CC",
|
||||
// "Forced", "Commentary", "Lyrics"). Deliberately NOT prefixed with the
|
||||
// language: the language rides its own element (MP4 elng/mdhd, MKV
|
||||
// Language/LanguageIETF) and players show it alongside the name. Returns nil
|
||||
// when the filename carries no type token.
|
||||
NSString * getFilenameSubtitlesTrackName(CFStringRef filename)
|
||||
{
|
||||
return subtitleTypeTokenFromFilename(filename);
|
||||
}
|
||||
|
||||
NSString *guessStringLanguage(NSString *stringFromFileAtURL)
|
||||
|
||||
@@ -67,6 +67,10 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
}
|
||||
}
|
||||
|
||||
// "...eng.forced.ass" style type tokens name the track after the type
|
||||
// (MP4 has no SDH/CC flag; the name is the convention players show)
|
||||
track.name = getFilenameSubtitlesTrackName((__bridge CFStringRef)self.fileURL.path);
|
||||
|
||||
_parser = [[MP42SSAParser alloc] initWithString:stringFromFileAtURL];
|
||||
|
||||
if (!_parser.lines.count) {
|
||||
|
||||
@@ -57,6 +57,10 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
}
|
||||
}
|
||||
|
||||
// "...eng.sdh.srt" style type tokens name the track after the type
|
||||
// (MP4 has no SDH/CC flag; the name is the convention players show)
|
||||
track.name = getFilenameSubtitlesTrackName((__bridge CFStringRef)self.fileURL.path);
|
||||
|
||||
NSInteger success = 0;
|
||||
MP4Duration duration = 0;
|
||||
|
||||
|
||||
Reference in new issue
Block a user