feat(export): record each track's KIDs

An import reads them first, so a track gets its own keys without a probe.
This commit is contained in:
imSp4rky
2026-09-23 14:35:58 -06:00
parent 84bb07f589
commit 06866eb12e
5 changed files with 135 additions and 32 deletions
+5 -1
View File
@@ -48,7 +48,7 @@ from unshackle.core.console import GradientPulseBarColumn, SyncLive, console, li
from unshackle.core.constants import DOWNLOAD_CANCELLED, DOWNLOAD_LICENCE_ONLY, AnyTrack, context_settings
from unshackle.core.credential import Credential
from unshackle.core.downloaders import default_max_workers, format_speed, parse_speed_limit, set_speed_limit
from unshackle.core.drm import DRM_T, ClearKeyCENC, MonaLisa, PlayReady, Widevine, verify
from unshackle.core.drm import DRM_T, ClearKeyCENC, MonaLisa, PlayReady, Widevine, own_kids, verify
from unshackle.core.events import events
from unshackle.core.providers.anilist import parse_anilist_ref
from unshackle.core.providers.tvdb import SEASON_TYPES, parse_int
@@ -3970,9 +3970,13 @@ class dl:
tracks_map = entry.ext("unshackle").setdefault("tracks", {})
tracks_map.setdefault(str(track.id), track.to_dict())
# the row names the track's KIDs, so an import gives the track only their keys
track_kids = [kid.hex for kid in own_kids(drm)] if drm is not None else []
for row in entry.tracks:
if row.get("id") == str(track.id):
row["selected"] = True
if track_kids:
row["kids"] = list(dict.fromkeys([*(row.get("kids") or []), *track_kids]))
if drm is not None:
if hasattr(drm, "to_dict"):
+23 -1
View File
@@ -48,4 +48,26 @@ def drm_from_dict(data: dict[str, Any]) -> Union[Widevine, PlayReady, ClearKeyCE
return drm
__all__ = ("ClearKey", "ClearKeyCENC", "Widevine", "PlayReady", "MonaLisa", "DRM_T", "drm_from_dict")
def real_kids(drm: Any) -> list[UUID]:
"""Return the KIDs a DRM object names, without the all-zero and test-pattern placeholders."""
return [kid for kid in getattr(drm, "kids", None) or [] if kid.int and kid not in Widevine.PLACEHOLDER_KIDS]
def own_kids(drm: Any) -> list[UUID]:
"""Return the real KIDs that identify the track's own content key, as the DRM object knows them."""
own = getattr(drm, "own_kids", None)
kids = own() if callable(own) else getattr(drm, "kids", None) or []
return [kid for kid in kids if kid.int and kid not in Widevine.PLACEHOLDER_KIDS]
__all__ = (
"ClearKey",
"ClearKeyCENC",
"Widevine",
"PlayReady",
"MonaLisa",
"DRM_T",
"drm_from_dict",
"real_kids",
"own_kids",
)
+46 -22
View File
@@ -14,7 +14,7 @@ from langcodes import tag_is_valid
from unshackle.core.config import config
from unshackle.core.constants import AnyTrack
from unshackle.core.credential import Credential
from unshackle.core.drm import Widevine, drm_from_dict
from unshackle.core.drm import drm_from_dict, real_kids
from unshackle.core.manifests import DASH, HLS, ISM
from unshackle.core.remote_service import RemoteService, build_title, match_track, resolve_proxy_arg
from unshackle.core.titles import Episode, Movies, Series, Title_T, Titles_T, remap_titles
@@ -95,11 +95,6 @@ def resolve_import_manifest_data(
track.data.update(matched.data)
def real_kids(drm: Any) -> list[UUID]:
"""Return the KIDs a DRM object names, without the all-zero and test-pattern placeholders."""
return [kid for kid in getattr(drm, "kids", None) or [] if kid.int and kid not in Widevine.PLACEHOLDER_KIDS]
class ImportService:
"""Reconstructs a download from an export JSON.
@@ -417,34 +412,55 @@ class ImportService:
out.append({"system": system, "pssh_b64": d.pssh, "kids": list(entry.keys)})
return out
def exported_kids(self, title_id: str, track_id: str) -> Optional[set[str]]:
"""Return the KIDs (hex) the export's row for the track lists, or None when it lists none."""
entry = self.doc.get(title_id)
row = next((r for r in (entry.tracks if entry else []) if str(r.get("id", "")) == track_id), None)
return set(row["kids"]) if row and row.get("kids") else None
def exported_keys(self, title_id: str, track_id: str) -> dict[UUID, str]:
"""Return the exported content keys for the KIDs the row of the track lists, else all of the title."""
entry = self.doc.get(title_id)
return {UUID(hex=kid): key for kid, key in (entry.keys_for(track_id) if entry else {}).items()}
def rebuild_drm(self, track_dict: dict[str, Any], title_id: str) -> Optional[list[Any]]:
"""Rebuild a DRM object (from stored PSSH, falling back to a stub) with the exported keys.
Keys and DRM are title-level in the shared format; a legacy v2 track dict that still
carries its own is honoured first. A title with more than one DRM entry does not say
which entry is this track's, so the track gets a stub that names every exported KID
instead of a guess. A rebuilt PSSH holds only the keys for the KIDs it names.
carries its own is honoured first. The track's row in the export can list its KIDs;
then the track gets only their keys, and the DRM entry that names them. Without that
list, a title with more than one DRM entry does not say which entry is this track's,
so the track gets a stub that names every exported KID instead of a guess. A rebuilt
PSSH holds only the keys for the KIDs it names.
"""
own_keys = track_dict.get("keys")
keys = own_keys or {kid.hex: key for kid, key in self.key_pool(title_id).items()}
drm_dicts = track_dict.get("drm") or self.title_drm_dicts(title_id)
if not drm_dicts and not keys:
own_drm = track_dict.get("drm")
row_kids = None if own_keys else self.exported_kids(title_id, str(track_dict.get("id", "")))
if own_keys:
keys = own_keys
else:
keys = {kid.hex: key for kid, key in self.exported_keys(title_id, str(track_dict.get("id", ""))).items()}
drm_dicts = own_drm or self.title_drm_dicts(title_id)
if row_kids and not own_drm:
drm_dicts = [d for d in drm_dicts if row_kids & {str(kid).lower() for kid in d.get("kids") or []}]
if not drm_dicts and not keys and not row_kids:
return None
drm_obj = None
if drm_dicts and (track_dict.get("drm") or len(drm_dicts) == 1):
if drm_dicts and (own_drm or len(drm_dicts) == 1):
try:
drm_obj = drm_from_dict(drm_dicts[0])
except Exception as e:
self.log.debug(f"Falling back to DRM stub (PSSH rebuild failed: {e})")
if drm_obj is not None and not own_keys:
if drm_obj is not None and not own_keys and not row_kids:
declared = {kid.hex for kid in real_kids(drm_obj)}
if declared:
keys = {kid_hex: key for kid_hex, key in keys.items() if kid_hex in declared}
if drm_obj is None and keys:
stub_kids = list(keys) or sorted(row_kids or [])
if drm_obj is None and stub_kids:
drm_type = (drm_dicts[0].get("system", "Widevine").lower()) if drm_dicts else "widevine"
drm_obj = RemoteService.create_drm_stub(drm_type, list(keys.keys()))
drm_obj = RemoteService.create_drm_stub(drm_type, stub_kids)
if drm_obj is None:
return None
@@ -457,8 +473,10 @@ class ImportService:
"""Inject exported keys into the selected encrypted tracks by KID (no network).
dl.py calls this method after selection. It touches only encrypted video and audio
tracks. A track gets only the content keys for the KIDs it declares, and the whole
title pool only when it declares no KID. Encrypted DASH tracks (no DRM at parse time)
tracks. A track gets only the content keys for the KIDs it declares: first the KIDs
its row in the export lists, which needs no probe, then the KIDs of its DRM, manifest
or init segment. It gets the whole title pool only when it declares no KID. Encrypted
DASH tracks (no DRM at parse time)
get a stub holding the content keys, which DASH.download_track preserves. The
decrypters then match each content key to the media by KID, and give the all-zero KID
only the content key of the track.
@@ -468,14 +486,20 @@ class ImportService:
return
system = self.exported_drm_system()
title_id = str(title.id)
for track in title.tracks:
if not isinstance(track, (Video, Audio)):
continue
declared = self.track_kids(track, self.session)
if declared is None:
continue
keys = {kid: key for kid, key in pool.items() if kid in declared} if declared else pool
row_kids = self.exported_kids(title_id, str(track.id))
if row_kids:
declared: Optional[set[UUID]] = {UUID(hex=kid) for kid in row_kids}
keys = self.exported_keys(title_id, str(track.id))
else:
declared = self.track_kids(track, self.session)
if declared is None:
continue
keys = {kid: key for kid, key in pool.items() if kid in declared} if declared else pool
if track.drm:
drm_obj = track.drm[0]
else: