fix(tracks): rank video picks by quality across regional tags and in hybrid

One video language request that matches several regional tags now gives one video, the best of them; hybrid bases and the DV source use the same pick, and hybrid --worst no longer needs -q. Thanks to reel.deal for the report and the first patch.
This commit is contained in:
imSp4rky
2026-11-15 15:54:21 -07:00
parent 511db0c768
commit 270662e9b2
11 changed files with 363 additions and 143 deletions
+2 -1
View File
@@ -11,7 +11,7 @@ from typing import Any
import pytest
from langcodes import Language
from unshackle.commands.dl import dl, hybrid_plan, pick_video
from unshackle.commands.dl import dl, hybrid_plan
from unshackle.core import binaries
from unshackle.core.config import config
from unshackle.core.drm.verify import decrypter_for
@@ -19,6 +19,7 @@ from unshackle.core.manifests import DASH, HLS
from unshackle.core.titles.movie import Movie
from unshackle.core.tracks import Audio, Chapter, Chapters, Track, Tracks, Video
from unshackle.core.tracks import tracks as tracks_module
from unshackle.core.tracks.tracks import pick_video
VIDEO_IDS = itertools.count()
+35 -21
View File
@@ -2,33 +2,32 @@
from __future__ import annotations
from pathlib import Path
from typing import Any
import pytest
from tests.lang_selection import test_kind_only_selection as harness
from unshackle.core import binaries
from unshackle.core.titles import Episode
from unshackle.core.tracks import Tracks, Video
LADDER = [
("1080-hi", 1920, 1080, 6_000_000),
("1080-lo", 1920, 1080, 3_000_000),
("1080-unknown", 1920, 1080, None),
("540", 960, 540, 900_000),
("270-lo", 480, 270, 79_000),
("240-hi", 320, 240, 153_000),
("240-lo", 320, 240, 140_000),
("240-unknown", 320, 240, None),
("1080-hi", Video.Range.SDR, 1920, 1080, 6_000_000),
("1080-lo", Video.Range.SDR, 1920, 1080, 3_000_000),
("1080-unknown", Video.Range.SDR, 1920, 1080, None),
("540", Video.Range.SDR, 960, 540, 900_000),
("270-lo", Video.Range.SDR, 480, 270, 79_000),
("240-hi", Video.Range.SDR, 320, 240, 153_000),
("240-lo", Video.Range.SDR, 320, 240, 140_000),
("240-unknown", Video.Range.SDR, 320, 240, None),
]
class LadderService(harness.FakeService):
"""The harness episode with the video ladder above; a track with no bitrate is never the worst."""
"""The harness episode with a video ladder of one codec; a track with no bitrate is never the worst."""
def __init__(self) -> None:
def __init__(self, ladder: list[tuple[Any, ...]] = LADDER, codec: Video.Codec = Video.Codec.AVC) -> None:
super().__init__({1: {}})
self.ladder, self.codec = ladder, codec
def get_tracks(self, title: Episode) -> Tracks:
tracks = super().get_tracks(title)
@@ -37,13 +36,13 @@ class LadderService(harness.FakeService):
id_=id_,
url=f"https://x/{id_}",
language="en",
codec=Video.Codec.AVC,
range_=Video.Range.SDR,
codec=self.codec,
range_=range_,
width=width,
height=height,
bitrate=bitrate,
)
for id_, width, height, bitrate in LADDER
for id_, range_, width, height, bitrate in self.ladder
]
return tracks
@@ -64,10 +63,25 @@ def test_worst_picks_the_lowest_bitrate_of_the_quality_or_of_the_lowest_resoluti
assert ids["video"] == expected
def test_worst_in_hybrid_mode_needs_a_quality(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
HDR_LADDER = [
("hdr-2160", Video.Range.HDR10, 3840, 2160, 15_000_000),
("hdr-1080-hi", Video.Range.HDR10, 1920, 1080, 6_000_000),
("hdr-1080-lo", Video.Range.HDR10, 1920, 1080, 4_000_000),
("hdr-1080-unknown", Video.Range.HDR10, 1920, 1080, None),
("dv-1080", Video.Range.DV, 1920, 1080, 5_000_000),
("dv-540", Video.Range.DV, 960, 540, 1_000_000),
]
@pytest.mark.parametrize(
"flags",
[{}, {"quality": [1080]}, {"quality": [4320], "best_available": True}],
ids=["no-quality", "quality", "best-available"],
)
def test_worst_in_hybrid_mode_takes_the_lowest_bitrate_base_of_the_lowest_base_height(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, flags: dict[str, Any]
) -> None:
monkeypatch.setattr(binaries, "DoviTool", Path("dovi_tool"))
code = harness.run(monkeypatch, caplog, {1: {}}, service=LadderService(), worst=True, range_=[Video.Range.HYBRID])
assert code == 1
assert "--worst requires -q/--quality to be specified in hybrid mode" in caplog.text
harness.stub_hybrid(monkeypatch)
service = LadderService(HDR_LADDER, Video.Codec.HEVC)
ids = harness.run(monkeypatch, caplog, {1: {}}, service=service, worst=True, range_=[Video.Range.HYBRID], **flags)
assert ids["video"] == ["hdr-1080-lo", "dv-540"]
@@ -9,12 +9,14 @@ from __future__ import annotations
import inspect
import logging
from pathlib import Path
from typing import Any, Optional, cast
import pytest
import requests
from unshackle.commands.dl import dl
from unshackle.core import binaries
from unshackle.core.api.handlers import DEFAULT_DOWNLOAD_PARAMS
from unshackle.core.constants import DOWNLOAD_LICENCE_ONLY
from unshackle.core.service import Service
@@ -81,6 +83,15 @@ class FakeService:
raise AttributeError(name)
def stub_hybrid(monkeypatch: pytest.MonkeyPatch) -> None:
"""Let a hybrid run start without dovi_tool, and load the service's tracks with no probes."""
monkeypatch.setattr(binaries, "DoviTool", Path("/unused/dovi_tool"), raising=False)
monkeypatch.setattr(
"unshackle.commands.dl.load_tracks",
lambda service, title, **_: setattr(title, "tracks", service.get_tracks(title)),
)
def run(
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
@@ -8,6 +8,7 @@ import pytest
from langcodes import Language
from tests.lang_selection import test_kind_only_selection as harness
from unshackle.commands.dl import hybrid_plan
from unshackle.core.titles import Episode
from unshackle.core.tracks import Audio, Subtitle, Tracks, Video
@@ -197,3 +198,185 @@ def test_the_meant_variant_does_not_depend_on_codec(
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"]
class RegionalVideoService(RegionalService):
"""A RegionalService whose one video is replaced by `videos`: SDR AVC tracks given as (id, language, height, kbps)."""
def __init__(self, title_lang: str, videos: list[tuple[Any, ...]]) -> None:
super().__init__(title_lang, [title_lang], [])
self.videos = videos
def get_tracks(self, title: Episode) -> Tracks:
tracks = super().get_tracks(title)
tracks.videos = [self.video(*row) for row in self.videos]
return tracks
def video(self, *row: Any) -> Video:
id_, lang, height, kbps = row
return Video(
id_=id_,
url=f"https://x/{id_}",
language=lang,
codec=Video.Codec.AVC,
range_=Video.Range.SDR,
width=height * 16 // 9,
height=height,
bitrate=kbps * 1000,
)
# en means en-US, and the title has none, so a bare en or orig request takes both regional forms
MIXED = [("au480", "en-AU", 480, 900), ("gb720", "en-GB", 720, 2000)]
SAME_HEIGHT = [("au720", "en-AU", 720, 900), ("gb720", "en-GB", 720, 2000)]
@pytest.mark.parametrize("request_", [{"v_lang": ["en"]}, {}], ids=["vl-en", "orig"])
@pytest.mark.parametrize(
("videos", "flags", "expected"),
[
(MIXED, {}, "gb720"),
(MIXED, {"quality": [1], "worst": True, "best_available": True}, "au480"),
(SAME_HEIGHT, {"quality": [720]}, "gb720"),
(SAME_HEIGHT, {"quality": [720], "worst": True}, "au720"),
],
ids=["best", "lowest-resolution", "best-at-720", "worst-at-720"],
)
def test_the_regional_forms_of_one_request_rank_as_one_language(
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
videos: list,
flags: dict,
expected: str,
request_: dict,
) -> None:
ids = run(monkeypatch, caplog, RegionalVideoService("en", videos), **request_, **flags)
assert ids["video"] == [expected]
@pytest.mark.parametrize(
("v_lang", "expected"),
[
(["en-AU"], ["au480"]),
(["en-GB", "en-AU"], ["gb720", "au480"]),
(["en-AU", "en-GB"], ["au480", "gb720"]),
(["all"], ["gb720", "au480"]),
],
)
def test_regional_tags_and_several_video_languages_keep_their_selection(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, v_lang: list, expected: list
) -> None:
assert run(monkeypatch, caplog, RegionalVideoService("en", MIXED), v_lang=v_lang)["video"] == expected
@pytest.mark.parametrize(
("flags", "expected"),
[
({"v_lang": ["en", "fr"]}, ["gb720", "fr720"]),
({"v_lang": ["en", "orig"]}, ["gb720"]),
({"v_lang": ["en", "de"], "best_available": True}, ["gb720"]),
],
ids=["two-languages", "orig-is-en", "missing-language-dropped"],
)
def test_each_video_request_gives_one_video_across_its_regional_tags(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, flags: dict, expected: list
) -> None:
service = RegionalVideoService("en", [*MIXED, ("fr720", "fr-FR", 720, 1500)])
assert run(monkeypatch, caplog, service, **flags)["video"] == expected
class HdrRegionalService(RegionalVideoService):
"""A RegionalVideoService of HEVC videos given as (id, language, range, width, height, kbps)."""
def video(self, *row: Any) -> Video:
id_, lang, range_, width, height, kbps = row
return Video(
id_=id_,
url=f"https://x/{id_}",
language=lang,
codec=Video.Codec.HEVC,
range_=Video.Range[range_],
width=width,
height=height,
bitrate=kbps * 1000,
)
class SelectedWithStandalone(harness.Selected):
def __init__(self, tracks: Tracks) -> None:
super().__init__(tracks)
self.ids["standalone"] = [t.id for t in tracks.videos if not t.hybrid_base_only]
def run_hdr(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, videos: list, ranges: list, **flags: Any
) -> Any:
harness.stub_hybrid(monkeypatch)
monkeypatch.setattr(harness, "Selected", SelectedWithStandalone)
service = HdrRegionalService("en", videos)
range_ = [Video.Range[r] for r in ranges]
return run(monkeypatch, caplog, service, quality=[2160], v_lang=["en"], range_=range_, **flags)
def test_hybrid_takes_the_best_dv_source_of_the_lowest_height_across_regional_tags(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
videos = [
("hdr", "en-AU", "HDR10", 3840, 2160, 8000),
("dv-au", "en-AU", "DV", 1920, 1080, 2000),
("dv-gb", "en-GB", "DV", 1920, 1080, 6000),
]
assert run_hdr(monkeypatch, caplog, videos, ["HYBRID"])["video"] == ["hdr", "dv-gb"]
def test_generated_dv_takes_the_best_base_across_regional_tags(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
videos = [("hdr-au", "en-AU", "HDR10", 3840, 1600, 8000), ("hdr-gb", "en-GB", "HDR10", 3840, 2160, 8000)]
assert run_hdr(monkeypatch, caplog, videos, ["HDR10"], generate_dv=True)["video"] == ["hdr-gb"]
@pytest.mark.parametrize(
("videos", "expected"),
[
(
[
("hdr-au", "en-AU", "HDR10", 3840, 1600, 8000),
("hdr-gb", "en-GB", "HDR10", 3840, 2160, 8000),
("dv", "en-AU", "DV", 1920, 1080, 2000),
],
["hdr-gb"],
),
(
[
("hdr10p", "en-AU", "HDR10P", 3840, 2160, 8000),
("hdr-crop", "en-AU", "HDR10", 3840, 1600, 9000),
("dv", "en-AU", "DV", 1920, 1080, 2000),
],
["hdr10p"],
),
],
ids=["regional-tags", "standalone-at-another-height"],
)
def test_a_standalone_range_beside_hybrid_does_not_add_a_hybrid_base(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, videos: list, expected: list
) -> None:
class Planned(BaseException):
def __init__(self, videos: list[Video], generate_dv: bool) -> None:
self.base_ids = [v.id for v in hybrid_plan(videos, generate_dv)[0]]
def stop_at_plan(videos: list[Video], generate_dv: bool) -> None:
raise Planned(videos, generate_dv)
monkeypatch.setattr("unshackle.commands.dl.hybrid_plan", stop_at_plan)
monkeypatch.setattr(Video, "normalize_vui", lambda self: None)
with pytest.raises(Planned) as planned:
run_hdr(monkeypatch, caplog, videos, ["HYBRID", "HDR10"], to_end=True, video_only=True)
assert planned.value.base_ids == expected
def test_the_fallback_from_a_failed_hybrid_ranks_regional_tags_as_one_language(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
videos = [("sdr-au", "en-AU", "SDR", 3840, 2160, 3000), ("sdr-gb", "en-GB", "SDR", 3840, 2160, 9000)]
assert run_hdr(monkeypatch, caplog, videos, ["HYBRID", "SDR"], best_available=True)["video"] == ["sdr-gb"]