This commit is contained in:
2026-01-21 14:06:18 -08:00
parent 7a357f45bd
commit 3c19109222
2 changed files with 52 additions and 6 deletions

View File

@@ -226,6 +226,8 @@ def is_url_supported_by_ytdlp(url: str) -> bool:
return False
_FORMATS_CACHE: Dict[str, tuple[float, List[Dict[str, Any]]]] = {}
def list_formats(
url: str,
*,
@@ -242,6 +244,14 @@ def list_formats(
if not is_url_supported_by_ytdlp(url):
return None
# Cache format probes to avoid redundant network hits
cache_key = hashlib.md5(f"{url}|{no_playlist}|{playlist_items}|{cookiefile}".encode()).hexdigest()
now = time.monotonic()
if cache_key in _FORMATS_CACHE:
ts, result = _FORMATS_CACHE[cache_key]
if now - ts < 300: # 5 minute cache for formats
return result
result_container: List[Optional[Any]] = [None, None] # [result, error]
def _do_list() -> None:
@@ -262,7 +272,7 @@ def list_formats(
ydl_opts["cookiefile"] = str(cookiefile)
else:
# Best effort attempt to use browser cookies if no file is explicitly passed
ydl_opts["cookiesfrombrowser"] = "chrome"
ydl_opts["cookiesfrombrowser"] = ["chrome"]
if no_playlist:
ydl_opts["noplaylist"] = True
@@ -302,9 +312,14 @@ def list_formats(
if result_container[1] is not None:
return None
if result_container[0] is not None:
_FORMATS_CACHE[cache_key] = (now, cast(List[Dict[str, Any]], result_container[0]))
return cast(Optional[List[Dict[str, Any]]], result_container[0])
_PROBE_CACHE: Dict[str, tuple[float, Dict[str, Any]]] = {}
def probe_url(
url: str,
no_playlist: bool = False,
@@ -320,6 +335,14 @@ def probe_url(
if not is_url_supported_by_ytdlp(url):
return None
# Simple in-memory cache to avoid duplicate probes for the same URL/options in a short window.
cache_key = hashlib.md5(f"{url}|{no_playlist}|{cookiefile}".encode()).hexdigest()
now = time.monotonic()
if cache_key in _PROBE_CACHE:
ts, result = _PROBE_CACHE[cache_key]
if now - ts < 60: # 60 second cache
return result
result_container: List[Optional[Any]] = [None, None] # [result, error]
def _do_probe() -> None:
@@ -340,6 +363,10 @@ def probe_url(
if cookiefile:
ydl_opts["cookiefile"] = str(cookiefile)
else:
# Best effort fallback
ydl_opts["cookiesfrombrowser"] = ["chrome"]
if no_playlist:
ydl_opts["noplaylist"] = True
@@ -381,6 +408,9 @@ def probe_url(
if result_container[1] is not None:
return None
if result_container[0] is not None:
_PROBE_CACHE[cache_key] = (now, cast(Dict[str, Any], result_container[0]))
return cast(Optional[Dict[str, Any]], result_container[0])
@@ -678,7 +708,7 @@ class YtDlpTool:
# Add browser cookies support "just in case" if no file found (best effort)
# This uses yt-dlp's support for extracting from common browsers.
# Defaulting to 'chrome' as the most common path.
base_options["cookiesfrombrowser"] = "chrome"
base_options["cookiesfrombrowser"] = ["chrome"]
# Special handling for format keywords
if opts.ytdl_format == "audio":