feat(dl): let --worst run without -q and pick the lowest bitrate

--worst picks the lowest bitrate at each -q resolution, or at the lowest resolution when no -q is set. Hybrid mode still requires -q.

Thanks to reel.deal for the report and the first patch.
This commit is contained in:
imSp4rky
2026-11-15 15:14:14 -07:00
parent 3522de1e9c
commit 511db0c768
7 changed files with 147 additions and 18 deletions
+42 -1
View File
@@ -221,10 +221,51 @@ def test_worst_prefers_the_2d_track_at_one_height(tmp_path: Path) -> None:
ranked.sort_videos()
assert pick_video(ranked.videos, worst=True) is videos[1]
assert pick_video(ranked.videos, worst=False) is videos[0]
assert pick_video([videos[3], videos[2]], worst=True) is videos[2]
assert pick_video([videos[2], videos[3]], worst=True) is videos[3]
assert pick_video([], worst=True) is None
def test_worst_ranks_unknown_bitrates_and_sizes_highest(tmp_path: Path) -> None:
def ranked(*specs: tuple[bool, int, Any]) -> list[Video]:
tracks = Tracks([stereo_video(tmp_path, stereo=s, width=h * 16 // 9, height=h, bitrate=b) for s, h, b in specs])
tracks.sort_videos()
return tracks.videos
tie = ranked((False, 720, 500_000), (False, 480, 500_000))
assert pick_video(tie, worst=True) is tie[1]
unknown = ranked((False, 1080, 900_000), (False, 1080, 0), (False, 1080, None))
assert pick_video(unknown, worst=True) is unknown[0]
none_known = ranked((False, 1080, 0), (False, 1080, None))
assert pick_video(none_known, worst=True) is none_known[-1]
sizeless = stereo_video(tmp_path, stereo=False, bitrate=50_000)
sized = ranked((False, 720, 900_000))[0]
assert pick_video([sized, sizeless], worst=True) is sized
with_3d = ranked((False, 1080, 5_000_000), (True, 1080, 1_000_000))
assert pick_video(with_3d, worst=True) is with_3d[0]
def test_worst_ignores_the_language_order() -> None:
def rendition(language: str, bitrate: int) -> Video:
return Video(
url=f"https://x/{language}.mp4",
language=language,
codec=Video.Codec.HEVC,
width=1920,
height=1080,
bitrate=bitrate,
)
lower, higher = rendition("en-AU", 3_000_000), rendition("en-GB", 6_000_000)
tracks = Tracks([lower, higher])
tracks.sort_videos(by_language=["en"])
candidates = Tracks.by_language(tracks.videos, ["en"])
assert candidates == [lower, higher]
assert pick_video(candidates, worst=True) is lower
def test_hybrid_never_pairs_a_3d_dv_track(tmp_path: Path) -> None:
base = stereo_video(tmp_path, stereo=False, range_=Video.Range.HDR10, height=2160)
layered = stereo_video(tmp_path, stereo=True, range_=Video.Range.DV, height=2160)
+73
View File
@@ -0,0 +1,73 @@
"""Run --worst through the real dl.result() selection loop."""
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),
]
class LadderService(harness.FakeService):
"""The harness episode with the video ladder above; a track with no bitrate is never the worst."""
def __init__(self) -> None:
super().__init__({1: {}})
def get_tracks(self, title: Episode) -> Tracks:
tracks = super().get_tracks(title)
tracks.videos = [
Video(
id_=id_,
url=f"https://x/{id_}",
language="en",
codec=Video.Codec.AVC,
range_=Video.Range.SDR,
width=width,
height=height,
bitrate=bitrate,
)
for id_, width, height, bitrate in LADDER
]
return tracks
@pytest.mark.parametrize(
("flags", "expected"),
[
({}, ["240-lo"]),
({"quality": [1080]}, ["1080-lo"]),
({"quality": [1080, 540]}, ["1080-lo", "540"]),
({"quality": [2160], "best_available": True}, ["240-lo"]),
],
)
def test_worst_picks_the_lowest_bitrate_of_the_quality_or_of_the_lowest_resolution(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, flags: dict[str, Any], expected: list[str]
) -> None:
ids = harness.run(monkeypatch, caplog, {1: {}}, service=LadderService(), worst=True, **flags)
assert ids["video"] == expected
def test_worst_in_hybrid_mode_needs_a_quality(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> 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