forked from kenzuya/unshackle
feat(dl): add --audio-description-only and --forced-subs-only
Each keeps one kind of track inside its track type. A run of more than one title skips a title that has no such track. The REST API rejects a non-boolean value for a boolean dl param, and the DASH parser reads audio description from Role elements.
This commit is contained in:
+80
-12
@@ -93,6 +93,7 @@ from unshackle.core.utilities import (
|
||||
is_close_match,
|
||||
is_exact_match,
|
||||
keep_forced_subtitle,
|
||||
kind_only_conflict,
|
||||
log_event,
|
||||
missing_required_langs,
|
||||
partition_exclusions,
|
||||
@@ -756,6 +757,13 @@ class dl:
|
||||
help="Subtitle languages that must exist. The title fails if one is missing. -sl still decides what to keep.",
|
||||
)
|
||||
@click.option("-fs", "--forced-subs", is_flag=True, default=False, help="Include forced subtitle tracks.")
|
||||
@click.option(
|
||||
"-fso",
|
||||
"--forced-subs-only",
|
||||
is_flag=True,
|
||||
default=False,
|
||||
help="Download forced subtitle tracks and no other subtitle tracks.",
|
||||
)
|
||||
@click.option(
|
||||
"-fsl",
|
||||
"--forced-s-lang",
|
||||
@@ -882,6 +890,13 @@ class dl:
|
||||
help="Do not download or mux attachments (cover art, subtitle fonts, and files the service attaches).",
|
||||
)
|
||||
@click.option("-ad", "--audio-description", is_flag=True, default=False, help="Download audio description tracks.")
|
||||
@click.option(
|
||||
"-ado",
|
||||
"--audio-description-only",
|
||||
is_flag=True,
|
||||
default=False,
|
||||
help="Download audio description tracks and no standard audio tracks.",
|
||||
)
|
||||
@click.option(
|
||||
"--slow",
|
||||
type=SLOW_DELAY_RANGE,
|
||||
@@ -1523,6 +1538,7 @@ class dl:
|
||||
require_video: list[str],
|
||||
require_subs: list[str],
|
||||
forced_subs: bool,
|
||||
forced_subs_only: bool,
|
||||
forced_s_lang: list[str],
|
||||
exact_lang: bool,
|
||||
sub_format: Optional[Union[Subtitle.Codec, str]],
|
||||
@@ -1537,6 +1553,7 @@ class dl:
|
||||
no_video: bool,
|
||||
no_attachments: bool,
|
||||
audio_description: bool,
|
||||
audio_description_only: bool,
|
||||
slow: Optional[tuple[int, int]],
|
||||
list_: bool,
|
||||
list_titles: bool,
|
||||
@@ -1639,7 +1656,27 @@ class dl:
|
||||
if excludes and name in parent_params:
|
||||
parent_params[name] = includes
|
||||
|
||||
if forced_s_lang or fsl_excl:
|
||||
conflict = kind_only_conflict(
|
||||
{
|
||||
"audio_description_only": audio_description_only,
|
||||
"forced_subs_only": forced_subs_only,
|
||||
"no_audio": no_audio,
|
||||
"no_subs": no_subs,
|
||||
"video_only": video_only,
|
||||
"audio_only": audio_only,
|
||||
"subs_only": subs_only,
|
||||
"chapters_only": chapters_only,
|
||||
}
|
||||
)
|
||||
if conflict:
|
||||
first, second = (f"--{name.replace('_', '-')}" for name in conflict)
|
||||
self.log.error(f"{first} cannot be used with {second}: {second} drops the tracks that {first} selects.")
|
||||
sys.exit(1)
|
||||
|
||||
if audio_description_only:
|
||||
audio_description = True
|
||||
|
||||
if forced_s_lang or fsl_excl or forced_subs_only:
|
||||
# services read forced_subs from raw ctx params, so the implication must be pushed there too
|
||||
forced_subs = True
|
||||
if "all" in forced_s_lang:
|
||||
@@ -2044,6 +2081,27 @@ class dl:
|
||||
post_script_last: dict[Any, dict[Path, dict[str, str]]] = {}
|
||||
post_script_folders: list[Path] = []
|
||||
post_script_sample: dict[str, str] = {}
|
||||
wanted_total = sum(post_script_pending.values())
|
||||
|
||||
def settle_post_script_group(title: Any) -> None:
|
||||
"""Count one title of its group as done; send the season event after the last one."""
|
||||
group_key = post_script_group(title)
|
||||
post_script_pending[group_key] = post_script_pending.get(group_key, 1) - 1
|
||||
if post_script_pending[group_key] <= 0:
|
||||
for folder, context in post_script_last.get(group_key, {}).items():
|
||||
dispatch("success", "season", season_context(context, folder), postscript)
|
||||
|
||||
def skip_title(title: Any, reason: str) -> None:
|
||||
"""Skip a title that has no track of the kind an only flag selects.
|
||||
|
||||
A run with one title stops instead, because it has nothing else to download.
|
||||
"""
|
||||
if wanted_total <= 1:
|
||||
self.log.error(reason)
|
||||
sys.exit(1)
|
||||
self.log.warning(f"{reason} Skipping {title}")
|
||||
if not no_mux:
|
||||
settle_post_script_group(title)
|
||||
|
||||
for i, title in enumerate(titles):
|
||||
v_lang, a_lang, s_lang, range_ = base_selection
|
||||
@@ -2316,13 +2374,14 @@ class dl:
|
||||
(
|
||||
"audio",
|
||||
require_audio if keep_audio else [],
|
||||
[a.language for a in title.tracks.audio] + embedded_audio_langs(title.tracks.videos, keep_videos),
|
||||
[a.language for a in title.tracks.audio if a.descriptive or not audio_description_only]
|
||||
+ embedded_audio_langs(title.tracks.videos, keep_videos and not audio_description_only),
|
||||
),
|
||||
("video", require_video if keep_videos else [], [v.language for v in title.tracks.videos]),
|
||||
(
|
||||
"subtitle",
|
||||
require_subs if keep_subtitles else [],
|
||||
[t.language for t in title.tracks.subtitles],
|
||||
[t.language for t in title.tracks.subtitles if t.forced or not forced_subs_only],
|
||||
),
|
||||
):
|
||||
missing_required = missing_required_langs(required, available, title.language, exact=exact_lang)
|
||||
@@ -2749,7 +2808,17 @@ class dl:
|
||||
fsl_excl_r, [t.language for t in title.tracks.subtitles if t.forced], exact_lang
|
||||
)
|
||||
title.tracks.select_subtitles(lambda x: not (x.forced and str(x.language) in drop))
|
||||
if forced_subs_only:
|
||||
title.tracks.select_subtitles(lambda x: x.forced)
|
||||
if not title.tracks.subtitles:
|
||||
skip_title(title, "There's no forced subtitle track for the selected languages.")
|
||||
continue
|
||||
|
||||
if keep_audio and audio_description_only:
|
||||
title.tracks.select_audio(lambda x: x.descriptive)
|
||||
if not title.tracks.audio:
|
||||
skip_title(title, "There's no audio description track.")
|
||||
continue
|
||||
# might have no audio tracks if part of the video, e.g. transport stream hls
|
||||
if keep_audio and len(title.tracks.audio) > 0:
|
||||
if not audio_description:
|
||||
@@ -2818,7 +2887,10 @@ class dl:
|
||||
if a_orig_token in audio_languages:
|
||||
a_orig_token = None
|
||||
|
||||
embedded_langs = embedded_audio_langs(title.tracks.videos, keep_videos)
|
||||
embedded_langs = embedded_audio_langs(
|
||||
title.tracks.videos, keep_videos and not audio_description_only
|
||||
)
|
||||
audio_noun = "audio description tracks" if audio_description_only else "audio tracks"
|
||||
|
||||
if not any(tok in processed_lang for tok in ("best", "all")):
|
||||
missing_a_langs = find_missing_langs(
|
||||
@@ -2832,17 +2904,17 @@ class dl:
|
||||
remaining = [tok for tok in processed_lang if tok not in missing_a_langs]
|
||||
if remaining:
|
||||
self.log.warning(
|
||||
f"{missing_str} not found in audio tracks, "
|
||||
f"{missing_str} not found in {audio_noun}, "
|
||||
f"continuing with: {as_requested(remaining, a_orig_token)}"
|
||||
)
|
||||
processed_lang = remaining
|
||||
else:
|
||||
self.log.error(
|
||||
f"{missing_str} not found in audio tracks and no fallback available"
|
||||
f"{missing_str} not found in {audio_noun} and no fallback available"
|
||||
)
|
||||
sys.exit(1)
|
||||
else:
|
||||
self.log.error(missing_str + " not found in audio tracks")
|
||||
self.log.error(f"{missing_str} not found in {audio_noun}")
|
||||
sys.exit(1)
|
||||
|
||||
title.tracks.audio = select_best_audio(
|
||||
@@ -3700,11 +3772,7 @@ class dl:
|
||||
post_script_folders.append(final_path.parent)
|
||||
|
||||
if not no_mux:
|
||||
group_key = post_script_group(title)
|
||||
post_script_pending[group_key] = post_script_pending.get(group_key, 1) - 1
|
||||
if post_script_pending[group_key] <= 0:
|
||||
for folder, context in post_script_last.get(group_key, {}).items():
|
||||
dispatch("success", "season", season_context(context, folder), postscript)
|
||||
settle_post_script_group(title)
|
||||
|
||||
title_dl_time = time_elapsed_since(dl_start_time)
|
||||
downloaded_label = "Track" if isinstance(title, Song) else "Title"
|
||||
|
||||
@@ -601,6 +601,7 @@ def perform_download(
|
||||
require_video=params.get("require_video", []),
|
||||
require_subs=params.get("require_subs", []),
|
||||
forced_subs=ctx.params["forced_subs"],
|
||||
forced_subs_only=params.get("forced_subs_only", False),
|
||||
forced_s_lang=params.get("forced_s_lang", []),
|
||||
exact_lang=params.get("exact_lang", False),
|
||||
sub_format=params.get("sub_format"),
|
||||
@@ -615,6 +616,7 @@ def perform_download(
|
||||
no_video=params.get("no_video", False),
|
||||
no_attachments=params.get("no_attachments", False),
|
||||
audio_description=params.get("audio_description", False),
|
||||
audio_description_only=params.get("audio_description_only", False),
|
||||
slow=params.get("slow", None),
|
||||
list_=False,
|
||||
list_titles=False,
|
||||
|
||||
@@ -33,7 +33,7 @@ from unshackle.core.proxies.resolve import initialize_proxy_providers, resolve_p
|
||||
from unshackle.core.services import Services
|
||||
from unshackle.core.titles import Episode, Movie, Song, Title_T
|
||||
from unshackle.core.tracks import Audio, Subtitle, Tracks, Video
|
||||
from unshackle.core.utilities import declared_kwargs
|
||||
from unshackle.core.utilities import declared_kwargs, kind_only_conflict
|
||||
from unshackle.core.utils.click_types import AUDIO_CODEC_LIST, SUBTITLE_CODEC, VIDEO_CODEC_LIST
|
||||
from unshackle.core.utils.collections import ci_get
|
||||
from unshackle.core.utils.redact import REDACTED, URL_USERINFO_RE, redact_all, redact_secrets, redact_text
|
||||
@@ -64,6 +64,7 @@ DEFAULT_DOWNLOAD_PARAMS = {
|
||||
"require_video": [],
|
||||
"require_subs": [],
|
||||
"forced_subs": False,
|
||||
"forced_subs_only": False,
|
||||
"forced_s_lang": [],
|
||||
"exact_lang": False,
|
||||
"sub_format": None,
|
||||
@@ -77,6 +78,7 @@ DEFAULT_DOWNLOAD_PARAMS = {
|
||||
"no_video": False,
|
||||
"no_attachments": False,
|
||||
"audio_description": False,
|
||||
"audio_description_only": False,
|
||||
"slow": None,
|
||||
"split_audio": None,
|
||||
"skip_dl": False,
|
||||
@@ -1925,6 +1927,13 @@ def validate_download_parameters(data: Dict[str, Any]) -> Optional[str]:
|
||||
return "Cannot use both no_subs and subs_only"
|
||||
if data.get("no_audio") and data.get("audio_only"):
|
||||
return "Cannot use both no_audio and audio_only"
|
||||
conflict = kind_only_conflict(data)
|
||||
if conflict:
|
||||
return f"Cannot use both {conflict[0]} and {conflict[1]}"
|
||||
|
||||
for key, default in DEFAULT_DOWNLOAD_PARAMS.items():
|
||||
if isinstance(default, bool) and data.get(key) is not None and not isinstance(data[key], bool):
|
||||
return f"{key} must be a boolean"
|
||||
|
||||
for key in ("require_audio", "require_video", "require_subs"):
|
||||
value = data.get(key)
|
||||
|
||||
@@ -895,6 +895,9 @@ async def download(request: web.Request) -> web.Response:
|
||||
forced_subs:
|
||||
type: boolean
|
||||
description: Include forced subtitle tracks (default - false)
|
||||
forced_subs_only:
|
||||
type: boolean
|
||||
description: Download forced subtitle tracks and no other subtitle tracks, implies forced_subs (default - false)
|
||||
forced_s_lang:
|
||||
type: array
|
||||
items:
|
||||
@@ -936,6 +939,9 @@ async def download(request: web.Request) -> web.Response:
|
||||
audio_description:
|
||||
type: boolean
|
||||
description: Download audio description tracks (default - false)
|
||||
audio_description_only:
|
||||
type: boolean
|
||||
description: Download audio description tracks and no standard audio tracks (default - false)
|
||||
slow:
|
||||
oneOf:
|
||||
- type: boolean
|
||||
|
||||
@@ -1209,7 +1209,7 @@ class DASH:
|
||||
("urn:mpeg:dash:role:2011", "descriptive"),
|
||||
("urn:tva:metadata:cs:AudioPurposeCS:2007", "1"),
|
||||
)
|
||||
for x in adaptation_set.findall("Accessibility")
|
||||
for x in (*adaptation_set.findall("Accessibility"), *adaptation_set.findall("Role"))
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -17,7 +17,7 @@ from collections import defaultdict
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from types import ModuleType
|
||||
from typing import Any, Callable, Optional, Sequence, Union
|
||||
from typing import Any, Callable, Mapping, Optional, Sequence, Union
|
||||
from urllib.parse import ParseResult, urlparse
|
||||
from uuid import uuid4
|
||||
|
||||
@@ -235,6 +235,29 @@ def keep_forced_subtitle(
|
||||
return match_func(language, forced_s_lang)
|
||||
|
||||
|
||||
def kind_only_conflict(params: Mapping[str, Any]) -> Optional[tuple[str, str]]:
|
||||
"""
|
||||
Return the first (kind-only flag, flag that drops its track type) pair that are both set.
|
||||
|
||||
The ``*_only`` track-type flags combine, so another one only drops the kind-only
|
||||
flag's track type when its own ``*_only`` flag is not set as well.
|
||||
"""
|
||||
for flag, own_only, no_flag in (
|
||||
("audio_description_only", "audio_only", "no_audio"),
|
||||
("forced_subs_only", "subs_only", "no_subs"),
|
||||
):
|
||||
if not params.get(flag):
|
||||
continue
|
||||
if params.get(no_flag):
|
||||
return flag, no_flag
|
||||
if params.get(own_only):
|
||||
continue
|
||||
for other in ("video_only", "audio_only", "subs_only", "chapters_only"):
|
||||
if params.get(other):
|
||||
return flag, other
|
||||
return None
|
||||
|
||||
|
||||
def embedded_audio_langs(videos: Sequence[Any], keep_videos: bool) -> list[str]:
|
||||
"""
|
||||
Return the audio languages carried inside video tracks rather than beside them.
|
||||
|
||||
Reference in New Issue
Block a user