diff --git a/app.py b/app.py index 0d93b3d..58e6d43 100644 --- a/app.py +++ b/app.py @@ -110,9 +110,46 @@ def download_video(url, download_id, format_type='mp4'): # return try: + # Primero obtener información del video para el título + info_opts = { + 'quiet': True, + 'no_warnings': True, + 'extract_flat': False, + 'restrictfilenames': True, + 'no_check_certificate': True, + 'socket_timeout': 60, + 'retries': 5, + 'ignoreerrors': True, + 'http_headers': { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36', + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', + 'Accept-Language': 'en-US,en;q=0.5', + 'Accept-Encoding': 'gzip, deflate', + 'DNT': '1', + 'Connection': 'keep-alive', + 'Upgrade-Insecure-Requests': '1' + } + } + + # Obtener información del video para usar el título + video_title = None + try: + with yt_dlp.YoutubeDL(info_opts) as ydl: + info = ydl.extract_info(url, download=False) + video_title = info.get('title', f'video_{download_id}') + # Limpiar el título para el sistema de archivos + import re + video_title = re.sub(r'[<>:"/\\|?*]', '', video_title) + video_title = video_title.strip() + if not video_title: + video_title = f'video_{download_id}' + except Exception as e: + print(f"Error obteniendo título del video: {e}") + video_title = f'video_{download_id}' + # Estrategia 1: Configuración simple y robusta ydl_opts = { - 'outtmpl': os.path.join(app.config['DOWNLOAD_FOLDER'], f'{download_id}.%(ext)s'), + 'outtmpl': os.path.join(app.config['DOWNLOAD_FOLDER'], f'{video_title}.%(ext)s'), 'progress_hooks': [lambda d: hook(d, download_id)], 'quiet': True, 'no_warnings': True,