mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-02 14:05:15 +00:00
feat(hybrid): use a dv track that carries hdr10+ as the hybrid of its resolution
This commit is contained in:
+19
-12
@@ -235,6 +235,8 @@ def select_hybrid_bases(videos: Sequence[Video]) -> list[Video]:
|
||||
|
||||
def hybrid_gate_failure(videos: Sequence[Video], generate_dv: bool) -> Optional[tuple[str, str]]:
|
||||
"""The (message, detail) that stops HYBRID for these selected videos, or None when it can go on."""
|
||||
if any(v.native_hybrid for v in videos):
|
||||
return None
|
||||
hdr_tracks = [v for v in videos if v.range in (Video.Range.HDR10, Video.Range.HDR10P)]
|
||||
base_tracks = [v for v in hdr_tracks if v.codec in (Video.Codec.HEVC, None)]
|
||||
dv_tracks = [v for v in videos if v.range == Video.Range.DV]
|
||||
@@ -270,10 +272,16 @@ def hybrid_gate_failure(videos: Sequence[Video], generate_dv: bool) -> Optional[
|
||||
def hybrid_plan(videos: Sequence[Video], generate_dv: bool) -> tuple[list[Video], Optional[Video]]:
|
||||
"""The hybrid bases and the DV track to pair with them.
|
||||
|
||||
With no DV track, only HDR10+ bases qualify, and only with ``--generate-dv``.
|
||||
With no DV track, only HDR10+ bases qualify, and only with ``--generate-dv``. A resolution with a
|
||||
DV track that carries HDR10+ gets no base: that track is its hybrid.
|
||||
"""
|
||||
dv_tracks = [v for v in videos if v.range == Video.Range.DV]
|
||||
base_pool = [v for v in videos if dv_tracks or (generate_dv and v.range == Video.Range.HDR10P)]
|
||||
native_heights = {v.height for v in videos if v.native_hybrid}
|
||||
base_pool = [
|
||||
v
|
||||
for v in videos
|
||||
if v.height not in native_heights and (dv_tracks or (generate_dv and v.range == Video.Range.HDR10P))
|
||||
]
|
||||
matching_dv = min(dv_tracks, key=lambda v: v.height or 0) if dv_tracks else None
|
||||
return select_hybrid_bases(base_pool), matching_dv
|
||||
|
||||
@@ -2759,18 +2767,14 @@ class dl:
|
||||
title.tracks.videos, non_hybrid_ranges
|
||||
)
|
||||
|
||||
if not effective_quality:
|
||||
hybrid_quality = effective_quality
|
||||
if not hybrid_quality:
|
||||
best_resolution = max((v.height for v in hybrid_candidate_tracks), default=None)
|
||||
if best_resolution:
|
||||
hybrid_filter = title.tracks.select_hybrid(
|
||||
hybrid_candidate_tracks, [best_resolution], worst=worst
|
||||
)
|
||||
hybrid_selected = list(filter(hybrid_filter, hybrid_candidate_tracks))
|
||||
else:
|
||||
hybrid_selected = []
|
||||
else:
|
||||
hybrid_quality = [best_resolution] if best_resolution else []
|
||||
hybrid_selected = []
|
||||
if hybrid_quality:
|
||||
hybrid_filter = title.tracks.select_hybrid(
|
||||
hybrid_candidate_tracks, effective_quality, worst=worst
|
||||
hybrid_candidate_tracks, hybrid_quality, worst=worst
|
||||
)
|
||||
hybrid_selected = list(filter(hybrid_filter, hybrid_candidate_tracks))
|
||||
|
||||
@@ -3666,6 +3670,9 @@ class dl:
|
||||
hybrid_bases, matching_dv = hybrid_plan(title.tracks.videos, generate_dv)
|
||||
hybrid_base_ids.update(v.id for v in hybrid_bases)
|
||||
hybrids_made = 0
|
||||
for video in (v for v in original_videos if v.native_hybrid):
|
||||
self.log.info(f"The {video.height}p DV track carries HDR10+: it is the hybrid")
|
||||
hybrids_made += 1
|
||||
|
||||
for group in sorted(standalone_groups, key=group_file_size, reverse=True):
|
||||
mux_video_group(group)
|
||||
|
||||
@@ -41,6 +41,11 @@ MP4BOX_PROGRESS = re.compile(r"\((\d{1,3})/100\)")
|
||||
MP4BOX_OPEN_FAILED = re.compile(r"error while opening|Error opening file|Invalid IsoMedia File", re.I)
|
||||
|
||||
|
||||
def at_resolution(video: Video, res: int) -> bool:
|
||||
"""Whether a track has this height, or this height from a 16:9 width (a letterboxed encode)."""
|
||||
return video.height == res or bool(video.width and int(video.width * 9 / 16) == res)
|
||||
|
||||
|
||||
class Tracks:
|
||||
"""
|
||||
Video, Audio, Subtitle, Chapter, and Attachment Track Store.
|
||||
@@ -434,12 +439,27 @@ class Tracks:
|
||||
|
||||
A track that the caller also selected as an explicit deliverable (same track in both
|
||||
selections) stays unflagged, and the standalone mux loop muxes it alongside the hybrid.
|
||||
A ``native_hybrid`` track is the hybrid itself, so it stays unflagged too.
|
||||
"""
|
||||
for video in hybrid_selected:
|
||||
if video not in non_hybrid_selected:
|
||||
if video not in non_hybrid_selected and not video.native_hybrid:
|
||||
video.hybrid_base_only = True
|
||||
|
||||
def select_hybrid(self, tracks, quality, worst: bool = False):
|
||||
"""A filter for the hybrid ingredients of each resolution. It sets ``native_hybrid`` on every track.
|
||||
|
||||
A DV track that carries HDR10+ is already a hybrid, so its resolution takes that track and no base.
|
||||
"""
|
||||
pick = min if worst else max
|
||||
natives = {}
|
||||
for res in quality:
|
||||
candidates = [v for v in tracks if v.range == Video.Range.DV and v.hdr10plus and at_resolution(v, res)]
|
||||
if candidates:
|
||||
natives[res] = pick(candidates, key=lambda v: v.bitrate or 0)
|
||||
for video in tracks:
|
||||
video.native_hybrid = any(video is v for v in natives.values())
|
||||
quality = [res for res in quality if res not in natives]
|
||||
|
||||
# Prefer HDR10+ over HDR10 as the base layer (preserves dynamic metadata).
|
||||
# Only an HEVC base qualifies, because dovi_tool injects the RPU into an HEVC stream.
|
||||
base_ranges = (Video.Range.HDR10P, Video.Range.HDR10)
|
||||
@@ -450,24 +470,23 @@ class Tracks:
|
||||
for v in tracks
|
||||
if v.range == range_type
|
||||
and v.codec in (Video.Codec.HEVC, None)
|
||||
and (v.height in quality or (v.width and int(v.width * 9 / 16) in quality))
|
||||
and any(at_resolution(v, res) for res in quality)
|
||||
]
|
||||
if base_tracks:
|
||||
break
|
||||
|
||||
pick = min if worst else max
|
||||
base_selected = []
|
||||
for res in quality:
|
||||
candidates = [v for v in base_tracks if v.height == res or (v.width and int(v.width * 9 / 16) == res)]
|
||||
candidates = [v for v in base_tracks if at_resolution(v, res)]
|
||||
if candidates:
|
||||
chosen = pick(candidates, key=lambda v: v.bitrate)
|
||||
base_selected.append(chosen)
|
||||
|
||||
dv_tracks = [v for v in tracks if v.range == Video.Range.DV]
|
||||
lowest_dv = min(dv_tracks, key=lambda v: v.height) if dv_tracks else None
|
||||
lowest_dv = min(dv_tracks, key=lambda v: v.height) if dv_tracks and (base_selected or not natives) else None
|
||||
|
||||
def select(x):
|
||||
if x in base_selected:
|
||||
if x in base_selected or x.native_hybrid:
|
||||
return True
|
||||
if lowest_dv and x is lowest_dv:
|
||||
return True
|
||||
|
||||
@@ -303,6 +303,7 @@ class Video(Track):
|
||||
self.dv_compatible_bitstream = dv_compatible_bitstream
|
||||
self.hdr10plus = hdr10plus
|
||||
self.hybrid_base_only = False
|
||||
self.native_hybrid = False
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
data = super().to_dict()
|
||||
|
||||
Reference in New Issue
Block a user