добавили куки для ютуба
This commit is contained in:
parent
c8feb4b78b
commit
1a54f10ea2
4 changed files with 146 additions and 3 deletions
|
|
@ -33,13 +33,20 @@ def _safe_filename(title: str) -> str:
|
|||
|
||||
|
||||
def download_youtube_video(url: str, max_retries: int = 3) -> Path:
|
||||
"""Скачивает видео с YouTube"""
|
||||
"""Скачивает видео с YouTube - используем cookies для обхода блокировок"""
|
||||
cookies_file = os.getenv('YOUTUBE_COOKIES_FILE', 'youtube_cookies.txt')
|
||||
cookies_file_path = Path(cookies_file)
|
||||
|
||||
if not cookies_file_path.exists():
|
||||
logger.info(f"YouTube: файл cookies не найден ({cookies_file_path}). Работаем без cookies. "
|
||||
f"Для лучшей работы рекомендуется добавить cookies через скрипт get_youtube_cookies_local.sh")
|
||||
|
||||
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
||||
|
||||
last_error = None
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
# Получаем информацию о видео
|
||||
# Базовые настройки для получения информации
|
||||
ydl_opts_info = {
|
||||
'quiet': False,
|
||||
'no_warnings': False,
|
||||
|
|
@ -60,12 +67,17 @@ def download_youtube_video(url: str, max_retries: int = 3) -> Path:
|
|||
},
|
||||
}
|
||||
|
||||
# Если есть файл с cookies, используем его
|
||||
if cookies_file_path.exists():
|
||||
ydl_opts_info['cookiefile'] = str(cookies_file_path.absolute())
|
||||
logger.info(f"YouTube: используем cookies из {cookies_file_path}")
|
||||
|
||||
with yt_dlp.YoutubeDL(ydl_opts_info) as ydl:
|
||||
info = ydl.extract_info(url, download=False)
|
||||
video_title = info.get('title', 'video')
|
||||
logger.info(f"YouTube: получена информация о видео: {video_title}")
|
||||
|
||||
# Скачиваем видео
|
||||
# Настройки для скачивания
|
||||
ydl_opts_download = {
|
||||
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
||||
'outtmpl': _safe_filename(video_title),
|
||||
|
|
@ -88,6 +100,10 @@ def download_youtube_video(url: str, max_retries: int = 3) -> Path:
|
|||
},
|
||||
}
|
||||
|
||||
# Если есть файл с cookies, используем его для скачивания
|
||||
if cookies_file_path.exists():
|
||||
ydl_opts_download['cookiefile'] = str(cookies_file_path.absolute())
|
||||
|
||||
logger.info(f"YouTube: начинаем скачивание (попытка {attempt + 1}/{max_retries})")
|
||||
with yt_dlp.YoutubeDL(ydl_opts_download) as ydl:
|
||||
ydl.download([url])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue