фикс2

This commit is contained in:
vrubel 2026-01-28 18:20:25 +03:00
parent 09347b45ec
commit 5dac739e17

View file

@ -187,66 +187,87 @@ async def download_and_convert(
temp_template = output_path.parent / f"temp_{output_path.name}.%(ext)s"
try:
cmd = [
'yt-dlp',
'-x', # Извлечь аудио
'-f', 'bestaudio[ext=m4a]/bestaudio[ext=webm]/bestaudio/best',
'--hls-prefer-ffmpeg',
'--audio-format', 'mp3',
'--audio-quality', '0', # Лучшее качество
'-o', str(temp_template),
'--no-warnings',
'--progress',
'--newline',
'--no-playlist',
url
formats_to_try = [
'bestaudio[ext=m4a]/bestaudio[ext=webm]/bestaudio/best',
'bestaudio/best',
]
if config:
if config.ytdlp_user_agent:
cmd.extend(['--user-agent', config.ytdlp_user_agent])
if config.ytdlp_cookies_file:
cmd.extend(['--cookies', config.ytdlp_cookies_file])
if config.ytdlp_player_client:
cmd.extend(['--extractor-args', f'youtube:player_client={config.ytdlp_player_client}'])
if config.ytdlp_force_ipv4:
cmd.append('--force-ipv4')
logger.info(f"Downloading {url}")
async def _run_yt_dlp(format_selector: str):
cmd = [
'yt-dlp',
'-x', # Извлечь аудио
'-f', format_selector,
'--hls-prefer-ffmpeg',
'--audio-format', 'mp3',
'--audio-quality', '0', # Лучшее качество
'-o', str(temp_template),
'--no-warnings',
'--progress',
'--newline',
'--no-playlist',
url
]
if config:
if config.ytdlp_user_agent:
cmd.extend(['--user-agent', config.ytdlp_user_agent])
if config.ytdlp_cookies_file:
cmd.extend(['--cookies', config.ytdlp_cookies_file])
if config.ytdlp_player_client:
cmd.extend(['--extractor-args', f'youtube:player_client={config.ytdlp_player_client}'])
if config.ytdlp_force_ipv4:
cmd.append('--force-ipv4')
process = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
logger.info(f"Downloading {url} with format: {format_selector}")
stderr_tail = deque(maxlen=12)
stdout_tail = deque(maxlen=12)
process = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
async def _log_stream(stream, level: str, tail: deque[str]):
while True:
line = await stream.readline()
if not line:
break
text = line.decode('utf-8', errors='ignore').strip()
if text:
tail.append(text)
if level == "info":
logger.info(f"yt-dlp: {text}")
else:
logger.warning(f"yt-dlp: {text}")
stderr_tail = deque(maxlen=12)
stdout_tail = deque(maxlen=12)
stderr_task = asyncio.create_task(_log_stream(process.stderr, "warn", stderr_tail))
stdout_task = asyncio.create_task(_log_stream(process.stdout, "info", stdout_tail))
async def _log_stream(stream, level: str, tail: deque[str]):
while True:
line = await stream.readline()
if not line:
break
text = line.decode('utf-8', errors='ignore').strip()
if text:
tail.append(text)
if level == "info":
logger.info(f"yt-dlp: {text}")
else:
logger.warning(f"yt-dlp: {text}")
await process.wait()
await stderr_task
await stdout_task
stderr_task = asyncio.create_task(_log_stream(process.stderr, "warn", stderr_tail))
stdout_task = asyncio.create_task(_log_stream(process.stdout, "info", stdout_tail))
if process.returncode != 0:
await process.wait()
await stderr_task
await stdout_task
return process.returncode, list(stderr_tail) + list(stdout_tail)
last_tail: list[str] = []
for fmt in formats_to_try:
returncode, tail_lines = await _run_yt_dlp(fmt)
last_tail = tail_lines
if returncode == 0:
break
tail_text = "\n".join(tail_lines[-12:]) if tail_lines else ""
if "Requested format is not available" in tail_text:
logger.warning("yt-dlp format unavailable, retrying with fallback")
continue
logger.error("yt-dlp failed")
tail_lines = list(stderr_tail) + list(stdout_tail)
if tail_lines:
tail_text = "\n".join(tail_lines[-12:])
if tail_text:
raise Exception(f"Ошибка скачивания: yt-dlp завершился с ошибкой\n{tail_text}")
raise Exception("Ошибка скачивания: yt-dlp завершился с ошибкой")
if returncode != 0:
logger.error("yt-dlp failed")
if last_tail:
tail_text = "\n".join(last_tail[-12:])
raise Exception(f"Ошибка скачивания: yt-dlp завершился с ошибкой\n{tail_text}")
raise Exception("Ошибка скачивания: yt-dlp завершился с ошибкой")