Fix yt-dlp title error, and inline title

This commit is contained in:
2025-12-05 22:16:33 +03:00
parent 5c280a4d3a
commit ec0fd18317
4 changed files with 188 additions and 7 deletions

View File

@@ -475,8 +475,25 @@ async def download_media(
# Search for downloaded file
title = info.get('title', 'video')
# Handle cases where title extraction failed (e.g., PornHub)
if not title or title == 'NA' or title.strip() == '':
# Try to generate title from URL or use default
try:
from urllib.parse import urlparse
parsed = urlparse(url)
if parsed.netloc:
# Use domain name as part of title
domain = parsed.netloc.replace('www.', '').split('.')[0]
title = f"video_from_{domain}"
else:
title = 'video'
except:
title = 'video'
# Clean title from invalid characters
title = "".join(c for c in title if c.isalnum() or c in (' ', '-', '_')).strip()
if not title: # If cleaning removed everything, use default
title = 'video'
ext = info.get('ext', 'mp4')
logger.info(f"Searching for downloaded file. Title: {title}, ext: {ext}, task_id: {task_id}")
@@ -670,6 +687,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)
}
# Add cookies if specified
@@ -698,8 +716,20 @@ async def get_videos_list(url: str, cookies_file: Optional[str] = None) -> Optio
def extract_info_sync():
"""Synchronous function for extracting information"""
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
return ydl.extract_info(url, download=False)
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
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
# Extract info without downloading
info = await loop.run_in_executor(None, extract_info_sync)
@@ -721,10 +751,21 @@ async def get_videos_list(url: str, cookies_file: Optional[str] = None) -> Optio
if not entry_url:
continue
# Handle missing title gracefully
title = entry.get('title')
if not title or title == 'NA':
# Try to generate a title from URL or use default
try:
from urllib.parse import urlparse
parsed = urlparse(entry_url)
title = f"Video from {parsed.netloc}" if parsed.netloc else "Video"
except:
title = "Video"
videos.append({
'id': entry.get('id'),
'url': entry_url,
'title': entry.get('title', 'Unknown'),
'title': title,
'duration': entry.get('duration'),
'thumbnail': entry.get('thumbnail'),
})