fix: prefer Russian audio track, add FFmpegFixupStretched, remove broken h264_metadata bsf

This commit is contained in:
vrubelroman 2026-05-03 03:22:26 +03:00
parent 59b1c54668
commit c1d8a8235d
2 changed files with 26 additions and 11 deletions

View file

@ -150,6 +150,8 @@ def _make_base_ydl_opts(user_agent: str, cookies_file_path: Path | None = None)
'user_agent': user_agent,
'socket_timeout': 60,
'extractor_retries': 3,
# Предпочитаем русскую озвучку — YouTube иногда подсовывает авто-дубляж на английском
'format_sort': ['lang:ru'],
'http_headers': {
'User-Agent': user_agent,
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
@ -285,9 +287,9 @@ def download_youtube_video(url: str, max_retries: int = 3, format_id: str | None
# Это важно, т.к. format_id из get_youtube_formats() может не совпадать
# с format_id при повторном extract_info() в download_youtube_video().
default_format_options = [
'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
'bestvideo[ext=mp4]+bestaudio[language=ru][ext=m4a]/bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
'best[ext=mp4]/best',
'bestvideo+bestaudio/best',
'bestvideo+bestaudio[language=ru]/bestvideo+bestaudio/best',
'best',
]
@ -335,6 +337,8 @@ def download_youtube_video(url: str, max_retries: int = 3, format_id: str | None
'format': format_option,
'outtmpl': _safe_filename(video_title),
'fragment_retries': 3,
# Явно включаем фикс растянутого/сплющенного aspect ratio
'postprocessors': [{'key': 'FFmpegFixupStretched'}],
})
use_cookies_this_attempt = cookies_valid
@ -650,17 +654,28 @@ def get_youtube_formats(url: str) -> list[dict]:
logger.info(f"[FORMATS] {display_label} (height={best_video_height}): video_size={video_size}, has_audio={has_audio}, total={total_size}, format_id={video_format_id}")
# format_selector без /best в конце — чтобы yt-dlp не молча скатывался на другой размер
# format_selector: предпочитаем русскую озвучку (YouTube Shorts часто авто-дублирует)
if has_audio:
format_selector = f"{video_format_id}/bestvideo[height<={best_video_height}]+bestaudio/best[height<={best_video_height}]"
format_selector = (
f"{video_format_id}/"
f"bestvideo[height<={best_video_height}]+bestaudio[language=ru]/"
f"bestvideo[height<={best_video_height}]+bestaudio/"
f"best[height<={best_video_height}]"
)
elif best_audio_info['format_id']:
format_selector = (
f"{video_format_id}+{best_audio_info['format_id']}/"
f"bestvideo[height<={best_video_height}]+bestaudio[language=ru]/"
f"bestvideo[height<={best_video_height}]+bestaudio/"
f"best[height<={best_video_height}]"
)
else:
format_selector = f"{video_format_id}+bestaudio/bestvideo[height<={best_video_height}]+bestaudio/best[height<={best_video_height}]"
format_selector = (
f"{video_format_id}+bestaudio[language=ru]/"
f"bestvideo[height<={best_video_height}]+bestaudio[language=ru]/"
f"bestvideo[height<={best_video_height}]+bestaudio/"
f"best[height<={best_video_height}]"
)
result.append({
'format_id': format_selector,
@ -673,7 +688,7 @@ def get_youtube_formats(url: str) -> list[dict]:
# Добавляем аудиодорожку (M4A в приоритете — Telegram поддерживает только MP3/M4A для reply_audio)
if best_audio_info['size']:
result.append({
'format_id': 'bestaudio[ext=m4a]/bestaudio[ext=mp3]/bestaudio/best',
'format_id': 'bestaudio[language=ru][ext=m4a]/bestaudio[language=ru][ext=mp3]/bestaudio[language=ru]/bestaudio/best',
'label': f"Audio only ({best_audio_info['ext']})",
'quality': 'audio',
'ext': best_audio_info['ext'],