forked from kenzuya/unshackle
refactor(manifests): share one segment pipeline across dash, hls, ism
This commit is contained in:
+42
-136
@@ -9,7 +9,7 @@ import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import time
|
||||
from contextlib import ExitStack, closing, suppress
|
||||
from contextlib import suppress
|
||||
from functools import partial
|
||||
from itertools import accumulate
|
||||
from pathlib import Path
|
||||
@@ -31,18 +31,20 @@ from requests import Session
|
||||
from unshackle.core import binaries
|
||||
from unshackle.core.cdm.detect import cdm_type_stub, is_playready_cdm, is_widevine_cdm
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.constants import DOWNLOAD_ALL_DRM, DOWNLOAD_CANCELLED, DOWNLOAD_LICENCE_ONLY, AnyTrack
|
||||
from unshackle.core.constants import DOWNLOAD_ALL_DRM, DOWNLOAD_LICENCE_ONLY, AnyTrack
|
||||
from unshackle.core.drm import DRM_T, ClearKey, ClearKeyCENC, MonaLisa, PlayReady, Widevine
|
||||
from unshackle.core.drm.segment_decrypt import SegmentDecrypter, can_use
|
||||
from unshackle.core.drm.verify import decrypt_track
|
||||
from unshackle.core.events import events
|
||||
from unshackle.core.manifests.dash import (
|
||||
PeriodRebase,
|
||||
RollingMerge,
|
||||
from unshackle.core.manifests.dash import PeriodRebase, mdhd_timescale, missing_language, rebase_webvtt_files
|
||||
from unshackle.core.manifests.segment_pipeline import (
|
||||
append_segment,
|
||||
mdhd_timescale,
|
||||
missing_language,
|
||||
rebase_webvtt_files,
|
||||
check_merged,
|
||||
decrypt_output,
|
||||
fetch_segments,
|
||||
license_drm,
|
||||
licensing,
|
||||
remove_segment_dir,
|
||||
)
|
||||
from unshackle.core.session import RnetResponse, RnetSession, utf8_text
|
||||
from unshackle.core.tracks import Audio, DownloadContext, Subtitle, Tracks, Video, resume
|
||||
@@ -645,16 +647,12 @@ class HLS:
|
||||
save_path = ctx.save_path
|
||||
save_dir = ctx.save_dir
|
||||
progress = ctx.progress
|
||||
proxy = ctx.proxy
|
||||
max_workers = ctx.max_workers
|
||||
adaptive = ctx.adaptive_workers
|
||||
processes = ctx.download_processes
|
||||
license_widevine = ctx.license_widevine
|
||||
cdm = ctx.cdm
|
||||
|
||||
if proxy:
|
||||
if ctx.proxy:
|
||||
if isinstance(session, Session):
|
||||
session.proxies.update({"all": proxy})
|
||||
session.proxies.update({"all": ctx.proxy})
|
||||
|
||||
log = logging.getLogger("HLS")
|
||||
assert isinstance(track.url, str)
|
||||
@@ -720,19 +718,11 @@ class HLS:
|
||||
track.drm = [media_drm]
|
||||
if DOWNLOAD_ALL_DRM.is_set():
|
||||
track.drm += HLS.alternate_drm(media_keys, media_drm, session, init_kid)
|
||||
try:
|
||||
if not license_widevine:
|
||||
raise ValueError("license_widevine func must be supplied to use DRM")
|
||||
progress(downloaded="LICENSING")
|
||||
license_widevine(media_drm, track_kid=track_kid)
|
||||
progress(downloaded="[yellow]LICENSED")
|
||||
initial_drm_licensed = True
|
||||
initial_drm_key = media_playlist_key
|
||||
session_drm = media_drm
|
||||
except Exception: # noqa
|
||||
DOWNLOAD_CANCELLED.set() # skip pending track downloads
|
||||
progress(downloaded="[red]FAILED")
|
||||
raise
|
||||
with licensing(ctx):
|
||||
license_drm(media_drm, ctx, track_kid=track_kid)
|
||||
initial_drm_licensed = True
|
||||
initial_drm_key = media_playlist_key
|
||||
session_drm = media_drm
|
||||
elif isinstance(media_drm, ClearKey):
|
||||
# AES-128 (ClearKey) needs no license server - the key is already fetched.
|
||||
track.drm = [media_drm]
|
||||
@@ -741,29 +731,13 @@ class HLS:
|
||||
initial_drm_licensed = True
|
||||
|
||||
if not initial_drm_licensed and session_drm and isinstance(session_drm, (Widevine, PlayReady)):
|
||||
try:
|
||||
if not license_widevine:
|
||||
raise ValueError("license_widevine func must be supplied to use DRM")
|
||||
with licensing(ctx):
|
||||
track_kid = HLS.get_track_kid_from_init(master, track, session) or session_drm.kid
|
||||
progress(downloaded="LICENSING")
|
||||
license_widevine(session_drm, track_kid=track_kid)
|
||||
progress(downloaded="[yellow]LICENSED")
|
||||
except Exception: # noqa
|
||||
DOWNLOAD_CANCELLED.set() # skip pending track downloads
|
||||
progress(downloaded="[red]FAILED")
|
||||
raise
|
||||
license_drm(session_drm, ctx, track_kid=track_kid)
|
||||
|
||||
if not initial_drm_licensed and session_drm and isinstance(session_drm, MonaLisa):
|
||||
try:
|
||||
if not license_widevine:
|
||||
raise ValueError("license_widevine func must be supplied to use DRM")
|
||||
progress(downloaded="LICENSING")
|
||||
license_widevine(session_drm)
|
||||
progress(downloaded="[yellow]LICENSED")
|
||||
except Exception: # noqa
|
||||
DOWNLOAD_CANCELLED.set() # skip pending track downloads
|
||||
progress(downloaded="[red]FAILED")
|
||||
raise
|
||||
with licensing(ctx):
|
||||
license_drm(session_drm, ctx)
|
||||
|
||||
if DOWNLOAD_LICENCE_ONLY.is_set():
|
||||
# a playlist can change its key part-way, and only the download loop licenses the later keys
|
||||
@@ -799,8 +773,6 @@ class HLS:
|
||||
total_segments = len(master.segments) - len(unwanted_segments)
|
||||
progress(total=total_segments)
|
||||
|
||||
downloader = track.downloader
|
||||
|
||||
urls: list[dict[str, Any]] = []
|
||||
segment_durations: list[int] = []
|
||||
|
||||
@@ -849,19 +821,6 @@ class HLS:
|
||||
if config.continue_downloads and resumable_drm:
|
||||
resume.write_sidecar(save_dir, digest)
|
||||
|
||||
downloader_args: dict[str, Any] = dict(
|
||||
urls=urls,
|
||||
output_dir=segment_save_dir,
|
||||
filename="{i:0%d}{ext}" % len(str(len(urls))),
|
||||
headers=session.headers,
|
||||
cookies=session.cookies,
|
||||
proxy=proxy,
|
||||
max_workers=max_workers,
|
||||
session=session,
|
||||
adaptive=adaptive,
|
||||
processes=processes,
|
||||
)
|
||||
|
||||
log_event(
|
||||
"manifest_hls_download_start",
|
||||
level="DEBUG",
|
||||
@@ -885,35 +844,20 @@ class HLS:
|
||||
use_segment_decrypt = use_segment_decrypt and can_use(session_drm, config.decryption, init_content)
|
||||
if use_segment_decrypt:
|
||||
assert isinstance(session_drm, (Widevine, PlayReady, ClearKeyCENC))
|
||||
segment_decrypter = SegmentDecrypter(session_drm, init_content, save_dir.parent, max_workers)
|
||||
segment_decrypter = SegmentDecrypter(session_drm, init_content, save_dir.parent, ctx.max_workers)
|
||||
|
||||
merger: Optional[RollingMerge] = None
|
||||
try:
|
||||
with ExitStack() as stack:
|
||||
if use_rolling_merge:
|
||||
output = stack.enter_context(open(save_path, "wb"))
|
||||
output.write(segment_decrypter.init_bytes() if segment_decrypter else init_content)
|
||||
merger = RollingMerge(output)
|
||||
|
||||
stream = stack.enter_context(closing(downloader(**downloader_args)))
|
||||
for status_update in stream:
|
||||
file_downloaded = status_update.get("file_downloaded")
|
||||
if file_downloaded:
|
||||
future = segment_decrypter.submit(file_downloaded) if segment_decrypter else None
|
||||
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"HLS {downloaded}"
|
||||
progress(**status_update)
|
||||
except BaseException:
|
||||
if segment_decrypter:
|
||||
segment_decrypter.close()
|
||||
if merger:
|
||||
save_path.unlink(missing_ok=True)
|
||||
raise
|
||||
merger, decrypted_init = fetch_segments(
|
||||
track,
|
||||
ctx,
|
||||
session,
|
||||
"HLS",
|
||||
urls,
|
||||
segment_save_dir,
|
||||
"{i:0%d}{ext}" % len(str(len(urls))),
|
||||
rolling=use_rolling_merge,
|
||||
init=map_data[1] if map_data else None,
|
||||
decrypter=segment_decrypter,
|
||||
)
|
||||
|
||||
for control_file in segment_save_dir.glob("*.!dev"):
|
||||
control_file.unlink(missing_ok=True)
|
||||
@@ -922,27 +866,8 @@ class HLS:
|
||||
resume.clear_sidecar(save_dir)
|
||||
|
||||
if merger:
|
||||
if segment_decrypter:
|
||||
segment_decrypter.finish()
|
||||
if merger.merged != total_segments:
|
||||
error_msg = f"Rolling merge appended {merger.merged} of {total_segments} segments: {save_dir}"
|
||||
log_event(
|
||||
"manifest_hls_rolling_merge_incomplete",
|
||||
level="ERROR",
|
||||
message=error_msg,
|
||||
context={
|
||||
"track_id": getattr(track, "id", None),
|
||||
"track_type": track.__class__.__name__,
|
||||
"save_dir": str(save_dir),
|
||||
"merged": merger.merged,
|
||||
"total_segments": total_segments,
|
||||
"downloader": "requests",
|
||||
},
|
||||
)
|
||||
save_path.unlink(missing_ok=True)
|
||||
raise FileNotFoundError(error_msg)
|
||||
if save_dir.name.endswith("_segments"):
|
||||
shutil.rmtree(save_dir, ignore_errors=True)
|
||||
check_merged(merger, total_segments, track, ctx, "HLS")
|
||||
remove_segment_dir(save_dir)
|
||||
log_event(
|
||||
"manifest_hls_download_complete",
|
||||
level="DEBUG",
|
||||
@@ -957,11 +882,7 @@ class HLS:
|
||||
},
|
||||
)
|
||||
if session_drm:
|
||||
progress(downloaded="Decrypting", completed=0, total=None)
|
||||
decrypt_track(session_drm, save_path, license_widevine, decrypt=not segment_decrypter)
|
||||
assert_fragments_decrypted(save_path)
|
||||
track.drm = None
|
||||
events.emit(events.Types.TRACK_DECRYPTED, track=track, drm=session_drm, segment=None)
|
||||
decrypt_output(track, ctx, session_drm, decrypt=not segment_decrypter)
|
||||
progress(downloaded="Downloaded", completed=100, total=100)
|
||||
track.path = save_path
|
||||
events.emit(events.Types.TRACK_DOWNLOADED, track=track)
|
||||
@@ -975,7 +896,6 @@ class HLS:
|
||||
# First segment's Media Sequence Number - used as the AES-128 IV when EXT-X-KEY has
|
||||
# no explicit IV (RFC 8216 §5.2), where each segment's IV is its sequence number.
|
||||
media_sequence_start = getattr(master, "media_sequence", None) or 0
|
||||
decrypted_init: Optional[bytes] = None
|
||||
encryption_data: Optional[tuple[Optional[m3u8.Key], DRM_T]] = None
|
||||
if session_drm:
|
||||
encryption_data = (initial_drm_key, session_drm)
|
||||
@@ -1032,7 +952,7 @@ class HLS:
|
||||
|
||||
Returns the decrypted path.
|
||||
"""
|
||||
nonlocal map_data, decrypted_init
|
||||
nonlocal map_data
|
||||
assert encryption_data is not None
|
||||
drm = encryption_data[1]
|
||||
first_segment_i = next(
|
||||
@@ -1062,8 +982,7 @@ class HLS:
|
||||
if segment_decrypter:
|
||||
if not map_data:
|
||||
raise ValueError("Segment decryption needs an EXT-X-MAP init segment.")
|
||||
if decrypted_init is None:
|
||||
decrypted_init = segment_decrypter.finish()
|
||||
assert decrypted_init is not None
|
||||
map_data = (map_data[0], decrypted_init)
|
||||
merge(to=merged_path, via=files, delete=True, include_map_data=True)
|
||||
decrypt_track(drm, merged_path, license_widevine, decrypt=not segment_decrypter)
|
||||
@@ -1158,20 +1077,11 @@ class HLS:
|
||||
if key is None:
|
||||
encryption_data = None
|
||||
elif not encryption_data or encryption_data[0] != key:
|
||||
try:
|
||||
with licensing(ctx):
|
||||
init_kid = track.get_key_id(map_data[1]) if map_data and key.method != "AES-128" else None
|
||||
drm = HLS.get_drm(key, session, **({"kid": init_kid} if init_kid else {}))
|
||||
if isinstance(drm, (Widevine, PlayReady)):
|
||||
if not license_widevine:
|
||||
raise ValueError("license_widevine func must be supplied to use DRM")
|
||||
track_kid = init_kid or drm.kid
|
||||
progress(downloaded="LICENSING")
|
||||
license_widevine(drm, track_kid=track_kid)
|
||||
progress(downloaded="[yellow]LICENSED")
|
||||
except Exception: # noqa
|
||||
DOWNLOAD_CANCELLED.set() # skip pending track downloads
|
||||
progress(downloaded="[red]FAILED")
|
||||
raise
|
||||
license_drm(drm, ctx, track_kid=init_kid or drm.kid)
|
||||
if (
|
||||
encryption_data
|
||||
and isinstance(drm, (Widevine, PlayReady))
|
||||
@@ -1258,11 +1168,7 @@ class HLS:
|
||||
with suppress(OSError):
|
||||
discontinuity_file.unlink()
|
||||
|
||||
if save_dir.exists() and save_dir.name.endswith("_segments"):
|
||||
try:
|
||||
save_dir.rmdir()
|
||||
except OSError:
|
||||
shutil.rmtree(save_dir, ignore_errors=True)
|
||||
remove_segment_dir(save_dir)
|
||||
|
||||
progress(downloaded="Downloaded", completed=100, total=100)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user