Prod 20-ago, 5 of 12 managed jobs failed and the alert said 'youtube download' — no download had terminally failed. Three fixes: - The quality probe now reports source duration, and /api/process rejects sources under MIN_SOURCE_SECONDS (default 45, 0 disables) on all three ingest paths with a clear message. A 24s YouTube Short cannot yield 15-60s clips; Gemini returns nothing and the job burns managed minutes to die with 'no usable clips' (3 of the 5 failures, one user retrying the same cat video). - _job_error_text drops per-attempt download warnings when a later attempt succeeded (HD-direct fails on every job since the server IP ban; static proxies recover it), and gained an 'Error:' marker so raised exceptions (RuntimeError:, GeminiBlockedError:) reach the alert instead of a bare 'Traceback ... exit code 1'. The classifier names 'no clips found (user video)' as its own content-shaped category. - (env, no code) DIRECT_FIRST=0 in prod: with the server IP banned the direct attempt only added ~10s and the noise behind these alerts.
109 lines
3.4 KiB
Python
109 lines
3.4 KiB
Python
"""
|
|
Fast pre-flight probe: what maximum resolution does YouTube offer for a URL?
|
|
|
|
Run as a short subprocess by app.py BEFORE a job starts, so the user can decide
|
|
to abort (and e.g. refresh cookies) instead of burning 20+ minutes of
|
|
transcription and rendering on a 360p-only source.
|
|
|
|
Prints JSON to stdout: {"max_height": int, "mode": str, "cookies_invalid": bool}
|
|
Always exits 0 — the caller treats probe failures as "unknown" and starts the
|
|
job anyway (fail-open), where the in-job warning still applies.
|
|
"""
|
|
import argparse
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
|
|
def _find_cookies_path():
|
|
# Mirrors main.py's cookie discovery.
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
|
for candidate in [
|
|
os.path.join(script_dir, 'www.youtube.com_cookies.txt'),
|
|
os.path.join(script_dir, 'cookies.txt'),
|
|
'/app/cookies.txt',
|
|
]:
|
|
if os.path.exists(candidate):
|
|
return candidate
|
|
return None
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description="Probe available YouTube quality for a URL.")
|
|
parser.add_argument("--url", required=True)
|
|
args = parser.parse_args()
|
|
|
|
result = {"max_height": 0, "mode": None, "cookies_invalid": False, "duration": 0}
|
|
try:
|
|
import yt_dlp
|
|
|
|
warnings = []
|
|
|
|
class _CollectLogger:
|
|
def debug(self, msg):
|
|
pass
|
|
|
|
def info(self, msg):
|
|
pass
|
|
|
|
def warning(self, msg):
|
|
warnings.append(str(msg))
|
|
|
|
def error(self, msg):
|
|
warnings.append(str(msg))
|
|
|
|
cookies_path = _find_cookies_path()
|
|
base_opts = {
|
|
'quiet': True,
|
|
'no_warnings': False,
|
|
'logger': _CollectLogger(),
|
|
'socket_timeout': 20,
|
|
'retries': 2,
|
|
'nocheckcertificate': True,
|
|
'cachedir': False,
|
|
}
|
|
# yt-dlp's default player clients (tv_downgraded/web_safari with
|
|
# cookies, android_vr/web_safari anonymous) are the ones that still
|
|
# serve HD without PO tokens — never override them here.
|
|
attempts = []
|
|
if cookies_path:
|
|
attempts.append(("cookie-auth", {**base_opts, 'cookiefile': cookies_path}))
|
|
attempts.append(("anonymous", {**base_opts, 'cookiefile': None}))
|
|
|
|
for mode, opts in attempts:
|
|
try:
|
|
with yt_dlp.YoutubeDL(opts) as ydl:
|
|
info = ydl.extract_info(args.url, download=False)
|
|
formats = info.get('formats') or []
|
|
heights = [
|
|
f.get('height') or 0
|
|
for f in formats
|
|
if f.get('vcodec', 'none') != 'none'
|
|
and f.get('protocol') != 'mhtml'
|
|
and f.get('ext') != 'mhtml'
|
|
]
|
|
max_height = max(heights, default=0)
|
|
if max_height > result["max_height"]:
|
|
result["max_height"] = max_height
|
|
result["mode"] = mode
|
|
if not result["duration"]:
|
|
result["duration"] = int(info.get('duration') or 0)
|
|
if result["max_height"] >= 1080:
|
|
break
|
|
except Exception:
|
|
continue
|
|
|
|
result["cookies_invalid"] = any("no longer valid" in w for w in warnings)
|
|
except Exception:
|
|
pass
|
|
|
|
print(json.dumps(result))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|