From 8706a4f3f71be333f89ae92ca89693cc23188e92 Mon Sep 17 00:00:00 2001 From: Cesar Mendivil Date: Sat, 28 Feb 2026 17:32:55 -0700 Subject: [PATCH] Add functionality to retrieve video title and description using yt-dlp --- main.py | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 79e0005..7aa006c 100644 --- a/main.py +++ b/main.py @@ -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}",