clipwright/yt_clients.py
juancarlos.cavero 60fa37a969 fix(gpu,proxy): release ASR models in the API, probe one video not the playlist
Three failures measured in prod on 17-sep-2026, one deploy.

1. 5 of 12 jobs failed with "ffmpeg could not cut clip ... exit 187, 0 bytes".
   Exit 187 is AVERROR_EXIT after "h264_nvenc: Could not open encoder":
   VRAM was full. The uvicorn process itself held 7.7 GB of the 20 GB card
   while idle: the thumbnail studio and /api/subtitle on a dubbed clip
   transcribe in-process, and the whisper (ctranslate2) and parakeet
   (onnxruntime CUDA) singletons then lived in the API for good. Both call
   transcribe_backends.release_models() now; the loaders fetch their model
   inside the ASR gate so a release can never pull it from under a running
   transcription.

2. 26 of the 37 probes that reached the per-GB proxy in 10 days were
   `watch?v=X&list=...` or mix links. Neither the probe nor the download set
   `noplaylist`, so yt-dlp walked the whole list, died on its first private /
   age-gated / bot-checked entry (a video nobody pasted), the probe read it
   as an IP problem and paid the proxy to walk the same list again, and the
   user got a 400 for a valid link. Reproduced in the prod container: the
   same URL with noplaylist answers in 9.6 s on a static. The probe also
   keeps every attempt's error per route now, so the anonymous retry's
   bot-check cannot overwrite a "confirm your age" from the cookie attempt
   into an escalation.

3. noplaylist does nothing for a search or a bare playlist URL: one
   `results?search_query=` held the probe thread for 2220 s in the container
   and still failed. Search, playlist, channel and feed URLs are refused by
   path before any request, with a message that says what to paste.
2026-09-17 17:19:57 +02:00

96 lines
4.3 KiB
Python

"""yt-dlp client selection for YouTube, shared by the download (``main.py``)
and the duration probe (``cloud/metering.py``) so the two cannot drift.
Why the client list is explicit. With account cookies yt-dlp's defaults are
``tv_downgraded`` + ``web``, and for a share of videos both come back
UNPLAYABLE / SABR-only, which surfaces as "Video unavailable" with no
formats. That looked like an IP ban (it also happened on every static ISP IP)
and for a week sent ~26 downloads a week to the per-GB proxy, which then
downloaded 360p through the same dead client list. Measured on 6-sep-2026
inside the prod container, same static proxy, same video:
cookies + default clients -> Video unavailable
cookies + mweb (+ PO token) -> 1080p
no cookies + default clients -> 1080p
cookies + default,mweb (+ PO token) -> 1080p (also on a video that worked)
``mweb`` needs a GVS PO token for its https formats, which the bgutil
provider mints as long as the webpage is NOT skipped (``player_skip:
webpage`` drops the account's Data Sync ID and the token request fails).
The old fallback list (``tv_embed``, ``android``) is gone from yt-dlp: the
first is "unsupported", the second is skipped whenever cookies are present.
"""
HD_CLIENTS = ["default", "mweb"]
class NotASingleVideo(ValueError):
"""The URL is a YouTube page that is not one video (search, playlist,
channel). Raised before yt-dlp ever sees it, by the probe and the
download alike."""
def youtube_non_video_reason(url):
"""Why this YouTube URL is not a single video, or None if it is one.
A search page or a bare playlist is walked entry by entry by yt-dlp even
with ``noplaylist`` (that option only strips the ``list=`` off a
``watch`` link). Measured in the prod container on 17-sep-2026: a
``results?search_query=`` URL held the probe's executor thread for
2220 s and still failed on an entry that "premieres in 4 days", after
the same URL had paid the per-GB proxy three times in a day. So the
check is by path, up front and free: nothing that is not one video is
worth a single request.
"""
from urllib.parse import urlparse, parse_qs
try:
parts = urlparse(url or "")
except ValueError:
return None
host = (parts.hostname or "").lower()
if host == "youtu.be":
return None if parts.path.strip("/") else "youtu.be link without a video id"
if not (host == "youtube.com" or host.endswith(".youtube.com")):
return None
path = parts.path.rstrip("/") or "/"
if path == "/watch":
return None if parse_qs(parts.query).get("v") else "a watch page without a video id"
first = path.split("/")[1] if path != "/" else ""
if first in ("shorts", "live", "embed", "v", "e"):
return None
if first == "results":
return "a search results page, not a video"
if first == "playlist":
return "a playlist, not a video; open one video in it and paste that link"
if first.startswith("@") or first in ("channel", "c", "user"):
return "a channel page, not a video"
if first in ("feed", "", "gaming", "music"):
return "a YouTube page with no video in it"
return None
def pot_provider_args(bgutil_http, bgutil_script):
"""Extractor args selecting the PO token provider (bgutil http or script)."""
if bgutil_http:
return {"youtubepot-bgutilhttp": {"base_url": [bgutil_http]}}
if bgutil_script:
return {"youtubepot-bgutilscript": {"script_path": [bgutil_script]}}
return {}
def hd_extractor_args(bgutil_http, bgutil_script):
"""Extractor args for the HD attempt, or None when no PO token provider
is configured (the HD path needs one; the plan then skips it)."""
provider = pot_provider_args(bgutil_http, bgutil_script)
if not provider:
return None
return {**provider, "youtube": {"player_client": list(HD_CLIENTS)}}
def fallback_extractor_args(bgutil_http, bgutil_script):
"""Extractor args for the conservative attempt: same live client list,
the provider when there is one, and never ``player_skip`` (see module
docstring). Without a provider mweb still serves the progressive 360p
format, which is enough for a probe and better than no download."""
return {**pot_provider_args(bgutil_http, bgutil_script),
"youtube": {"player_client": list(HD_CLIENTS)}}