mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-03 22:45:09 +00:00
refactor(manifests): merge downloaded DASH and ISM segments through segment_pipeline.merge_downloaded
This commit is contained in:
@@ -24,6 +24,7 @@ from unshackle.core.manifests.segment_pipeline import (
|
||||
complete_track,
|
||||
fetch_segments,
|
||||
license_drm,
|
||||
merge_downloaded,
|
||||
)
|
||||
from unshackle.core.remote_protocol import APIErrorCode
|
||||
from unshackle.core.tracks.segment_store import SegmentStore
|
||||
@@ -427,3 +428,63 @@ def test_complete_track_without_drm_skips_the_decryption(tmp_path, seen):
|
||||
assert seen == []
|
||||
assert track.path == ctx.save_path
|
||||
assert ctx.statuses == ["Downloaded"]
|
||||
|
||||
|
||||
class FakeStore:
|
||||
"""Records when the merge ends the download phase."""
|
||||
|
||||
def __init__(self, calls: list[str]) -> None:
|
||||
self.calls = calls
|
||||
|
||||
def merging(self) -> None:
|
||||
self.calls.append("merging")
|
||||
|
||||
|
||||
def segment_dir(ctx: DownloadContext, parts: list[bytes]) -> list[Path]:
|
||||
ctx.save_dir.mkdir()
|
||||
files = [ctx.save_dir / f"{i}.mp4" for i in range(len(parts))]
|
||||
for path, data in zip(files, parts, strict=True):
|
||||
path.write_bytes(data)
|
||||
return files
|
||||
|
||||
|
||||
def test_merge_downloaded_writes_the_init_then_each_segment_and_removes_it(tmp_path):
|
||||
ctx = make_ctx(tmp_path)
|
||||
files = segment_dir(ctx, PARTS[:3])
|
||||
calls: list[str] = []
|
||||
|
||||
def append(dst, path):
|
||||
calls.append(path.name)
|
||||
dst.write(path.read_bytes())
|
||||
|
||||
merge_downloaded(
|
||||
SimpleNamespace(id="EXAMPLE"), # type: ignore[arg-type]
|
||||
ctx,
|
||||
"DASH",
|
||||
FakeStore(calls), # type: ignore[arg-type]
|
||||
INIT,
|
||||
append,
|
||||
prepare=lambda segment_files: calls.append(f"prepare {len(segment_files)}"),
|
||||
)
|
||||
|
||||
assert ctx.save_path.read_bytes() == INIT + b"".join(PARTS[:3])
|
||||
assert calls == ["merging", "prepare 3", "0.mp4", "1.mp4", "2.mp4"]
|
||||
assert not any(path.exists() for path in files)
|
||||
assert ctx.statuses == ["Merging"] # type: ignore[attr-defined]
|
||||
|
||||
|
||||
def test_merge_downloaded_without_segments_raises(tmp_path):
|
||||
ctx = make_ctx(tmp_path)
|
||||
calls: list[str] = []
|
||||
track = SimpleNamespace(id="EXAMPLE")
|
||||
store = FakeStore(calls)
|
||||
|
||||
with pytest.raises(FileNotFoundError, match="Output directory does not exist"):
|
||||
merge_downloaded(track, ctx, "ISM", store, None, pytest.fail) # type: ignore[arg-type]
|
||||
assert calls == []
|
||||
|
||||
ctx.save_dir.mkdir()
|
||||
with pytest.raises(FileNotFoundError, match="No segment files found"):
|
||||
merge_downloaded(track, ctx, "ISM", store, None, pytest.fail) # type: ignore[arg-type]
|
||||
assert calls == ["merging"]
|
||||
assert not ctx.save_path.exists()
|
||||
|
||||
Reference in New Issue
Block a user