fix(drm): key PIFF-first tracks by track ID and trust shaka's exit code

mp4decrypt copied a track whose first sinf has no tenc through encrypted; pass its key by track ID and retry with shaka on a passthrough. Shaka fails only on a non-zero exit now.
This commit is contained in:
imSp4rky
2026-11-03 17:05:26 -07:00
parent 5ffdda3883
commit 893c81f428
14 changed files with 444 additions and 140 deletions
+15 -22
View File
@@ -16,6 +16,8 @@ from rich.text import Text
from unshackle.core import binaries
from unshackle.core.config import config
from unshackle.core.console import console
from unshackle.core.drm import key_args as drm_key_args
from unshackle.core.drm.verify import kidless_track_kids, mp4decrypt_or_shaka, read_top_level_box
from unshackle.core.utilities import log_event
from unshackle.core.utils import run_proxy
@@ -209,7 +211,7 @@ class ClearKeyCENC:
decrypt_start = time.monotonic()
if decrypter == "mp4decrypt":
self.decrypt_with_mp4decrypt(path)
mp4decrypt_or_shaka(self, path)
else:
self.decrypt_with_shaka_packager(path)
@@ -224,12 +226,12 @@ class ClearKeyCENC:
output_size=path.stat().st_size if path.exists() else 0,
)
def mp4decrypt_key_args(self) -> list[str]:
"""Build the mp4decrypt --key arguments for every content key."""
def mp4decrypt_key_args(self, track_kids: Optional[dict[int, UUID]] = None) -> list[str]:
"""Build the mp4decrypt --key arguments for every content key, and by track ID for each ``track_kids`` KID."""
key_args = []
for kid, key in self.content_keys.items():
key_args.extend(["--key", f"{kid.hex}:{key}"])
return key_args
return key_args + drm_key_args.track_key_args(self.content_keys, track_kids)
def decrypt_with_mp4decrypt(self, path: Path) -> None:
"""Decrypt using mp4decrypt"""
@@ -238,7 +240,7 @@ class ClearKeyCENC:
output_path = path.with_stem(f"{path.stem}_decrypted")
key_args = self.mp4decrypt_key_args()
key_args = self.mp4decrypt_key_args(kidless_track_kids(read_top_level_box(path, b"moov") or b""))
cmd = [
str(binaries.Mp4decrypt),
@@ -301,7 +303,6 @@ class ClearKeyCENC:
)
stream_skipped = False
had_error = False
shaka_log_buffer = ""
assert p.stderr is not None
@@ -312,27 +313,19 @@ class ClearKeyCENC:
if "Skip stream" in line:
# file/segment was so small that it didn't have any actual data, ignore
stream_skipped = True
if ":INFO:" in line:
continue
if "I0" in line or "W0" in line:
continue
if ":ERROR:" in line:
had_error = True
if "Insufficient bits in bitstream for given AVC profile" in line:
# this is a warning and is something we don't have to worry about
if drm_key_args.shaka_noise(line):
continue
shaka_log_buffer += f"{line.strip()}\n"
if shaka_log_buffer:
# wrap to console width - padding - '[ClearKey]: '
shaka_log_buffer = "\n ".join(
textwrap.wrap(shaka_log_buffer.rstrip(), width=console.width - 22, initial_indent="")
)
console.log(Text.from_ansi("\n[ClearKey]: " + shaka_log_buffer))
p.wait()
if p.returncode != 0 or had_error:
if p.returncode != 0:
if shaka_log_buffer:
# wrap to console width - padding - '[ClearKey]: '
shaka_log_buffer = "\n ".join(
textwrap.wrap(shaka_log_buffer.rstrip(), width=console.width - 22, initial_indent="")
)
console.log(Text.from_ansi("\n[ClearKey]: " + shaka_log_buffer))
raise subprocess.CalledProcessError(p.returncode, [binaries.ShakaPackager, *arguments])
path.unlink()