This commit is contained in:
vrubel 2026-01-28 18:54:03 +03:00
parent 5dac739e17
commit 990444601d

View file

@ -112,44 +112,57 @@ async def get_video_title(url: str, config: Optional[Config] = None) -> Optional
Название видео или None в случае ошибки Название видео или None в случае ошибки
""" """
try: try:
cmd = [ async def _run_get_title(use_player_client: bool) -> tuple[int, str, str]:
'yt-dlp', cmd = [
'--no-download', 'yt-dlp',
'--skip-download', '--no-download',
'--get-title', '--skip-download',
'--no-warnings', '--get-title',
'--no-playlist', '--no-warnings',
url '--no-playlist',
] url
if config: ]
if config.ytdlp_user_agent: if config:
cmd.extend(['--user-agent', config.ytdlp_user_agent]) if config.ytdlp_user_agent:
if config.ytdlp_cookies_file: cmd.extend(['--user-agent', config.ytdlp_user_agent])
cmd.extend(['--cookies', config.ytdlp_cookies_file]) if config.ytdlp_cookies_file:
if config.ytdlp_player_client: cmd.extend(['--cookies', config.ytdlp_cookies_file])
cmd.extend(['--extractor-args', f'youtube:player_client={config.ytdlp_player_client}']) if use_player_client and config.ytdlp_player_client:
if config.ytdlp_force_ipv4: cmd.extend(['--extractor-args', f'youtube:player_client={config.ytdlp_player_client}'])
cmd.append('--force-ipv4') if config.ytdlp_force_ipv4:
cmd.append('--force-ipv4')
process = await asyncio.create_subprocess_exec(
*cmd, process = await asyncio.create_subprocess_exec(
stdout=asyncio.subprocess.PIPE, *cmd,
stderr=asyncio.subprocess.PIPE stdout=asyncio.subprocess.PIPE,
) stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate() stdout, stderr = await process.communicate()
return process.returncode, stdout.decode('utf-8', errors='ignore'), stderr.decode('utf-8', errors='ignore')
if process.returncode == 0:
title = stdout.decode('utf-8', errors='ignore').strip() # Первый проход — как обычно (с player_client, если задан)
returncode, stdout, stderr = await _run_get_title(use_player_client=True)
if returncode == 0:
title = stdout.strip()
if title: if title:
logger.info(f"Got title for {url}: {title[:50]}...") logger.info(f"Got title for {url}: {title[:50]}...")
return title return title
else: else:
error = stderr.decode('utf-8', errors='ignore') logger.warning(f"Failed to get title for {url}: {stderr}")
logger.warning(f"Failed to get title for {url}: {error}")
# Фолбэк — без player_client
if config and config.ytdlp_player_client:
returncode, stdout, stderr = await _run_get_title(use_player_client=False)
if returncode == 0:
title = stdout.strip()
if title:
logger.info(f"Got title for {url} without player_client: {title[:50]}...")
return title
else:
logger.warning(f"Failed to get title for {url} without player_client: {stderr}")
return None return None
except Exception as e: except Exception as e:
logger.error(f"Error getting video title: {e}", exc_info=True) logger.error(f"Error getting video title: {e}", exc_info=True)
return None return None
@ -192,7 +205,7 @@ async def download_and_convert(
'bestaudio/best', 'bestaudio/best',
] ]
async def _run_yt_dlp(format_selector: str): async def _run_yt_dlp(format_selector: str, use_player_client: bool):
cmd = [ cmd = [
'yt-dlp', 'yt-dlp',
'-x', # Извлечь аудио '-x', # Извлечь аудио
@ -212,7 +225,7 @@ async def download_and_convert(
cmd.extend(['--user-agent', config.ytdlp_user_agent]) cmd.extend(['--user-agent', config.ytdlp_user_agent])
if config.ytdlp_cookies_file: if config.ytdlp_cookies_file:
cmd.extend(['--cookies', config.ytdlp_cookies_file]) cmd.extend(['--cookies', config.ytdlp_cookies_file])
if config.ytdlp_player_client: if use_player_client and config.ytdlp_player_client:
cmd.extend(['--extractor-args', f'youtube:player_client={config.ytdlp_player_client}']) cmd.extend(['--extractor-args', f'youtube:player_client={config.ytdlp_player_client}'])
if config.ytdlp_force_ipv4: if config.ytdlp_force_ipv4:
cmd.append('--force-ipv4') cmd.append('--force-ipv4')
@ -250,8 +263,15 @@ async def download_and_convert(
return process.returncode, list(stderr_tail) + list(stdout_tail) return process.returncode, list(stderr_tail) + list(stdout_tail)
last_tail: list[str] = [] last_tail: list[str] = []
attempts = []
for fmt in formats_to_try: for fmt in formats_to_try:
returncode, tail_lines = await _run_yt_dlp(fmt) attempts.append((fmt, True))
if config and config.ytdlp_player_client:
for fmt in formats_to_try:
attempts.append((fmt, False))
for fmt, use_player_client in attempts:
returncode, tail_lines = await _run_yt_dlp(fmt, use_player_client)
last_tail = tail_lines last_tail = tail_lines
if returncode == 0: if returncode == 0:
break break