fix: prefer exact language code match in track selection

Fixes #703
This commit is contained in:
edde746
2026-03-15 11:40:45 +01:00
parent bf464b257d
commit 5f597ef567
+22 -4
View File
@@ -39,9 +39,12 @@ SubtitleTrack? findMpvTrackForPlexSubtitle(PlexSubtitleTrack plexTrack, List<Sub
int score = 0;
// Language match is most important (+10)
// Language match is most important (+10, +1 bonus for exact code match)
if (_languagesMatch(mpvTrack.language, plexTrack.languageCode)) {
score += 10;
if (_languageCodesExactMatch(mpvTrack.language, plexTrack.languageCode)) {
score += 1;
}
}
// Codec match (+5)
@@ -94,9 +97,12 @@ PlexSubtitleTrack? findPlexTrackForMpvSubtitle(SubtitleTrack mpvTrack, List<Plex
int score = 0;
// Language match is most important (+10)
// Language match is most important (+10, +1 bonus for exact code match)
if (_languagesMatch(mpvTrack.language, plexTrack.languageCode)) {
score += 10;
if (_languageCodesExactMatch(mpvTrack.language, plexTrack.languageCode)) {
score += 1;
}
}
// Codec match (+5)
@@ -134,9 +140,12 @@ AudioTrack? findMpvTrackForPlexAudio(PlexAudioTrack plexTrack, List<AudioTrack>
for (final mpvTrack in mpvTracks) {
int score = 0;
// Language match is most important (+10)
// Language match is most important (+10, +1 bonus for exact code match)
if (_languagesMatch(mpvTrack.language, plexTrack.languageCode)) {
score += 10;
if (_languageCodesExactMatch(mpvTrack.language, plexTrack.languageCode)) {
score += 1;
}
}
// Codec match (+5)
@@ -176,9 +185,12 @@ PlexAudioTrack? findPlexTrackForMpvAudio(AudioTrack mpvTrack, List<PlexAudioTrac
for (final plexTrack in plexTracks) {
int score = 0;
// Language match is most important (+10)
// Language match is most important (+10, +1 bonus for exact code match)
if (_languagesMatch(mpvTrack.language, plexTrack.languageCode)) {
score += 10;
if (_languageCodesExactMatch(mpvTrack.language, plexTrack.languageCode)) {
score += 1;
}
}
// Codec match (+5)
@@ -208,6 +220,12 @@ PlexAudioTrack? findPlexTrackForMpvAudio(AudioTrack mpvTrack, List<PlexAudioTrac
return bestScore >= 10 ? bestMatch : null;
}
/// Check if two language codes match exactly (after normalizing case and stripping region suffixes)
bool _languageCodesExactMatch(String? a, String? b) {
if (a == null || b == null) return false;
return a.toLowerCase().split('-').first == b.toLowerCase().split('-').first;
}
/// Check if two language codes refer to the same language
/// Handles both ISO 639-1 (2-letter) and ISO 639-2 (3-letter) codes
bool _languagesMatch(String? mpvLang, String? plexLang) {