fix(drm): keep the track file when shaka-packager skips a stream with no samples

This commit is contained in:
imSp4rky
2026-11-28 14:21:21 -07:00
parent 4743e2f32c
commit b5b34cea02
2 changed files with 27 additions and 4 deletions
+21
View File
@@ -270,6 +270,7 @@ def shaka_run(tmp_path: Path, make: Callable[[], Any], raises: type[BaseExceptio
drm.content_keys[A] = KEY drm.content_keys[A] = KEY
with pytest.raises(raises): with pytest.raises(raises):
drm.decrypt_with_shaka_packager(path) drm.decrypt_with_shaka_packager(path)
assert path.read_bytes() == b"encrypted"
@pytest.mark.parametrize("make", DRMS) @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] 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: def test_a_shaka_ctrl_c_exit_is_a_keyboard_interrupt(tmp_path: Path, fake_packager: list[str]) -> None:
FakePackager.code = 0xC000013A FakePackager.code = 0xC000013A
shaka_run(tmp_path, lambda: _widevine(A), KeyboardInterrupt) shaka_run(tmp_path, lambda: _widevine(A), KeyboardInterrupt)
+6 -4
View File
@@ -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``. """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]``. 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: if not binaries.ShakaPackager:
raise coded(OSError("Shaka Packager executable not found but is required."), APIErrorCode.DRM_ERROR) 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: if not line:
continue continue
if "Skip stream" in line: if "Skip stream" in line:
# file/segment was so small that it didn't have any actual data, ignore
stream_skipped = True stream_skipped = True
if shaka_noise(line): if shaka_noise(line):
continue 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)) console.log(Text.from_ansi(f"\n[{label}]: " + shaka_log_buffer))
raise subprocess.CalledProcessError(p.returncode, [binaries.ShakaPackager, *arguments]) raise subprocess.CalledProcessError(p.returncode, [binaries.ShakaPackager, *arguments])
path.unlink() if stream_skipped:
if not stream_skipped: log.warning(f"shaka-packager found no samples in {path.name}, kept the file as it is")
shutil.move(output_path, path) output_path.unlink(missing_ok=True)
else:
output_path.replace(path)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
if e.returncode == 0xC000013A: # STATUS_CONTROL_C_EXIT if e.returncode == 0xC000013A: # STATUS_CONTROL_C_EXIT
raise KeyboardInterrupt() from None raise KeyboardInterrupt() from None