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
+123
-47
No files matched your search
@@ -51,6 +51,9 @@ FourCharCode getTrackMediaSubType(MP4FileHandle fileHandle, MP4TrackId Id, uint3
|
|||||||
|
|
||||||
NSString * getTrackLanguage(MP4FileHandle fileHandle, MP4TrackId Id);
|
NSString * getTrackLanguage(MP4FileHandle fileHandle, MP4TrackId Id);
|
||||||
NSString * getFilenameLanguage(CFStringRef filename);
|
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);
|
NSString * guessStringLanguage(NSString *string);
|
||||||
|
|
||||||
MP4Duration getTrackDuration(MP4FileHandle fileHandle, MP4TrackId trackId);
|
MP4Duration getTrackDuration(MP4FileHandle fileHandle, MP4TrackId trackId);
|
||||||
|
|||||||
+112
-47
@@ -299,64 +299,129 @@ NSString * getTrackLanguage(MP4FileHandle fileHandle, MP4TrackId Id)
|
|||||||
return @(lang);
|
return @(lang);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the subtitle filename is something like title.en.srt or movie.fre.srt
|
// If the subtitle filename is something like title.en.srt or movie.fre.srt,
|
||||||
// this function detects it and returns the subtitle language
|
// 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)
|
NSString * getFilenameLanguage(CFStringRef filename)
|
||||||
{
|
{
|
||||||
CFRange findResult;
|
NSString *baseName = [(__bridge NSString *)filename lastPathComponent].stringByDeletingPathExtension;
|
||||||
CFStringRef baseName = NULL;
|
NSArray<NSString *> *tokens =
|
||||||
CFStringRef langStr = NULL;
|
[baseName componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@". _"]];
|
||||||
NSString *lang = @"en";
|
|
||||||
|
|
||||||
MP42Languages *langManager = MP42Languages.defaultManager;
|
MP42Languages *langManager = MP42Languages.defaultManager;
|
||||||
|
|
||||||
// find and strip the extension
|
for (NSString *token in [tokens reverseObjectEnumerator]) {
|
||||||
findResult = CFStringFind(filename, CFSTR("."), kCFCompareBackwards);
|
NSString *lowercaseToken = token.lowercaseString;
|
||||||
findResult.length = findResult.location;
|
|
||||||
findResult.location = 0;
|
|
||||||
baseName = CFStringCreateWithSubstring(NULL, filename, findResult);
|
|
||||||
|
|
||||||
// then find the previous period
|
if (lowercaseToken.length == 3) {
|
||||||
findResult = CFStringFind(baseName, CFSTR("."), kCFCompareBackwards);
|
// ISO 639-2/T through the code cache first, then the library
|
||||||
findResult.location++;
|
// lookup which also matches the B codes (fre, ger, dut, …) that
|
||||||
findResult.length = CFStringGetLength(baseName) - findResult.location;
|
// mkvmerge-style naming still uses.
|
||||||
|
NSString *tag = [langManager extendedTagForISO_639_2:lowercaseToken];
|
||||||
// check for 3 char language code
|
if (tag.length && ![tag isEqualToString:@"und"]) {
|
||||||
if (findResult.length == 3) {
|
return tag;
|
||||||
char langCStr[4] = "";
|
}
|
||||||
|
tag = [langManager extendedTagForISO_639_2b:lowercaseToken];
|
||||||
langStr = CFStringCreateWithSubstring(NULL, baseName, findResult);
|
if (tag.length && ![tag isEqualToString:@"und"]) {
|
||||||
CFStringGetCString(langStr, langCStr, 4, kCFStringEncodingASCII);
|
return tag;
|
||||||
lang = [langManager extendedTagForISO_639_2:@(langCStr)];
|
}
|
||||||
|
} else if (lowercaseToken.length == 2) {
|
||||||
CFRelease(langStr);
|
// ISO 639-1; unknown codes come back empty/und from the library
|
||||||
|
NSString *tag = [langManager extendedTagForISO_639_1:lowercaseToken];
|
||||||
// and for a 2 char language code
|
if (tag.length && ![tag isEqualToString:@"und"]) {
|
||||||
} else if (findResult.length == 2) {
|
return tag;
|
||||||
char langCStr[3] = "";
|
}
|
||||||
|
} else if (lowercaseToken.length) {
|
||||||
langStr = CFStringCreateWithSubstring(NULL, baseName, findResult);
|
// A full language name ("english") or a locale-identifier style
|
||||||
CFStringGetCString(langStr, langCStr, 3, kCFStringEncodingASCII);
|
// extended tag ("pt-BR") as before
|
||||||
lang = [langManager extendedTagForISO_639_1:@(langCStr)];
|
NSString *tag = [langManager extendedTagForLang:token];
|
||||||
|
if (tag.length && ![tag isEqualToString:@"und"]) {
|
||||||
CFRelease(langStr);
|
return tag;
|
||||||
|
}
|
||||||
|
if ([langManager validateExtendedTag:token]) {
|
||||||
|
return token;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CFRelease(langStr);
|
return @"und";
|
||||||
}
|
}
|
||||||
|
|
||||||
CFRelease(baseName);
|
// Community subtitle-type tokens: the flags release naming puts next to the
|
||||||
return lang;
|
// 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)
|
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];
|
_parser = [[MP42SSAParser alloc] initWithString:stringFromFileAtURL];
|
||||||
|
|
||||||
if (!_parser.lines.count) {
|
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;
|
NSInteger success = 0;
|
||||||
MP4Duration duration = 0;
|
MP4Duration duration = 0;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user