feat(tracks)!: match a language tag to the variant it means

A tag with a region (en-US, es-419) matches only that variant and reports a miss instead of taking another region. A bare tag takes the variant it means (pt -> pt-BR, es -> es-ES) and falls back to any variant only when that one is missing. `orig` prefers the title's own tag, `-sl orig` resolves, forced and SDH subtitles are picked from their own pools, and the track name no longer strips the region from the cached language.

BREAKING CHANGE: -l es-419 no longer selects es-ES, and language_priority, language_tags and muxing.default_language treat a regional tag as exact.
This commit is contained in:
imSp4rky
2026-10-15 12:26:39 -06:00
parent c231114cda
commit a4ce70a53c
26 changed files with 584 additions and 253 deletions
+13 -9
View File
@@ -5,7 +5,7 @@ from __future__ import annotations
import pytest
from langcodes import Language
from unshackle.core.utilities import excluded_language_tags, partition_exclusions
from unshackle.core.utilities import matched_language_tags, partition_exclusions
from unshackle.core.utils.click_types import LANGUAGE_RANGE
@@ -42,34 +42,38 @@ def test_partition_reads_the_tokens_language_range_produces():
def test_close_match_excludes_the_regional_variants():
assert excluded_language_tags(["es"], ["es-419", "es-ES", "en-US"]) == {"es-419", "es-ES"}
assert matched_language_tags(["es"], ["es-419", "es-ES", "en-US"], every_variant=True) == {"es-419", "es-ES"}
def test_selection_takes_only_the_variant_a_bare_tag_means():
assert matched_language_tags(["es"], ["es-419", "es-ES", "en-US"]) == {"es-ES"}
def test_exact_match_prefers_the_string_equal_tag():
# same string preference as selection: an exact 'es' only takes 'es' when it exists
assert excluded_language_tags(["es"], ["es", "es-ES", "es-419"], exact=True) == {"es"}
assert matched_language_tags(["es"], ["es", "es-ES", "es-419"], exact=True) == {"es"}
def test_exact_match_falls_back_to_the_paradigm_variant():
# no literal 'es' available, so 'es' resolves the way an exact '-sl es' selection would
assert excluded_language_tags(["es"], ["es-ES", "es-419"], exact=True) == {"es-ES"}
assert matched_language_tags(["es"], ["es-ES", "es-419"], exact=True) == {"es-ES"}
def test_exact_match_keeps_an_unnamed_regional_variant():
assert excluded_language_tags(["es-ES"], ["es-ES", "es-419"], exact=True) == {"es-ES"}
assert matched_language_tags(["es-ES"], ["es-ES", "es-419"], exact=True) == {"es-ES"}
def test_language_objects_are_accepted():
assert excluded_language_tags(["es"], [Language.get("es")]) == {"es"}
assert matched_language_tags(["es"], [Language.get("es")]) == {"es"}
def test_an_untagged_track_is_never_excluded():
assert excluded_language_tags(["es"], [None, "en-US"]) == set()
assert matched_language_tags(["es"], [None, "en-US"]) == set()
def test_no_exclusions_excludes_nothing():
assert excluded_language_tags([], ["es-419", "en-US"]) == set()
assert matched_language_tags([], ["es-419", "en-US"]) == set()
def test_an_unmatched_language_is_kept():
assert excluded_language_tags(["es", "fr"], ["en-US", "ja-JP"]) == set()
assert matched_language_tags(["es", "fr"], ["en-US", "ja-JP"]) == set()
+6
View File
@@ -20,6 +20,12 @@ def test_a_list_matches_any_of_its_entries() -> None:
assert evaluate_language_tag(rules, JA, []) == "ASIAN"
def test_a_regional_rule_does_not_match_another_region() -> None:
rules = [{"audio": "es-419", "tag": "LATAM"}]
assert evaluate_language_tag(rules, [Language.get("es-ES")], []) == ""
assert evaluate_language_tag(rules, [Language.get("es-419")], []) == "LATAM"
def test_subs_contain_all_needs_every_entry() -> None:
rules = [{"subs_contain_all": ["en", "fr"], "tag": "MULTI"}]
assert evaluate_language_tag(rules, JA, EN_ES) == ""
@@ -15,7 +15,9 @@ from unshackle.core.utilities import find_missing_langs
(["fil"], ["en", "ja"], ["fil"]),
(["fil", "ko"], ["en"], ["fil", "ko"]),
(["es"], ["es-419"], []),
(["es-419"], ["es"], []),
(["es-419"], ["es"], ["es-419"]),
(["es-419"], ["es-MX", "es-ES"], ["es-419"]),
(["en-US"], ["en-GB"], ["en-US"]),
(["en"], ["en-US"], []),
(["all"], [], []),
(["best"], [], []),
@@ -34,7 +36,7 @@ from unshackle.core.utilities import find_missing_langs
(["zh"], ["zh-Hant"], ["zh"]),
(["zh-Hans"], ["zh-Hant"], ["zh-Hans"]),
(["zh-CN"], ["zh-TW"], ["zh-CN"]),
(["zh-HK"], ["zh-Hant"], []),
(["zh-HK"], ["zh-Hant"], ["zh-HK"]),
(["zh"], ["cmn"], []),
(["cmn"], ["zh"], []),
(["zh"], ["yue"], ["zh"]),
@@ -1,25 +0,0 @@
from __future__ import annotations
import pytest
from unshackle.core.utilities import keep_forced_subtitle
@pytest.mark.parametrize(
"forced,language,forced_s_lang,exact,expected",
[
(False, "en", [], False, True),
(False, "de", ["en"], False, True),
(True, "en", [], False, False),
(True, "en", ["en"], False, True),
(True, "en-US", ["en"], False, True),
(True, "de", ["en"], False, False),
(True, "de", ["en", "de"], False, True),
# CLDR rates paradigm-regional pairs (en/en-US) distance 0, so exact still matches
(True, "en-US", ["en"], True, True),
(True, "en", ["en"], True, True),
(True, "es-419", ["es-ES"], True, False),
],
)
def test_keep_forced_subtitle(forced: bool, language: str, forced_s_lang: list[str], exact: bool, expected: bool):
assert keep_forced_subtitle(forced, language, forced_s_lang, exact) is expected
@@ -9,7 +9,7 @@ from __future__ import annotations
import inspect
import logging
from typing import Any, cast
from typing import Any, Optional, cast
import pytest
@@ -77,7 +77,13 @@ class FakeService:
raise AttributeError(name)
def run(monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, episodes: dict, **flags: Any) -> Any:
def run(
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
episodes: dict,
service: Optional[FakeService] = None,
**flags: Any,
) -> Any:
"""Return the Selected ids, the exit code, or None for a run that completes."""
monkeypatch.setattr(Tracks, "tree", lambda self, *a, **k: (_ for _ in ()).throw(Selected(self)))
monkeypatch.setattr("unshackle.core.providers.search_metadata", lambda *a, **k: None)
@@ -96,7 +102,7 @@ def run(monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, episo
kwargs.update(flags)
caplog.set_level(logging.WARNING, logger="test_kind_only")
try:
cmd.result(cast(Service, FakeService(episodes)), **kwargs)
cmd.result(cast(Service, service or FakeService(episodes)), **kwargs)
except Selected as sel:
return sel.ids
except SystemExit as exc:
@@ -4,7 +4,7 @@ from typing import Sequence
import pytest
from unshackle.core.utilities import matching_languages
from unshackle.core.utilities import ServiceTag, matching_languages
POOL = ["ar", "en", "en-GB", "en-US", "ja"]
@@ -12,12 +12,31 @@ POOL = ["ar", "en", "en-GB", "en-US", "ja"]
@pytest.mark.parametrize(
("language", "available", "exact", "expected"),
[
("en", POOL, False, {"en", "en-GB", "en-US"}),
# a bare tag takes the variant it means, and every regional form only when that is missing
("en", POOL, False, {"en"}),
("en", ["en-US", "en-GB"], False, {"en-US"}),
("pt", ["pt-PT", "pt-BR"], False, {"pt-BR"}),
("es", ["es-419", "es-ES"], False, {"es-ES"}),
("es", ["es-419", "es-MX"], False, {"es-419", "es-MX"}),
("es", ["es-419", "es-MX"], True, set()),
# CLDR rates en/en-US at distance 0, so exact mode leans on the RFC 4647 string preference
("en", POOL, True, {"en"}),
("en-GB", POOL, True, {"en-GB"}),
# no string-equal tag, so exact mode falls back to the distance match
("zh", ["ar", "cmn", "ja"], True, {"cmn"}),
# a region asks for that variant in either mode: never another region, a bare tag only
# when it means the same variant
("en-US", POOL, False, {"en-US"}),
("en-US", ["en", "en-GB"], False, {"en"}),
("en-US", ["en-GB", "en-AU"], False, set()),
("es-419", ["es-419", "es-MX", "es-ES"], False, {"es-419"}),
("es-419", ["es-MX", "es-ES"], False, set()),
("es-419", ["es", "es-ES"], False, set()),
("pt-BR", ["pt", "pt-PT"], False, {"pt"}),
("pt-PT", ["pt", "pt-BR"], False, set()),
# a script alone is not a region: zh-Hant means zh-TW, else any Traditional Chinese
("zh-Hant", ["zh-TW", "zh-HK", "zh-Hans"], False, {"zh-TW"}),
("zh-Hant", ["zh-HK", "zh-Hans"], False, {"zh-HK"}),
("en", ["ar", "ja"], False, set()),
("en", [], False, set()),
("en", ["ar", None, "en"], True, {"en"}),
@@ -31,3 +50,26 @@ POOL = ["ar", "en", "en-GB", "en-US", "ja"]
)
def test_matching_languages(language: str, available: Sequence[str], exact: bool, expected: set[str]) -> None:
assert matching_languages(language, available, exact) == expected
@pytest.mark.parametrize(
("title", "available", "expected"),
[
# the title's own variant beats the one a bare tag would mean
("fr-CA", ["fr-FR", "fr-CA"], {"fr-CA"}),
("en-GB", ["en-US", "en-GB"], {"en-GB"}),
("zh-HK", ["zh-TW", "zh-HK", "zh-CN"], {"zh-HK"}),
("sr-ME", ["sr", "sr-ME"], {"sr-ME"}),
# without its own variant, it takes the close regional forms
("en-US", ["en-GB", "fr"], {"en-GB"}),
("zh-TW", ["zh-HK", "zh-CN"], {"zh-HK"}),
],
)
def test_a_service_tag_prefers_its_own_variant(title: str, available: Sequence[str], expected: set[str]) -> None:
assert matching_languages(ServiceTag(title), available) == expected
def test_every_variant_returns_each_regional_form_of_a_bare_tag() -> None:
assert matching_languages("es", ["es-419", "es-ES", "en"], every_variant=True) == {"es-419", "es-ES"}
# a region still asks for one variant
assert matching_languages("es-ES", ["es-419", "es-ES"], every_variant=True) == {"es-ES"}
@@ -30,6 +30,9 @@ class FakeTitle:
# them and the flag still matches what a default 'orig' download selects
(["es"], ["es-419"], ["es-419"]),
(["fr"], ["fr-CA"], ["fr-CA"]),
# the service chose the title's region, so a different region still counts
(["en-US"], ["en-GB"], ["en-GB"]),
(["zh-HK"], ["zh-Hant", "zh-Hans"], ["zh-Hant"]),
# alias
(["zh"], ["cmn"], ["cmn"]),
# nothing in the original language, and no language on the title at all
@@ -0,0 +1,166 @@
"""Run regional language requests through the real dl.result() selection loop."""
from __future__ import annotations
from typing import Any
import pytest
from langcodes import Language
from tests.lang_selection import test_kind_only_selection as harness
from unshackle.core.titles import Episode
from unshackle.core.tracks import Audio, Subtitle, Tracks, Video
class RegionalService(harness.FakeService):
"""One episode in `title_lang`, with one video, one audio track per entry of `audio` and the given subtitles."""
def __init__(self, title_lang: str, audio: list[str], subs: list[tuple[str, str]]) -> None:
super().__init__({1: {}})
self.title_lang, self.audio, self.subs = title_lang, audio, subs
def get_titles_cached(self) -> Any:
title = super().get_titles_cached()
for episode in title:
episode.language = Language.get(self.title_lang)
return title
def get_tracks(self, title: Episode) -> Tracks:
tracks = Tracks()
lang = self.audio[0].split(":")[0]
tracks.add(Video(id_="v", url="https://x/v", language=lang, codec=Video.Codec.AVC, range_=Video.Range.SDR))
for entry in self.audio:
# "es-ES:EC3" gives the track a codec other than AAC
lang, _, codec = entry.partition(":")
tracks.add(
Audio(id_=entry, url=f"https://x/{entry}", language=lang, codec=Audio.Codec[codec or "AAC"], bitrate=1)
)
for lang, kind in self.subs:
sdh, forced = kind == "sdh", kind == "forced"
sub_id = f"{lang}{'-' + kind if kind else ''}"
tracks.add(
Subtitle(
id_=sub_id,
url=f"https://x/{sub_id}",
language=lang,
codec=Subtitle.Codec.WebVTT,
sdh=sdh,
forced=forced,
)
)
return tracks
def run(
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
service: RegionalService,
**flags: Any,
) -> Any:
return harness.run(monkeypatch, caplog, {1: {}}, service=service, **flags)
def test_orig_takes_another_region_of_the_title_language(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
ids = run(monkeypatch, caplog, RegionalService("en-US", ["en-GB", "fr-FR"], []))
assert ids["audio"] == ["en-GB"]
assert ids["video"] == ["v"]
def test_orig_keeps_the_script_the_title_region_implies(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
ids = run(monkeypatch, caplog, RegionalService("zh-TW", ["zh-HK", "zh-CN"], []))
assert ids["audio"] == ["zh-HK"]
def test_a_regional_audio_request_misses_rather_than_take_a_sibling(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
assert run(monkeypatch, caplog, RegionalService("fr-FR", ["fr-CA"], []), a_lang=["fr-FR"]) == 1
assert "fr-FR not found in audio tracks" in caplog.text
def test_a_regional_sdh_track_is_stripped_when_only_a_sibling_is_plain(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
service = RegionalService("en-US", ["en-US"], [("en-US", "sdh"), ("en-GB", "")])
ids = run(monkeypatch, caplog, service, s_lang=["en-US"])
assert sorted(ids["subs"]) == ["en-US-sdh", "en-US-sdh_stripped"]
def test_a_forced_track_does_not_hide_the_plain_track_a_region_means(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
service = RegionalService("ko", ["ko"], [("es", ""), ("es-ES", "forced"), ("fr", "")])
assert run(monkeypatch, caplog, service, s_lang=["es-ES"])["subs"] == ["es"]
def test_a_bare_sdh_track_is_stripped_when_only_another_variant_is_plain(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
service = RegionalService("en", ["en"], [("en", "sdh"), ("en-GB", "")])
assert sorted(run(monkeypatch, caplog, service, s_lang=["en"])["subs"]) == ["en-sdh", "en-sdh_stripped"]
def test_a_bare_audio_request_takes_the_variant_it_means(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
service = RegionalService("en", ["en", "pt-PT", "pt-BR"], [])
assert run(monkeypatch, caplog, service, a_lang=["pt"])["audio"] == ["pt-BR"]
def test_orig_takes_the_title_variant_over_the_one_a_bare_tag_means(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
assert run(monkeypatch, caplog, RegionalService("fr-CA", ["fr-FR", "fr-CA", "en"], []))["audio"] == ["fr-CA"]
@pytest.mark.parametrize(
("subs", "flags", "expected"),
[
# -fs keeps a forced track that -sl names, even when no plain track has its language
([("en", "forced")], {"s_lang": ["en"], "forced_subs": True}, ["en-forced"]),
([("es", ""), ("es-ES", "forced")], {"s_lang": ["es-ES"], "forced_subs": True}, ["es", "es-ES-forced"]),
# an SDH track cannot take the plain variant's place, and joins it as the same variant
([("en-US", ""), ("en", "sdh")], {"s_lang": ["en"]}, ["en-US", "en-sdh"]),
# -fsl matches across every forced track, so the result does not depend on -sl
([("es-ES", "forced"), ("es-419", "forced")], {"forced_s_lang": ["es"]}, ["es-ES-forced"]),
(
[("fr", ""), ("es-ES", "forced"), ("es-419", "forced")],
{"s_lang": ["fr"], "forced_s_lang": ["es"]},
["es-ES-forced", "fr"],
),
# -sl orig resolves to the title language
([("en-GB", ""), ("en-US", "")], {"s_lang": ["orig"]}, ["en-GB"]),
],
)
def test_subtitle_selection(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, subs: list, flags: dict, expected: list
) -> None:
ids = run(monkeypatch, caplog, RegionalService("en-GB", ["ja"], subs), lang=["ja"], **flags)
assert sorted(ids["subs"]) == expected
def test_a_forced_only_language_without_fs_is_reported_missing(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
service = RegionalService("en", ["ja"], [("en", ""), ("es-419", ""), ("es-ES", "forced")])
assert run(monkeypatch, caplog, service, lang=["ja"], s_lang=["en", "es-ES"]) == 1
assert "es-ES not found in subtitle tracks" in caplog.text
def test_an_exclusion_removes_every_regional_form(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
service = RegionalService("ja", ["ja", "es-419", "es-ES"], [])
assert run(monkeypatch, caplog, service, a_lang=["all", "-es"])["audio"] == ["ja"]
def test_the_meant_variant_does_not_depend_on_codec(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
service = RegionalService("ja", ["ja", "es-ES:EC3", "es-419"], [])
ids = run(monkeypatch, caplog, service, a_lang=["es"], acodec=[Audio.Codec.AAC, Audio.Codec.EC3])
assert ids["audio"] == ["es-ES:EC3"]
+9 -2
View File
@@ -46,10 +46,12 @@ def test_orig_resolves_to_the_title_language():
assert missing_required_langs(["orig"], ["en"], None) == []
def test_exact_lang_separates_a_regional_variant_from_its_base():
def test_a_regional_tag_never_matches_another_region():
assert missing_required_langs(["en-US"], ["en-US"], exact=True) == []
assert missing_required_langs(["en-US"], ["en-GB"], exact=True) == ["en-US"]
assert missing_required_langs(["en-US"], ["en-GB"]) == []
assert missing_required_langs(["en-US"], ["en-GB"]) == ["en-US"]
# 'orig' is the service's tag, so its region does not narrow the match
assert missing_required_langs(["orig"], ["en-GB"], "en-US") == []
def test_all_and_best_tokens_require_nothing():
@@ -150,3 +152,8 @@ def test_all_three_require_flags_share_one_gate():
for kind in ("audio", "video", "subtitle"):
assert f'"{kind}",' in source
assert "missing_required_langs(required, available, title.language, exact=exact_lang)" in source
def test_exact_lang_turns_off_the_bare_tag_fallback():
assert missing_required_langs(["en"], ["en-GB"]) == []
assert missing_required_langs(["en"], ["en-GB"], exact=True) == ["en"]
@@ -5,7 +5,7 @@ from typing import Optional, Sequence
import pytest
from langcodes import Language
from unshackle.core.utilities import resolve_sort_langs
from unshackle.core.utilities import ServiceTag, resolve_sort_langs
@pytest.mark.parametrize(
@@ -13,7 +13,9 @@ from unshackle.core.utilities import resolve_sort_langs
[
(["orig", "en", "es"], "ja", ["ja", "en", "es"]),
(["orig", "en", "es"], None, ["en", "es"]),
# orig keeps the title's full tag; matching_languages handles its fallback
(["orig"], Language.get("pt-BR"), ["pt-BR"]),
(["orig"], "zh-Hant-TW", ["zh-Hant-TW"]),
(["en", "orig"], "en", ["en"]),
(["en", "es", "en", "fr"], None, ["en", "es", "fr"]),
(["all", "best", "en"], "ja", ["all", "best", "en"]),
@@ -22,3 +24,8 @@ from unshackle.core.utilities import resolve_sort_langs
)
def test_resolve_sort_langs(tokens: Sequence[str], original: Optional[str], expected: list[str]) -> None:
assert resolve_sort_langs(tokens, original) == expected
def test_orig_is_a_service_tag_outside_exact_mode() -> None:
assert isinstance(resolve_sort_langs(["orig"], "pt-BR")[0], ServiceTag)
assert not isinstance(resolve_sort_langs(["orig"], "pt-BR", exact=True)[0], ServiceTag)
+26
View File
@@ -0,0 +1,26 @@
from __future__ import annotations
from typing import Optional
import pytest
from unshackle.core.tracks import Audio, Tracks
@pytest.mark.parametrize(
("languages", "preferred", "expected"),
[
(["en-GB", "en-US"], "en-US", 1),
# matched against every track at once, so the string-equal tag wins over a bare en
(["en", "en-US"], "en-US", 1),
(["en-GB", "fr"], "en-US", None),
(["fr", "en-GB"], "en", 1),
(["en-US"], None, None),
],
)
def test_first_match_index(languages: list[str], preferred: Optional[str], expected: Optional[int]) -> None:
tracks = [
Audio(id_=f"a{i}", url=f"https://x/{i}", language=lang, codec=Audio.Codec.AAC, bitrate=1)
for i, lang in enumerate(languages)
]
assert Tracks.first_match_index(tracks, preferred) == expected
+47 -4
View File
@@ -114,7 +114,7 @@ def test_language_grouping_still_honours_type_priority_and_sections(tracks: Trac
def test_language_grouping_keeps_regional_variants_adjacent_through_sections() -> None:
# "en" floats en-GB and en-US as one block, so each exact tag keeps its variants
# "en" floats en-US (the variant it means) and then en-GB, and each tag keeps its variants
t = Tracks()
t.add(
[
@@ -129,11 +129,11 @@ def test_language_grouping_keeps_regional_variants_adjacent_through_sections() -
)
t.sort_subtitles(by_language=["en"], group_by="language")
assert shape(t) == [
("en-US", "normal"),
("en-US", "sdh"),
("en-GB", "forced"),
("en-GB", "normal"),
("en-GB", "sdh"),
("en-US", "normal"),
("en-US", "sdh"),
("fi", "normal"),
("fi", "sdh"),
]
@@ -172,7 +172,15 @@ def test_priority_overlapping_entries_are_harmless() -> None:
t = Tracks()
t.add([sub("ar"), sub("en-GB"), sub("en-US"), sub("es"), sub("ja")])
t.sort_subtitles(by_language=["en", "en-US", "es"], group_by="language")
assert langs(t) == ["en-GB", "en-US", "es", "ar", "ja"]
assert langs(t) == ["en-US", "en-GB", "es", "ar", "ja"]
@pytest.mark.parametrize("group_by", ["language", "type"])
def test_a_regional_priority_outranks_its_sibling(group_by: str) -> None:
t = Tracks()
t.add([sub("en-GB"), sub("en-US"), sub("fr")])
t.sort_subtitles(by_language=["en-US"], group_by=group_by)
assert langs(t) == ["en-US", "en-GB", "fr"]
def test_no_priority_leaves_order_unchanged(many: Tracks) -> None:
@@ -223,3 +231,38 @@ def test_unparseable_priority_entry_is_ignored(many: Tracks, exact: bool) -> Non
# a config typo in language_priority must not crash the sort
many.sort_subtitles(by_language=["en", "engrish", "f", ""], group_by="language", exact_match=exact)
assert langs(many) == ["en", "ar", "bg", "es", "fr", "ja"]
def test_a_bare_audio_priority_sorts_the_meant_variant_then_its_other_forms() -> None:
from unshackle.core.tracks import Audio
t = Tracks()
t.add(
[
Audio(id_=lang, url=f"https://x/{lang}", language=lang, codec=Audio.Codec.AAC, bitrate=bitrate)
for lang, bitrate in (("en", 3), ("es-419", 2), ("es-ES", 1))
]
)
t.sort_audio(by_language=["es"])
assert [str(a.language) for a in t.audio] == ["es-ES", "es-419", "en"]
def test_all_sorts_the_original_track_then_its_other_regional_forms() -> None:
from unshackle.core.tracks import Audio
t = Tracks()
t.add(
[
Audio(
id_=lang,
url=f"https://x/{lang}",
language=lang,
codec=Audio.Codec.AAC,
bitrate=bitrate,
is_original_lang=orig,
)
for lang, bitrate, orig in (("en", 3, False), ("fr-FR", 2, False), ("fr-CA", 1, True))
]
)
t.sort_audio(by_language=["all"])
assert [str(a.language) for a in t.audio] == ["fr-CA", "fr-FR", "en"]
+19
View File
@@ -0,0 +1,19 @@
from langcodes import Language
from unshackle.core.tracks import Audio, Tracks
def audio(lang: str, bitrate: int, id_: str) -> Audio:
return Audio(url=f"http://x/{id_}", id_=id_, language=lang, codec=Audio.Codec.AAC, bitrate=bitrate, channels=2.0)
def test_naming_a_track_keeps_its_region() -> None:
track = audio("fr-FR", 128000, "a1")
assert track.name is None
assert track.language.territory == "FR"
assert Language.get("fr-FR").territory == "FR"
def test_a_regional_request_picks_its_own_variant_over_a_higher_bitrate_sibling() -> None:
tracks = [audio("fr-FR", 128000, "a1"), audio("fr-CA", 192000, "a2")]
assert [str(t.language) for t in Tracks.by_language(tracks, ["fr-FR"])] == ["fr-FR"]