diff --git a/bot/modules/media_loader/ytdlp.py b/bot/modules/media_loader/ytdlp.py index 453f969..ec7f291 100644 --- a/bot/modules/media_loader/ytdlp.py +++ b/bot/modules/media_loader/ytdlp.py @@ -557,7 +557,7 @@ async def download_media( title = f"video_from_{domain}" else: title = 'video' - except: + except Exception: title = 'video' # Clean title from invalid characters @@ -717,8 +717,21 @@ async def get_media_info(url: str, cookies_file: Optional[str] = None) -> Option def extract_info_sync(): """Synchronous function for extracting information""" - with yt_dlp.YoutubeDL(ydl_opts) as ydl: - return ydl.extract_info(url, download=False) + import sys + original_stderr = sys.stderr + error_filter = None + + try: + # Redirect stderr to filter non-critical errors + error_filter = YtDlpErrorFilter(original_stderr) + sys.stderr = error_filter + + with yt_dlp.YoutubeDL(ydl_opts) as ydl: + return ydl.extract_info(url, download=False) + finally: + # Restore original stderr + if error_filter: + sys.stderr = original_stderr # Run synchronous yt-dlp in executor to avoid blocking event loop info = await loop.run_in_executor(None, extract_info_sync) @@ -757,7 +770,7 @@ async def get_videos_list(url: str, cookies_file: Optional[str] = None) -> Optio 'quiet': True, 'no_warnings': True, 'extract_flat': 'in_playlist', # Extract flat for playlist entries, full for single videos - 'ignoreerrors': True, # Continue on extraction errors (e.g., missing title) + # Note: ignoreerrors is NOT set here - it will be enabled in exception handler if needed } # Add cookies if specified @@ -786,20 +799,33 @@ async def get_videos_list(url: str, cookies_file: Optional[str] = None) -> Optio def extract_info_sync(): """Synchronous function for extracting information""" + import sys + original_stderr = sys.stderr + error_filter = None + try: - with yt_dlp.YoutubeDL(ydl_opts) as ydl: - return ydl.extract_info(url, download=False) - except Exception as e: - # Log but don't fail completely - some metadata might still be available - logger.warning(f"Error extracting info (some metadata may be missing): {e}") - # Try to extract with ignoreerrors to get partial info - ydl_opts['ignoreerrors'] = True + # Redirect stderr to filter non-critical errors + error_filter = YtDlpErrorFilter(original_stderr) + sys.stderr = error_filter + try: with yt_dlp.YoutubeDL(ydl_opts) as ydl: return ydl.extract_info(url, download=False) - except Exception as e2: - logger.error(f"Failed to extract info even with ignoreerrors: {e2}") - raise + except Exception as e: + # Log but don't fail completely - some metadata might still be available + logger.warning(f"Error extracting info (some metadata may be missing): {e}") + # Try to extract with ignoreerrors to get partial info + ydl_opts['ignoreerrors'] = True + try: + with yt_dlp.YoutubeDL(ydl_opts) as ydl: + return ydl.extract_info(url, download=False) + except Exception as e2: + logger.error(f"Failed to extract info even with ignoreerrors: {e2}") + raise + finally: + # Restore original stderr + if error_filter: + sys.stderr = original_stderr # Extract info without downloading info = await loop.run_in_executor(None, extract_info_sync) @@ -829,7 +855,7 @@ async def get_videos_list(url: str, cookies_file: Optional[str] = None) -> Optio from urllib.parse import urlparse parsed = urlparse(entry_url) title = f"Video from {parsed.netloc}" if parsed.netloc else "Video" - except: + except Exception: title = "Video" videos.append({ @@ -875,4 +901,4 @@ async def get_videos_list(url: str, cookies_file: Optional[str] = None) -> Optio except Exception as e: logger.error(f"Error getting videos list: {e}", exc_info=True) - return None + return None \ No newline at end of file