From b5b34cea02d27a132361ca5a53585f03296cbda7 Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Sat, 28 Nov 2026 14:21:21 -0700 Subject: [PATCH] fix(drm): keep the track file when shaka-packager skips a stream with no samples --- tests/core/test_mp4decrypt_track_key.py | 21 +++++++++++++++++++++ unshackle/core/drm/decrypters.py | 10 ++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/tests/core/test_mp4decrypt_track_key.py b/tests/core/test_mp4decrypt_track_key.py index 5cde540f..1268e514 100644 --- a/tests/core/test_mp4decrypt_track_key.py +++ b/tests/core/test_mp4decrypt_track_key.py @@ -270,6 +270,7 @@ def shaka_run(tmp_path: Path, make: Callable[[], Any], raises: type[BaseExceptio drm.content_keys[A] = KEY with pytest.raises(raises): drm.decrypt_with_shaka_packager(path) + assert path.read_bytes() == b"encrypted" @pytest.mark.parametrize("make", DRMS) @@ -299,6 +300,26 @@ def test_a_shaka_failure_logs_under_the_drm_label( assert len(fake_packager) == 1 and f"[{label}]: " in fake_packager[0] and "Bad key" in fake_packager[0] +@pytest.mark.parametrize("make", DRMS) +def test_a_skipped_shaka_stream_keeps_the_input( + tmp_path: Path, fake_packager: list[str], make: Callable[[], Any] +) -> None: + FakePackager.log = ( + "[1128/120000:INFO:mp4_muxer.cc(1)] Skip stream 'track_decrypted.mp4' which does not contain any sample.\n" + ) + path = tmp_path / "track.mp4" + path.write_bytes(b"encrypted") + stray = tmp_path / "track_decrypted.mp4" + stray.write_bytes(b"stale") + drm = make() + drm.content_keys[A] = KEY + + drm.decrypt_with_shaka_packager(path) + + assert path.read_bytes() == b"encrypted" + assert not stray.exists() + + def test_a_shaka_ctrl_c_exit_is_a_keyboard_interrupt(tmp_path: Path, fake_packager: list[str]) -> None: FakePackager.code = 0xC000013A shaka_run(tmp_path, lambda: _widevine(A), KeyboardInterrupt) diff --git a/unshackle/core/drm/decrypters.py b/unshackle/core/drm/decrypters.py index ad6749a8..bfb33ea1 100644 --- a/unshackle/core/drm/decrypters.py +++ b/unshackle/core/drm/decrypters.py @@ -115,6 +115,7 @@ def shaka_decrypt(path: Path, keys: str, label: str) -> None: """Decrypt ``path`` in place with shaka-packager and its ``--keys`` value ``keys``. Only a non-zero exit is a failure; its non-noise log lines go to the console under ``[label]``. + A track that shaka-packager skips because it has no samples leaves ``path`` as it is. """ if not binaries.ShakaPackager: raise coded(OSError("Shaka Packager executable not found but is required."), APIErrorCode.DRM_ERROR) @@ -149,7 +150,6 @@ def shaka_decrypt(path: Path, keys: str, label: str) -> None: if not line: continue if "Skip stream" in line: - # file/segment was so small that it didn't have any actual data, ignore stream_skipped = True if shaka_noise(line): continue @@ -165,9 +165,11 @@ def shaka_decrypt(path: Path, keys: str, label: str) -> None: console.log(Text.from_ansi(f"\n[{label}]: " + shaka_log_buffer)) raise subprocess.CalledProcessError(p.returncode, [binaries.ShakaPackager, *arguments]) - path.unlink() - if not stream_skipped: - shutil.move(output_path, path) + if stream_skipped: + log.warning(f"shaka-packager found no samples in {path.name}, kept the file as it is") + output_path.unlink(missing_ok=True) + else: + output_path.replace(path) except subprocess.CalledProcessError as e: if e.returncode == 0xC000013A: # STATUS_CONTROL_C_EXIT raise KeyboardInterrupt() from None