mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-30 21:15:18 +00:00
refactor(manifests): share one segment pipeline across dash, hls, ism
This commit is contained in:
@@ -0,0 +1,243 @@
|
||||
"""The segment pipeline: the steps DASH, HLS and ISM share to download a segmented track.
|
||||
|
||||
Each manifest finds its own segments and supplies its own quirks; the pipeline runs the
|
||||
licence step, the download with its rolling merge, the decrypt hand-off and the cleanup.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import html
|
||||
import shutil
|
||||
from concurrent.futures import Future
|
||||
from contextlib import ExitStack, closing, contextmanager
|
||||
from pathlib import Path
|
||||
from typing import Any, BinaryIO, Callable, Iterator, Optional
|
||||
|
||||
from unshackle.core.constants import DOWNLOAD_CANCELLED, AnyTrack
|
||||
from unshackle.core.drm import DRM_T
|
||||
from unshackle.core.drm.segment_decrypt import SegmentDecrypter
|
||||
from unshackle.core.drm.verify import decrypt_track
|
||||
from unshackle.core.events import events
|
||||
from unshackle.core.session import AnySession
|
||||
from unshackle.core.tracks import DownloadContext, resume
|
||||
from unshackle.core.tracks.track import assert_fragments_decrypted
|
||||
from unshackle.core.utilities import log_event, try_ensure_utf8
|
||||
|
||||
|
||||
def append_segment(dst: BinaryIO, segment_file: Path, is_text_subtitle: bool) -> None:
|
||||
"""Append one segment file to the output, with the text subtitle fixups when they apply."""
|
||||
if is_text_subtitle:
|
||||
segment_data = try_ensure_utf8(segment_file.read_bytes())
|
||||
segment_data = (
|
||||
segment_data.decode("utf8")
|
||||
.replace("‎", html.unescape("‎"))
|
||||
.replace("‏", html.unescape("‏"))
|
||||
.encode("utf8")
|
||||
)
|
||||
dst.write(segment_data)
|
||||
else:
|
||||
with open(segment_file, "rb") as src:
|
||||
shutil.copyfileobj(src, dst, 1024 * 1024)
|
||||
|
||||
|
||||
class RollingMerge:
|
||||
"""Appends segments to the output in index order as they arrive, out of order, and unlinks them.
|
||||
|
||||
A cursor holds the next expected index; every `add` records one ready segment and then drains
|
||||
every contiguous ready segment from the cursor onward. A segment that carries a future (per-segment
|
||||
decryption) waits for that future before the append, so the output never holds raw bytes.
|
||||
|
||||
`append(dst, segment_file)` writes one segment; the default is a plain byte copy. A parser
|
||||
whose merge needs a per-segment transform, or a head that only exists once segment 0 has
|
||||
landed (ISM), passes its own.
|
||||
"""
|
||||
|
||||
def __init__(self, dst: BinaryIO, append: Optional[Callable[[BinaryIO, Path], None]] = None) -> None:
|
||||
self.dst = dst
|
||||
self.append = append or (lambda dst, path: append_segment(dst, path, is_text_subtitle=False))
|
||||
self.cursor = 0
|
||||
self.ready: dict[int, tuple[Path, Optional[Future]]] = {}
|
||||
|
||||
def add(self, index: int, segment_file: Path, future: Optional[Future] = None) -> None:
|
||||
# runs on the downloader's own loop, so a decrypt wait or a catch-up burst
|
||||
# delays new submissions; move the drain to a writer thread if throughput measurably drops
|
||||
self.ready[index] = (segment_file, future)
|
||||
while self.cursor in self.ready:
|
||||
path, pending = self.ready.pop(self.cursor)
|
||||
if pending is not None:
|
||||
pending.result()
|
||||
self.append(self.dst, path)
|
||||
self.cursor += 1
|
||||
# the bytes are already in the output; a handle another process still holds on
|
||||
# the just-replaced file (Windows) must not fail the track, the segment dir sweep
|
||||
# after the download removes any leftover
|
||||
try:
|
||||
path.unlink()
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
@property
|
||||
def merged(self) -> int:
|
||||
return self.cursor
|
||||
|
||||
|
||||
@contextmanager
|
||||
def licensing(ctx: DownloadContext) -> Iterator[None]:
|
||||
"""Show FAILED and cancel the pending track downloads when the body raises."""
|
||||
try:
|
||||
yield
|
||||
except Exception:
|
||||
DOWNLOAD_CANCELLED.set()
|
||||
ctx.progress(downloaded="[red]FAILED")
|
||||
raise
|
||||
|
||||
|
||||
def license_drm(drm: DRM_T, ctx: DownloadContext, **kwargs: Any) -> None:
|
||||
"""Call ctx.license_widevine for one DRM, with the LICENSING and LICENSED progress around it."""
|
||||
if not ctx.license_widevine:
|
||||
raise ValueError("license_widevine func must be supplied to use DRM")
|
||||
ctx.progress(downloaded="LICENSING")
|
||||
ctx.license_widevine(drm, **kwargs)
|
||||
ctx.progress(downloaded="[yellow]LICENSED")
|
||||
|
||||
|
||||
def fetch_segments(
|
||||
track: AnyTrack,
|
||||
ctx: DownloadContext,
|
||||
session: AnySession,
|
||||
label: str,
|
||||
urls: list[dict[str, Any]],
|
||||
output_dir: Path,
|
||||
filename: str,
|
||||
*,
|
||||
rolling: bool = False,
|
||||
init: Optional[bytes] = None,
|
||||
append: Optional[Callable[[BinaryIO, Path], None]] = None,
|
||||
decrypter: Optional[SegmentDecrypter] = None,
|
||||
) -> tuple[Optional[RollingMerge], Optional[bytes]]:
|
||||
"""Download the segment requests and return the rolling merge, if any, and the init that heads the output.
|
||||
|
||||
With `rolling`, the init (or the decrypter's decrypted init) and each segment go into ctx.save_path
|
||||
as they land. A decrypter gets every segment before its SEGMENT_DOWNLOADED event. Any failure closes
|
||||
the decrypter and removes a rolling output. The returned init is the decrypter's decrypted init, else `init`.
|
||||
"""
|
||||
merger: Optional[RollingMerge] = None
|
||||
try:
|
||||
with ExitStack() as stack:
|
||||
if rolling:
|
||||
output = stack.enter_context(open(ctx.save_path, "wb"))
|
||||
head = decrypter.init_bytes() if decrypter else init
|
||||
if head:
|
||||
output.write(head)
|
||||
merger = RollingMerge(output, append)
|
||||
|
||||
# an exception raised in this body (a decrypt failure re-raised by the merge)
|
||||
# must close the generator now, so its finally aborts the workers
|
||||
stream = stack.enter_context(
|
||||
closing(
|
||||
track.downloader(
|
||||
urls=urls,
|
||||
output_dir=output_dir,
|
||||
filename=filename,
|
||||
headers=session.headers,
|
||||
cookies=session.cookies,
|
||||
proxy=ctx.proxy,
|
||||
max_workers=ctx.max_workers,
|
||||
session=session,
|
||||
adaptive=ctx.adaptive_workers,
|
||||
processes=ctx.download_processes,
|
||||
)
|
||||
)
|
||||
)
|
||||
for status_update in stream:
|
||||
file_downloaded = status_update.get("file_downloaded")
|
||||
if file_downloaded:
|
||||
future = decrypter.submit(file_downloaded) if decrypter else None
|
||||
# listeners get the segment while it still exists; the merge unlinks it
|
||||
events.emit(events.Types.SEGMENT_DOWNLOADED, track=track, segment=file_downloaded)
|
||||
if merger:
|
||||
merger.add(int(file_downloaded.stem), file_downloaded, future)
|
||||
else:
|
||||
downloaded = status_update.get("downloaded")
|
||||
if downloaded and downloaded.endswith("/s"):
|
||||
status_update["downloaded"] = f"{label} {downloaded}"
|
||||
ctx.progress(**status_update)
|
||||
|
||||
head_init = decrypter.finish() if decrypter else init
|
||||
except BaseException:
|
||||
if decrypter:
|
||||
decrypter.close()
|
||||
if merger:
|
||||
ctx.save_path.unlink(missing_ok=True)
|
||||
raise
|
||||
return merger, head_init
|
||||
|
||||
|
||||
def check_merged(merger: RollingMerge, total: int, track: AnyTrack, ctx: DownloadContext, label: str) -> None:
|
||||
"""Remove the output and raise FileNotFoundError when the rolling merge appended fewer than total segments."""
|
||||
if merger.merged == total:
|
||||
return
|
||||
error_msg = f"Rolling merge appended {merger.merged} of {total} segments: {ctx.save_dir}"
|
||||
log_event(
|
||||
f"manifest_{label.lower()}_rolling_merge_incomplete",
|
||||
level="ERROR",
|
||||
message=error_msg,
|
||||
context={
|
||||
"track_id": getattr(track, "id", None),
|
||||
"track_type": track.__class__.__name__,
|
||||
"save_dir": str(ctx.save_dir),
|
||||
"merged": merger.merged,
|
||||
"total_segments": total,
|
||||
"downloader": "requests",
|
||||
},
|
||||
)
|
||||
ctx.save_path.unlink(missing_ok=True)
|
||||
raise FileNotFoundError(error_msg)
|
||||
|
||||
|
||||
def decrypt_output(track: AnyTrack, ctx: DownloadContext, drm: DRM_T, decrypt: bool = True) -> None:
|
||||
"""Decrypt ctx.save_path through decrypt_track, check every fragment, and emit TRACK_DECRYPTED.
|
||||
|
||||
`decrypt=False` means a SegmentDecrypter already decrypted the segments, so only the check runs.
|
||||
"""
|
||||
ctx.progress(downloaded="Decrypting", completed=0, total=None)
|
||||
decrypt_track(drm, ctx.save_path, ctx.license_widevine, decrypt=decrypt)
|
||||
assert_fragments_decrypted(ctx.save_path)
|
||||
track.drm = None
|
||||
events.emit(events.Types.TRACK_DECRYPTED, track=track, drm=drm, segment=None)
|
||||
|
||||
|
||||
def remove_segment_dir(save_dir: Path) -> None:
|
||||
"""Remove the segment directory, also when a superseded hedge download still dropped a .!dev file in it."""
|
||||
if save_dir.exists() and save_dir.name.endswith("_segments"):
|
||||
try:
|
||||
save_dir.rmdir()
|
||||
except OSError:
|
||||
shutil.rmtree(save_dir, ignore_errors=True)
|
||||
|
||||
|
||||
def complete_track(track: AnyTrack, ctx: DownloadContext, drm: Optional[DRM_T], decrypt: bool = True) -> None:
|
||||
"""Hand the merged file to the track: TRACK_DOWNLOADED, then the decryption, then the segment directory cleanup."""
|
||||
track.path = ctx.save_path
|
||||
events.emit(events.Types.TRACK_DOWNLOADED, track=track)
|
||||
|
||||
if drm:
|
||||
decrypt_output(track, ctx, drm, decrypt)
|
||||
ctx.progress(downloaded="Decrypted", completed=100, total=100)
|
||||
|
||||
remove_segment_dir(ctx.save_dir)
|
||||
resume.clear_sidecar(ctx.save_dir)
|
||||
ctx.progress(downloaded="Downloaded")
|
||||
|
||||
|
||||
__all__ = (
|
||||
"RollingMerge",
|
||||
"append_segment",
|
||||
"check_merged",
|
||||
"complete_track",
|
||||
"decrypt_output",
|
||||
"fetch_segments",
|
||||
"license_drm",
|
||||
"licensing",
|
||||
"remove_segment_dir",
|
||||
)
|
||||
Reference in New Issue
Block a user