Compare commits

..
4 Commits
Author SHA1 Message Date
Andy 98ecf6f876 feat: Add download retry count option to download function 2025-09-23 01:32:00 +00:00
Andy 5df6914536 feat: Add options for required subtitles and best available quality in download command 2025-09-23 01:28:55 +00:00
Andy c1df074965 Change new dynamic CDM selection text to be in Debug only 2025-09-14 04:25:57 +00:00
Andy da60a396dd Fix: Prevent KeyError when reusing remote CDMs in dynamic selection
Creates a copy of the CDM dictionary before modification to prevent the original configuration from being mutated, allowing the same CDM to be selected multiple times within a session without errors.
2025-09-14 01:14:01 +00:00
2 changed files with 72 additions and 11 deletions
+67 -9
View File
@@ -173,6 +173,12 @@ class dl:
help="Language wanted for Audio, overrides -l/--lang for audio tracks.", help="Language wanted for Audio, overrides -l/--lang for audio tracks.",
) )
@click.option("-sl", "--s-lang", type=LANGUAGE_RANGE, default=["all"], help="Language wanted for Subtitles.") @click.option("-sl", "--s-lang", type=LANGUAGE_RANGE, default=["all"], help="Language wanted for Subtitles.")
@click.option(
"--require-subs",
type=LANGUAGE_RANGE,
default=[],
help="Required subtitle languages. Downloads all subtitles only if these languages exist. Cannot be used with --s-lang.",
)
@click.option("-fs", "--forced-subs", is_flag=True, default=False, help="Include forced subtitle tracks.") @click.option("-fs", "--forced-subs", is_flag=True, default=False, help="Include forced subtitle tracks.")
@click.option( @click.option(
"--proxy", "--proxy",
@@ -263,6 +269,13 @@ class dl:
@click.option( @click.option(
"--reset-cache", "reset_cache", is_flag=True, default=False, help="Clear title cache before fetching." "--reset-cache", "reset_cache", is_flag=True, default=False, help="Clear title cache before fetching."
) )
@click.option(
"--best-available",
"best_available",
is_flag=True,
default=False,
help="Continue with best available quality if requested resolutions are not available.",
)
@click.pass_context @click.pass_context
def cli(ctx: click.Context, **kwargs: Any) -> dl: def cli(ctx: click.Context, **kwargs: Any) -> dl:
return dl(ctx, **kwargs) return dl(ctx, **kwargs)
@@ -322,6 +335,16 @@ class dl:
vault_copy = vault.copy() vault_copy = vault.copy()
del vault_copy["type"] del vault_copy["type"]
if vault_type.lower() == "api" and "decrypt_labs" in vault_name.lower():
if "token" not in vault_copy or not vault_copy["token"]:
if config.decrypt_labs_api_key:
vault_copy["token"] = config.decrypt_labs_api_key
else:
self.log.warning(
f"No token provided for DecryptLabs vault '{vault_name}' and no global "
"decrypt_labs_api_key configured"
)
if vault_type.lower() == "sqlite": if vault_type.lower() == "sqlite":
try: try:
self.vaults.load_critical(vault_type, **vault_copy) self.vaults.load_critical(vault_type, **vault_copy)
@@ -442,6 +465,7 @@ class dl:
v_lang: list[str], v_lang: list[str],
a_lang: list[str], a_lang: list[str],
s_lang: list[str], s_lang: list[str],
require_subs: list[str],
forced_subs: bool, forced_subs: bool,
sub_format: Optional[Subtitle.Codec], sub_format: Optional[Subtitle.Codec],
video_only: bool, video_only: bool,
@@ -462,6 +486,7 @@ class dl:
no_source: bool, no_source: bool,
workers: Optional[int], workers: Optional[int],
downloads: int, downloads: int,
best_available: bool,
*_: Any, *_: Any,
**__: Any, **__: Any,
) -> None: ) -> None:
@@ -469,6 +494,10 @@ class dl:
self.search_source = None self.search_source = None
start_time = time.time() start_time = time.time()
if require_subs and s_lang != ["all"]:
self.log.error("--require-subs and --s-lang cannot be used together")
sys.exit(1)
# Check if dovi_tool is available when hybrid mode is requested # Check if dovi_tool is available when hybrid mode is requested
if any(r == Video.Range.HYBRID for r in range_): if any(r == Video.Range.HYBRID for r in range_):
from unshackle.core.binaries import DoviTool from unshackle.core.binaries import DoviTool
@@ -703,6 +732,12 @@ class dl:
res_list = ", ".join([f"{x}p" for x in missing_resolutions[:-1]]) + " or " res_list = ", ".join([f"{x}p" for x in missing_resolutions[:-1]]) + " or "
res_list = f"{res_list}{missing_resolutions[-1]}p" res_list = f"{res_list}{missing_resolutions[-1]}p"
plural = "s" if len(missing_resolutions) > 1 else "" plural = "s" if len(missing_resolutions) > 1 else ""
if best_available:
self.log.warning(
f"There's no {res_list} Video Track{plural}, continuing with available qualities..."
)
else:
self.log.error(f"There's no {res_list} Video Track{plural}...") self.log.error(f"There's no {res_list} Video Track{plural}...")
sys.exit(1) sys.exit(1)
@@ -740,7 +775,21 @@ class dl:
title.tracks.videos = selected_videos title.tracks.videos = selected_videos
# filter subtitle tracks # filter subtitle tracks
if s_lang and "all" not in s_lang: if require_subs:
missing_langs = [
lang
for lang in require_subs
if not any(is_close_match(lang, [sub.language]) for sub in title.tracks.subtitles)
]
if missing_langs:
self.log.error(f"Required subtitle language(s) not found: {', '.join(missing_langs)}")
sys.exit(1)
self.log.info(
f"Required languages found ({', '.join(require_subs)}), downloading all available subtitles"
)
elif s_lang and "all" not in s_lang:
missing_langs = [ missing_langs = [
lang_ lang_
for lang_ in s_lang for lang_ in s_lang
@@ -880,7 +929,7 @@ class dl:
self.service, self.profile, drm="widevine", quality=highest_quality self.service, self.profile, drm="widevine", quality=highest_quality
) )
if quality_based_cdm and quality_based_cdm != self.cdm: if quality_based_cdm and quality_based_cdm != self.cdm:
self.log.info( self.log.debug(
f"Pre-selecting Widevine CDM based on highest quality {highest_quality}p across all video tracks" f"Pre-selecting Widevine CDM based on highest quality {highest_quality}p across all video tracks"
) )
self.cdm = quality_based_cdm self.cdm = quality_based_cdm
@@ -891,7 +940,7 @@ class dl:
self.service, self.profile, drm="playready", quality=highest_quality self.service, self.profile, drm="playready", quality=highest_quality
) )
if quality_based_cdm and quality_based_cdm != self.cdm: if quality_based_cdm and quality_based_cdm != self.cdm:
self.log.info( self.log.debug(
f"Pre-selecting PlayReady CDM based on highest quality {highest_quality}p across all video tracks" f"Pre-selecting PlayReady CDM based on highest quality {highest_quality}p across all video tracks"
) )
self.cdm = quality_based_cdm self.cdm = quality_based_cdm
@@ -1583,31 +1632,31 @@ class dl:
for key in quality_keys: for key in quality_keys:
if key.isdigit() and quality == int(key): if key.isdigit() and quality == int(key):
quality_match = cdm_name[key] quality_match = cdm_name[key]
self.log.info(f"Selected CDM based on exact quality match {quality}p: {quality_match}") self.log.debug(f"Selected CDM based on exact quality match {quality}p: {quality_match}")
break break
elif key.startswith(">="): elif key.startswith(">="):
threshold = int(key[2:]) threshold = int(key[2:])
if quality >= threshold: if quality >= threshold:
quality_match = cdm_name[key] quality_match = cdm_name[key]
self.log.info(f"Selected CDM based on quality {quality}p >= {threshold}p: {quality_match}") self.log.debug(f"Selected CDM based on quality {quality}p >= {threshold}p: {quality_match}")
break break
elif key.startswith(">"): elif key.startswith(">"):
threshold = int(key[1:]) threshold = int(key[1:])
if quality > threshold: if quality > threshold:
quality_match = cdm_name[key] quality_match = cdm_name[key]
self.log.info(f"Selected CDM based on quality {quality}p > {threshold}p: {quality_match}") self.log.debug(f"Selected CDM based on quality {quality}p > {threshold}p: {quality_match}")
break break
elif key.startswith("<="): elif key.startswith("<="):
threshold = int(key[2:]) threshold = int(key[2:])
if quality <= threshold: if quality <= threshold:
quality_match = cdm_name[key] quality_match = cdm_name[key]
self.log.info(f"Selected CDM based on quality {quality}p <= {threshold}p: {quality_match}") self.log.debug(f"Selected CDM based on quality {quality}p <= {threshold}p: {quality_match}")
break break
elif key.startswith("<"): elif key.startswith("<"):
threshold = int(key[1:]) threshold = int(key[1:])
if quality < threshold: if quality < threshold:
quality_match = cdm_name[key] quality_match = cdm_name[key]
self.log.info(f"Selected CDM based on quality {quality}p < {threshold}p: {quality_match}") self.log.debug(f"Selected CDM based on quality {quality}p < {threshold}p: {quality_match}")
break break
if quality_match: if quality_match:
@@ -1630,13 +1679,22 @@ class dl:
if not cdm_name: if not cdm_name:
return None return None
cdm_api = next(iter(x for x in config.remote_cdm if x["name"] == cdm_name), None) cdm_api = next(iter(x.copy() for x in config.remote_cdm if x["name"] == cdm_name), None)
if cdm_api: if cdm_api:
is_decrypt_lab = True if cdm_api.get("type") == "decrypt_labs" else False is_decrypt_lab = True if cdm_api.get("type") == "decrypt_labs" else False
if is_decrypt_lab: if is_decrypt_lab:
del cdm_api["name"] del cdm_api["name"]
del cdm_api["type"] del cdm_api["type"]
if "secret" not in cdm_api or not cdm_api["secret"]:
if config.decrypt_labs_api_key:
cdm_api["secret"] = config.decrypt_labs_api_key
else:
raise ValueError(
f"No secret provided for DecryptLabs CDM '{cdm_name}' and no global "
"decrypt_labs_api_key configured"
)
# All DecryptLabs CDMs use DecryptLabsRemoteCDM # All DecryptLabs CDMs use DecryptLabsRemoteCDM
return DecryptLabsRemoteCDM(service_name=service, vaults=self.vaults, **cdm_api) return DecryptLabsRemoteCDM(service_name=service, vaults=self.vaults, **cdm_api)
else: else:
@@ -150,6 +150,7 @@ def download(
track_type = track.__class__.__name__ track_type = track.__class__.__name__
thread_count = str(config.n_m3u8dl_re.get("thread_count", max_workers)) thread_count = str(config.n_m3u8dl_re.get("thread_count", max_workers))
retry_count = str(config.n_m3u8dl_re.get("retry_count", max_workers))
ad_keyword = config.n_m3u8dl_re.get("ad_keyword") ad_keyword = config.n_m3u8dl_re.get("ad_keyword")
arguments = [ arguments = [
@@ -160,6 +161,8 @@ def download(
output_dir, output_dir,
"--thread-count", "--thread-count",
thread_count, thread_count,
"--download-retry-count",
retry_count,
"--no-log", "--no-log",
"--write-meta-json", "--write-meta-json",
"false", "false",