Fix yt-dlp errors

This commit is contained in:
2025-12-05 22:40:14 +03:00
parent 588d3f4c87
commit e635a6fb0d

View File

@@ -557,7 +557,7 @@ async def download_media(
title = f"video_from_{domain}" title = f"video_from_{domain}"
else: else:
title = 'video' title = 'video'
except: except Exception:
title = 'video' title = 'video'
# Clean title from invalid characters # 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(): def extract_info_sync():
"""Synchronous function for extracting information""" """Synchronous function for extracting information"""
with yt_dlp.YoutubeDL(ydl_opts) as ydl: import sys
return ydl.extract_info(url, download=False) 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 # Run synchronous yt-dlp in executor to avoid blocking event loop
info = await loop.run_in_executor(None, extract_info_sync) 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, 'quiet': True,
'no_warnings': True, 'no_warnings': True,
'extract_flat': 'in_playlist', # Extract flat for playlist entries, full for single videos '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 # 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(): def extract_info_sync():
"""Synchronous function for extracting information""" """Synchronous function for extracting information"""
import sys
original_stderr = sys.stderr
error_filter = None
try: try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl: # Redirect stderr to filter non-critical errors
return ydl.extract_info(url, download=False) error_filter = YtDlpErrorFilter(original_stderr)
except Exception as e: sys.stderr = error_filter
# 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: try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl: with yt_dlp.YoutubeDL(ydl_opts) as ydl:
return ydl.extract_info(url, download=False) return ydl.extract_info(url, download=False)
except Exception as e2: except Exception as e:
logger.error(f"Failed to extract info even with ignoreerrors: {e2}") # Log but don't fail completely - some metadata might still be available
raise 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 # Extract info without downloading
info = await loop.run_in_executor(None, extract_info_sync) 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 from urllib.parse import urlparse
parsed = urlparse(entry_url) parsed = urlparse(entry_url)
title = f"Video from {parsed.netloc}" if parsed.netloc else "Video" title = f"Video from {parsed.netloc}" if parsed.netloc else "Video"
except: except Exception:
title = "Video" title = "Video"
videos.append({ videos.append({