fix(import): keep the FairPlay bridged mark and every KID of a DRM through an export

This commit is contained in:
imSp4rky
2026-12-01 21:46:44 -07:00
parent 1288a8e9b7
commit 992fff3626
5 changed files with 124 additions and 18 deletions
-1
View File
@@ -379,7 +379,6 @@ def handle_single_server_cdm(
from unshackle.core.cdm import load_cdm
from unshackle.core.cdm.detect import is_playready_cdm, is_widevine_cdm
ensure_track_drm(track, getattr(service, "session", None))
shared = {} if shared is None else shared
service_name = service.__class__.__name__
+3 -2
View File
@@ -654,8 +654,8 @@ class ImportService(TitleSource):
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.
so the track gets a stub that names every exported KID instead of a guess, bridged when
any entry is. A rebuilt PSSH holds only the keys for the KIDs it names.
A track that declares no content key, DRM or KID of its own is clear if it is a subtitle,
or if the export lists the KIDs of its other tracks. Only a video or audio track in an
@@ -693,6 +693,7 @@ class ImportService(TitleSource):
if drm_obj is None and stub_kids:
drm_type = (drm_dicts[0].get("system", "Widevine").lower()) if drm_dicts else "widevine"
drm_obj = drm_stub(drm_type, stub_kids)
drm_obj.bridged = any(d.get("bridged") for d in drm_dicts)
if drm_obj is None:
return None
+14 -14
View File
@@ -31,6 +31,7 @@ from pywidevine.pssh import PSSH as WidevinePSSH
from unshackle.core.config import config
from unshackle.core.constants import AUDIO_CODEC_MAP, DYNAMIC_RANGE_MAP, VIDEO_CODEC_MAP
from unshackle.core.drm import WHOLE_FILE_DRM, BlowfishStripe, ClearKeyCENC, ClearKeyCTR, PlayReady, Widevine
from unshackle.core.manifests import HLS
from unshackle.core.manifests.snapshot import restore
from unshackle.core.titles import Album, Episode, Movie, Music, Song, Title_T
from unshackle.core.titles.availability import availability_metadata
@@ -439,7 +440,7 @@ def pssh_drm(system: str, pssh_b64: str, kids: list[str] | None) -> Widevine | P
def drm_to_dict(drm: Widevine | PlayReady | ClearKeyCENC) -> dict[str, Any]:
"""A DRM in the export form: its system, PSSH and KIDs.
"""A DRM in the export form: its system, PSSH, KIDs and the FairPlay bridged mark.
The export keeps the content keys once at title level, not here.
"""
@@ -452,24 +453,21 @@ def drm_to_dict(drm: Widevine | PlayReady | ClearKeyCENC) -> dict[str, Any]:
data["laurl"] = drm.laurl
return data
if isinstance(drm, Widevine):
return {
"system": "Widevine",
"pssh_b64": drm.pssh.dumps(),
"kids": [kid.hex for kid in drm.kids],
}
return {
"system": "PlayReady",
"pssh_b64": drm.pssh_b64,
"kids": [kid.hex for kid in drm.kids],
}
data = {"system": "Widevine", "pssh_b64": drm.pssh.dumps()}
else:
data = {"system": "PlayReady", "pssh_b64": drm.pssh_b64}
data["kids"] = [kid.hex for kid in drm.kids]
if drm.bridged:
data["bridged"] = True
return data
def drm_from_dict(data: dict[str, Any]) -> Widevine | PlayReady | ClearKeyCENC:
"""Reconstruct a Widevine/PlayReady/ClearKeyCENC DRM instance from its export form.
Rebuilds the PSSH from the stored base64 (KIDs for ClearKey, which has no PSSH)
and re-injects any saved content keys so the resulting object can decrypt without
contacting a license server.
Rebuilds the PSSH from the stored base64 (KIDs for ClearKey, which has no PSSH, and for
a bridged stub, whose PlayReady PSSH names only its first KID) and re-injects any saved
content keys so the resulting object can decrypt without contacting a license server.
"""
system = data.get("system")
pssh_b64 = data.get("pssh_b64")
@@ -478,6 +476,8 @@ def drm_from_dict(data: dict[str, Any]) -> Widevine | PlayReady | ClearKeyCENC:
if system == "ClearKeyCENC":
drm: Widevine | PlayReady | ClearKeyCENC = ClearKeyCENC(kids=kids, laurl=data.get("laurl"))
elif data.get("bridged") and system in ("PlayReady", "Widevine"):
drm = HLS.bridge_stub(system.lower(), [UUID(hex=kid) for kid in kids])
elif not pssh_b64:
raise ValueError("Cannot reconstruct DRM without a stored PSSH.")
elif system in ("PlayReady", "Widevine"):