#!/usr/bin/env python3 """ Script de prueba para extraer URL m3u8 de un video de YouTube en vivo """ import subprocess import sys def test_get_m3u8_url(video_url): """ Prueba la extracción de URL m3u8 de un video de YouTube """ print(f"🔍 Probando extracción de URL m3u8...") print(f"📺 Video: {video_url}\n") command = [ "yt-dlp", "-g", # Obtener solo la URL "-f", "best[ext=m3u8]/bestvideo[ext=m3u8]+bestaudio[ext=m3u8]/best", # Preferir m3u8 "--no-warnings", # Sin advertencias video_url ] try: print("⏳ Ejecutando yt-dlp...") result = subprocess.run(command, capture_output=True, text=True, timeout=30) if result.returncode != 0: print(f"❌ Error: {result.stderr}") return None # Obtener todas las URLs urls = result.stdout.strip().split('\n') print(f"✅ Se encontraron {len(urls)} URL(s)\n") # Mostrar todas las URLs encontradas for i, url in enumerate(urls, 1): if url: is_m3u8 = 'm3u8' in url print(f"URL {i} {'(m3u8)' if is_m3u8 else ''}:") print(f"{url[:100]}..." if len(url) > 100 else url) print() # Buscar la URL m3u8 m3u8_url = None for url in urls: if url and ('m3u8' in url or 'googlevideo.com' in url): m3u8_url = url break stream_url = m3u8_url if m3u8_url else (urls[0] if urls and urls[0] else None) if stream_url: print("🎯 URL seleccionada para streaming:") print(f"{stream_url}\n") print("📋 Comando FFmpeg de ejemplo:") rtmp_example = "rtmps://live-api-s.facebook.com:443/rtmp/TU-STREAM-KEY" ffmpeg_cmd = f'ffmpeg -re -i "{stream_url}" -c copy -f flv {rtmp_example}' print(f"{ffmpeg_cmd}\n") return stream_url else: print("❌ No se pudo extraer ninguna URL") return None except subprocess.TimeoutExpired: print("❌ Timeout: La operación tardó demasiado") return None except Exception as e: print(f"❌ Error: {str(e)}") return None if __name__ == "__main__": if len(sys.argv) > 1: video_url = sys.argv[1] else: # Video de prueba (debes reemplazar con un video en vivo real) print("💡 Uso: python3 test_m3u8_extraction.py ") print("Ejemplo: python3 test_m3u8_extraction.py 'https://www.youtube.com/watch?v=VIDEO_ID'\n") # Pedir al usuario video_url = input("Ingresa la URL de un video de YouTube en vivo: ").strip() if not video_url: print("❌ No se proporcionó URL") sys.exit(1) print("═" * 70) m3u8_url = test_get_m3u8_url(video_url) print("═" * 70) if m3u8_url: print("✅ Extracción exitosa!") sys.exit(0) else: print("❌ Falló la extracción") sys.exit(1)