fix(dl): make audio/video track selection consistent and crash-safe

Share one best-audio selector across the episode and music paths so best/all works for songs, descriptive audio is capped to one track, and codec_priority is honored. Guard --channels and 16:9 canvas math against tracks missing channel/width metadata. Extend --best-available to video languages and full resolution misses in both normal and hybrid mode, and count only deliverable ranges when reporting missing resolutions. Rank video by resolution before bitrate, and trim group audio selections to one track per song with a warning.

BREAKING CHANGE: a missing resolution with -r HYBRID now errors without --best-available, matching non-hybrid behavior.
This commit is contained in:
imSp4rky
2026-07-11 18:41:41 -06:00
parent 575f453592
commit 6434fc2081
4 changed files with 304 additions and 132 deletions
+8 -6
View File
@@ -244,11 +244,11 @@ class Tracks:
log.debug(f" - Found and skipped {duplicates} duplicate tracks...")
def sort_videos(self, by_language: Optional[Sequence[Union[str, Language]]] = None) -> None:
"""Sort video tracks by bitrate, and optionally language."""
"""Sort video tracks by resolution then bitrate, and optionally language."""
if not self.videos:
return
# bitrate
self.videos.sort(key=lambda x: float(x.bitrate or 0.0), reverse=True)
# resolution first, then bitrate (unknown-bitrate tracks still rank by resolution)
self.videos.sort(key=lambda x: (x.height or 0, float(x.bitrate or 0.0)), reverse=True)
# language
for language in reversed(by_language or []):
if str(language) in ("all", "best"):
@@ -396,7 +396,9 @@ class Tracks:
base_tracks = []
for range_type in base_ranges:
base_tracks = [
v for v in tracks if v.range == range_type and (v.height in quality or int(v.width * 9 / 16) in quality)
v
for v in tracks
if v.range == range_type and (v.height in quality or (v.width and int(v.width * 9 / 16) in quality))
]
if base_tracks:
break
@@ -404,7 +406,7 @@ class Tracks:
pick = min if worst else max
base_selected = []
for res in quality:
candidates = [v for v in base_tracks if v.height == res or int(v.width * 9 / 16) == res]
candidates = [v for v in base_tracks if v.height == res or (v.width and int(v.width * 9 / 16) == res)]
if candidates:
chosen = pick(candidates, key=lambda v: v.bitrate)
base_selected.append(chosen)
@@ -431,7 +433,7 @@ class Tracks:
]
if not matches:
matches = [ # 16:9 canvas matches
x for x in self.videos if int(x.width * (9 / 16)) == resolution
x for x in self.videos if x.width and int(x.width * (9 / 16)) == resolution
]
selected.extend(matches[: per_resolution or None])
self.videos = selected