mirror of
https://github.com/dirtydishes/dreamio.git
synced 2026-06-06 13:37:24 +00:00
preserve opensubtitles caption labels
This commit is contained in:
parent
046444f9ad
commit
ae996e7ffb
6 changed files with 628 additions and 5 deletions
|
|
@ -40,6 +40,108 @@ struct SubtitleTrack: Equatable {
|
|||
let name: String
|
||||
}
|
||||
|
||||
enum SubtitleDisplayName {
|
||||
private static let genericLabels = [
|
||||
"external subtitle",
|
||||
"subtitle",
|
||||
"unknown"
|
||||
]
|
||||
|
||||
private static let languageCodeAliases = [
|
||||
"eng": "en",
|
||||
"en": "en",
|
||||
"spa": "es",
|
||||
"es": "es",
|
||||
"fre": "fr",
|
||||
"fra": "fr",
|
||||
"fr": "fr"
|
||||
]
|
||||
|
||||
static func displayName(for candidate: SubtitleCandidate) -> String {
|
||||
if let label = meaningfulDisplayText(candidate.label) {
|
||||
return label
|
||||
}
|
||||
|
||||
if let languageName = languageName(for: candidate.language) {
|
||||
return languageName
|
||||
}
|
||||
|
||||
return fallbackName(from: candidate)
|
||||
}
|
||||
|
||||
static func name(forVLCTrackName trackName: String, preservedName: String?) -> String {
|
||||
guard isGenericLabel(trackName), let preservedName else {
|
||||
return trackName
|
||||
}
|
||||
return preservedName
|
||||
}
|
||||
|
||||
static func isGenericLabel(_ value: String) -> Bool {
|
||||
let normalized = value.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !normalized.isEmpty else {
|
||||
return true
|
||||
}
|
||||
|
||||
let lowercased = normalized.lowercased()
|
||||
if genericLabels.contains(lowercased) {
|
||||
return true
|
||||
}
|
||||
if Int(normalized) != nil {
|
||||
return true
|
||||
}
|
||||
if lowercased.range(of: #"^track\s*\d+$"#, options: .regularExpression) != nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private static func meaningfulDisplayText(_ value: String?) -> String? {
|
||||
guard let value else {
|
||||
return nil
|
||||
}
|
||||
let trimmed = value.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !isGenericLabel(trimmed) else {
|
||||
return nil
|
||||
}
|
||||
guard trimmed.count <= 3 else {
|
||||
return trimmed
|
||||
}
|
||||
return languageName(for: trimmed) ?? trimmed
|
||||
}
|
||||
|
||||
private static func languageName(for value: String?) -> String? {
|
||||
guard let value else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let trimmed = value.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !trimmed.isEmpty, Int(trimmed) == nil else {
|
||||
return nil
|
||||
}
|
||||
|
||||
if trimmed.count > 3 {
|
||||
return trimmed.capitalized
|
||||
}
|
||||
|
||||
let lowercased = trimmed.lowercased()
|
||||
let languageCode = languageCodeAliases[lowercased] ?? lowercased
|
||||
guard let name = Locale.current.localizedString(forLanguageCode: languageCode) else {
|
||||
return nil
|
||||
}
|
||||
return name.capitalized
|
||||
}
|
||||
|
||||
private static func fallbackName(from candidate: SubtitleCandidate) -> String {
|
||||
let trimmedLabel = candidate.label.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
if !isGenericLabel(trimmedLabel) {
|
||||
return trimmedLabel
|
||||
}
|
||||
|
||||
let fileName = candidate.url.deletingPathExtension().lastPathComponent
|
||||
return fileName.isEmpty ? "External Subtitle" : fileName
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
enum SubtitleDebugFormatter {
|
||||
static func candidateSummary(_ candidates: [SubtitleCandidate]) -> String {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue