Add functionality to retrieve video title and description using yt-dlp

This commit is contained in:
Cesar Mendivil 2026-02-28 17:32:55 -07:00
parent 344fd5809a
commit 8706a4f3f7

43
main.py
View File

@ -804,7 +804,7 @@ def stream_endpoint(video_id: str):
"""
stream_url, error = get_stream_url(video_id)
if error:
raise HTTPException(status_code=400, detail=error)
@ -812,13 +812,50 @@ def stream_endpoint(video_id: str):
# Determinar el tipo de URL obtenida
url_type = "unknown"
if "m3u8" in stream_url.lower():
if stream_url and "m3u8" in stream_url.lower():
url_type = "m3u8/hls"
elif "googlevideo.com" in stream_url:
elif stream_url and "googlevideo.com" in stream_url:
url_type = "direct/mp4"
# Obtener title y description con yt-dlp --dump-json
title = None
description = None
try:
_cookie_mgr = CookieManager()
_cookiefile = _cookie_mgr.get_cookiefile_path()
_cookies_path = _cookiefile or os.getenv('API_COOKIES_PATH', DEFAULT_COOKIES_PATH)
_proxy = os.getenv('API_PROXY', DEFAULT_PROXY) or None
_cmd = [
"yt-dlp",
"--skip-download",
"--dump-json",
"--no-warnings",
"--extractor-args", "youtube:player_client=android",
f"https://www.youtube.com/watch?v={video_id}"
]
if _cookiefile:
_cmd.extend(["--cookies", _cookiefile])
if _proxy:
_cmd.extend(["--proxy", _proxy])
_proc = subprocess.run(_cmd, capture_output=True, text=True, timeout=60)
if _proc.returncode == 0 and _proc.stdout:
_meta = json.loads(_proc.stdout)
title = _meta.get("title")
description = _meta.get("description")
except Exception:
pass
finally:
try:
_cookie_mgr.cleanup()
except Exception:
pass
return {
"video_id": video_id,
"title": title,
"description": description,
"stream_url": stream_url,
"url_type": url_type,
"youtube_url": f"https://www.youtube.com/watch?v={video_id}",