fix(tracks): keep regional language variants distinct in exact selection

by_language now applies RFC 4647 Lookup in exact mode, preferring the string-equal tag so --exact-lang separates base/regional pairs (ar/ar-EG, en/en-US) instead of collapsing them, and dedupes by id to prevent the duplicate-track-id crash. Adds a language-selection regression test.
This commit is contained in:
imSp4rky
2026-07-14 09:17:22 -06:00
parent 1cf9e5b487
commit b8ec9e2498
4 changed files with 149 additions and 14 deletions
+4 -1
View File
@@ -1622,7 +1622,10 @@ class dl:
selected.append(descs[0])
else:
selected.append(candidates[0])
return selected
# Built via per-tag by_language calls, so overlapping tags can append
# the same track across iterations; dedupe by id.
seen_ids: set[str] = set()
return [t for t in selected if not (t.id in seen_ids or seen_ids.add(t.id))]
music_group_download = (
bool(music_titles)
+2 -6
View File
@@ -61,9 +61,7 @@ class Title:
returned dict with their specific fields (e.g., season/episode).
"""
primary_video_track = next(iter(media_info.video_tracks), None)
original_lang_tag = (
str(self.language).split("-")[0].lower() if self.language else ""
)
original_lang_tag = str(self.language).split("-")[0].lower() if self.language else ""
primary_audio_track = None
if original_lang_tag:
primary_audio_track = next(
@@ -170,9 +168,7 @@ class Title:
channel_count = primary_audio_track.channel_s or primary_audio_track.channels or 0
channels = float(channel_count)
has_atmos = any(
"JOC" in (t.format_additionalfeatures or "") or t.joc for t in media_info.audio_tracks
)
has_atmos = any("JOC" in (t.format_additionalfeatures or "") or t.joc for t in media_info.audio_tracks)
context.update(
{
+20 -7
View File
@@ -443,13 +443,26 @@ class Tracks:
tracks: list[TrackT], languages: list[str], per_language: int = 0, exact_match: bool = False
) -> list[TrackT]:
distance = LANGUAGE_EXACT_DISTANCE if exact_match else LANGUAGE_MAX_DISTANCE
selected = []
selected: list[TrackT] = []
seen_ids: set[str] = set()
for language in languages:
selected.extend(
[x for x in tracks if closest_supported_match(str(x.language), [language], distance)][
: per_language or None
]
)
matches = [x for x in tracks if closest_supported_match(str(x.language), [language], distance)]
if exact_match and len(matches) > 1:
# CLDR tag_distance measures intelligibility, not tag identity: it rates a base
# language and its "paradigm" regional variant as the same language (distance 0
# for ar/ar-EG, en/en-US, pt/pt-BR), so exact mode alone cannot separate them.
# Follow RFC 4647 Lookup: prefer the most specific (string-equal) tag, and fall
# back to the fuzzy match only when none exists (e.g. zh matches cmn).
want = Language.get(language).to_tag().casefold()
exact_hits = [x for x in matches if Language.get(str(x.language)).to_tag().casefold() == want]
if exact_hits:
matches = exact_hits
# Overlapping tags can still resolve to the same physical track; dedupe by id so
# callers never get duplicate tracks.
for track in matches[: per_language or None]:
if track.id not in seen_ids:
seen_ids.add(track.id)
selected.append(track)
return selected
def mux(
@@ -694,7 +707,7 @@ class Tracks:
log_event(
"mux_start",
level="INFO",
message=(f"Muxing {len(self.videos)}V/{len(self.audio)}A/{len(self.subtitles)}S " f"-> {output_path.name}"),
message=(f"Muxing {len(self.videos)}V/{len(self.audio)}A/{len(self.subtitles)}S -> {output_path.name}"),
context={
"title": title,
"output_path": str(output_path),