mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-30 13:05:08 +00:00
fix(drm): keep the input when mp4decrypt fails or writes no output
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"""Whole-file CENC decryption with mp4decrypt or shaka-packager, shared by Widevine, PlayReady and ClearKeyCENC."""
|
||||
|
||||
import contextlib
|
||||
import logging
|
||||
import shutil
|
||||
import subprocess
|
||||
from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
@@ -79,7 +79,11 @@ def decrypt_cenc(drm: Any, path: Path, drm_type: str) -> None:
|
||||
|
||||
|
||||
def mp4decrypt(path: Path, key_args_for: Callable[[dict[int, UUID]], list[str]]) -> None:
|
||||
"""Decrypt ``path`` in place with mp4decrypt, taking the ``--key`` arguments from ``key_args_for(track_kids)``."""
|
||||
"""Decrypt ``path`` in place with mp4decrypt, taking the ``--key`` arguments from ``key_args_for(track_kids)``.
|
||||
|
||||
A failure keeps ``path`` as it is and removes the output. mp4decrypt exits 0 after it fails to
|
||||
process a file and leaves a partial output, so this function also reads its stderr.
|
||||
"""
|
||||
if not binaries.Mp4decrypt:
|
||||
raise coded(OSError("mp4decrypt executable not found but is required."), APIErrorCode.DRM_ERROR)
|
||||
|
||||
@@ -87,18 +91,21 @@ def mp4decrypt(path: Path, key_args_for: Callable[[dict[int, UUID]], list[str]])
|
||||
key_args = key_args_for(kidless_track_kids(read_top_level_box(path, b"moov") or b""))
|
||||
cmd = [str(binaries.Mp4decrypt), "--show-progress", *key_args, str(path), str(output_path)]
|
||||
|
||||
output_path.unlink(missing_ok=True)
|
||||
try:
|
||||
run_tool("mp4decrypt", cmd, "mp4decrypt")
|
||||
p = run_tool("mp4decrypt", cmd, "mp4decrypt")
|
||||
if b"failed to process" in p.stderr:
|
||||
raise ToolFailed("mp4decrypt", p.returncode, cmd, p.stdout, p.stderr)
|
||||
if not output_path.exists():
|
||||
raise RuntimeError(f"mp4decrypt failed: output file {output_path} was not created")
|
||||
if output_path.stat().st_size == 0:
|
||||
raise RuntimeError(f"mp4decrypt failed: output file {output_path} is empty")
|
||||
output_path.replace(path)
|
||||
except ToolFailed as e:
|
||||
raise coded(e, APIErrorCode.DRM_ERROR) from None
|
||||
|
||||
if not output_path.exists():
|
||||
raise RuntimeError(f"mp4decrypt failed: output file {output_path} was not created")
|
||||
if output_path.stat().st_size == 0:
|
||||
raise RuntimeError(f"mp4decrypt failed: output file {output_path} is empty")
|
||||
|
||||
path.unlink()
|
||||
shutil.move(output_path, path)
|
||||
finally:
|
||||
with contextlib.suppress(OSError):
|
||||
output_path.unlink(missing_ok=True)
|
||||
|
||||
|
||||
def shaka_decrypt(path: Path, keys: str) -> None:
|
||||
|
||||
Reference in New Issue
Block a user