This commit is contained in:
vrubel 2026-01-28 19:17:14 +03:00
parent 4fb33befcf
commit da0922be32

View file

@ -209,12 +209,12 @@ async def download_and_convert(
'bestaudio/best', 'bestaudio/best',
] ]
async def _run_yt_dlp(format_selector: str, use_player_client: bool): async def _run_yt_dlp(format_selector: str, use_player_client: bool, hls_prefer_native: bool):
cmd = [ cmd = [
'yt-dlp', 'yt-dlp',
'-x', # Извлечь аудио '-x', # Извлечь аудио
'-f', format_selector, '-f', format_selector,
'--hls-prefer-ffmpeg', '--hls-prefer-native' if hls_prefer_native else '--hls-prefer-ffmpeg',
'--audio-format', 'mp3', '--audio-format', 'mp3',
'--audio-quality', '0', # Лучшее качество '--audio-quality', '0', # Лучшее качество
'-o', str(temp_template), '-o', str(temp_template),
@ -269,13 +269,13 @@ async def download_and_convert(
last_tail: list[str] = [] last_tail: list[str] = []
attempts = [] attempts = []
for fmt in formats_to_try: for fmt in formats_to_try:
attempts.append((fmt, True)) attempts.append((fmt, True, False)) # prefer ffmpeg for HLS by default
if config and config.ytdlp_player_client: if config and config.ytdlp_player_client:
for fmt in formats_to_try: for fmt in formats_to_try:
attempts.append((fmt, False)) attempts.append((fmt, False, False))
for fmt, use_player_client in attempts: for fmt, use_player_client, hls_prefer_native in attempts:
returncode, tail_lines = await _run_yt_dlp(fmt, use_player_client) returncode, tail_lines = await _run_yt_dlp(fmt, use_player_client, hls_prefer_native)
last_tail = tail_lines last_tail = tail_lines
if returncode == 0: if returncode == 0:
break break
@ -283,6 +283,13 @@ async def download_and_convert(
if "Requested format is not available" in tail_text: if "Requested format is not available" in tail_text:
logger.warning("yt-dlp format unavailable, retrying with fallback") logger.warning("yt-dlp format unavailable, retrying with fallback")
continue continue
if "Error when loading first segment" in tail_text or "m3u8" in tail_text or "ffmpeg exited with code 183" in tail_text:
logger.warning("yt-dlp HLS failed, retrying with native HLS downloader")
returncode, tail_lines = await _run_yt_dlp(fmt, use_player_client, True)
last_tail = tail_lines
if returncode == 0:
break
tail_text = "\n".join(tail_lines[-12:]) if tail_lines else ""
logger.error("yt-dlp failed") logger.error("yt-dlp failed")
if tail_text: if tail_text:
raise Exception(f"Ошибка скачивания: yt-dlp завершился с ошибкой\n{tail_text}") raise Exception(f"Ошибка скачивания: yt-dlp завершился с ошибкой\n{tail_text}")