f
This commit is contained in:
@@ -726,7 +726,7 @@ class YtDlpDefaults:
|
||||
|
||||
format: str = "best"
|
||||
video_format: str = "bestvideo+bestaudio/best"
|
||||
audio_format: str = "251/140/bestaudio"
|
||||
audio_format: str = "bestaudio/best"
|
||||
format_sort: Optional[List[str]] = None
|
||||
cookies_from_browser: Optional[str] = None
|
||||
|
||||
@@ -958,6 +958,36 @@ class YtDlpTool:
|
||||
# Add browser cookies support "just in case" if no file found (best effort)
|
||||
_add_browser_cookies_if_available(base_options)
|
||||
|
||||
# YouTube hardening: prefer browser cookies + mobile/web clients when available
|
||||
try:
|
||||
netloc = urlparse(opts.url).netloc.lower()
|
||||
except Exception:
|
||||
netloc = ""
|
||||
is_youtube = ("youtube.com" in netloc) or ("youtu.be" in netloc)
|
||||
if is_youtube:
|
||||
# Prefer browser cookies over a potentially stale cookiefile when available
|
||||
pref = (self.defaults.cookies_from_browser or "").lower().strip()
|
||||
if pref not in {"none", "off", "false"} and _has_browser_cookie_database():
|
||||
_add_browser_cookies_if_available(
|
||||
base_options,
|
||||
preferred_browser=None if pref in {"", "auto", "detect"} else pref,
|
||||
)
|
||||
if base_options.get("cookiesfrombrowser"):
|
||||
base_options.pop("cookiefile", None)
|
||||
debug("[ytdlp] Using browser cookies for YouTube; ignoring cookiefile")
|
||||
|
||||
extractor_args = base_options.get("extractor_args")
|
||||
if not isinstance(extractor_args, dict):
|
||||
extractor_args = {}
|
||||
youtube_args = extractor_args.get("youtube")
|
||||
if not isinstance(youtube_args, dict):
|
||||
youtube_args = {}
|
||||
if "player_client" not in youtube_args:
|
||||
youtube_args["player_client"] = ["android", "web"]
|
||||
debug("[ytdlp] Using YouTube player_client override: android, web")
|
||||
extractor_args["youtube"] = youtube_args
|
||||
base_options["extractor_args"] = extractor_args
|
||||
|
||||
# Special handling for format keywords explicitly passed in via options
|
||||
if opts.ytdl_format == "audio":
|
||||
try:
|
||||
@@ -1014,7 +1044,14 @@ class YtDlpTool:
|
||||
if resolved:
|
||||
ytdl_format = resolved
|
||||
|
||||
fmt = ytdl_format or self.default_format(opts.mode)
|
||||
default_fmt = self.default_format(opts.mode)
|
||||
fmt = ytdl_format or default_fmt
|
||||
debug(
|
||||
"[ytdlp] build options: "
|
||||
f"mode={opts.mode}, ytdl_format={ytdl_format}, default_format={default_fmt}, final_format={fmt}, "
|
||||
f"cookiefile={base_options.get('cookiefile')}, cookiesfrombrowser={base_options.get('cookiesfrombrowser')}, "
|
||||
f"player_client={((base_options.get('extractor_args') or {}).get('youtube') or {}).get('player_client')}"
|
||||
)
|
||||
base_options["format"] = fmt
|
||||
|
||||
if opts.mode == "audio":
|
||||
@@ -1870,6 +1907,12 @@ def download_media(opts: DownloadOptions, *, config: Optional[Dict[str, Any]] =
|
||||
extractor_args["youtube"] = youtube_args
|
||||
fallback_options["extractor_args"] = extractor_args
|
||||
|
||||
debug(
|
||||
"[ytdlp] retry options: "
|
||||
f"cookiefile={fallback_options.get('cookiefile')}, cookiesfrombrowser={fallback_options.get('cookiesfrombrowser')}, "
|
||||
f"player_client={((fallback_options.get('extractor_args') or {}).get('youtube') or {}).get('player_client')}"
|
||||
)
|
||||
|
||||
with yt_dlp.YoutubeDL(fallback_options) as ydl: # type: ignore[arg-type]
|
||||
info = cast(Dict[str, Any], ydl.extract_info(opts.url, download=True))
|
||||
except Exception as exc2:
|
||||
|
||||
Reference in New Issue
Block a user