try fix youtube download

This commit is contained in:
juancarlos.cavero 2025-12-22 22:18:15 +01:00
parent 6feaf4b03d
commit c5e2d4818f

18
main.py
View file

@ -453,6 +453,7 @@ def download_youtube_video(url, output_dir="."):
Downloads a YouTube video using yt-dlp. Downloads a YouTube video using yt-dlp.
Returns the path to the downloaded video and the video title. Returns the path to the downloaded video and the video title.
""" """
print(f"🔍 Debug: yt-dlp version: {yt_dlp.version.__version__}")
print("📥 Downloading video from YouTube...") print("📥 Downloading video from YouTube...")
step_start_time = time.time() step_start_time = time.time()
@ -463,6 +464,11 @@ def download_youtube_video(url, output_dir="."):
try: try:
with open(cookies_path, 'w') as f: with open(cookies_path, 'w') as f:
f.write(cookies_env) f.write(cookies_env)
if os.path.exists(cookies_path):
print(f" Debug: Cookies file created. Size: {os.path.getsize(cookies_path)} bytes")
with open(cookies_path, 'r') as f:
content = f.read(100)
print(f" Debug: First 100 chars of cookie file: {content}")
except Exception as e: except Exception as e:
print(f"⚠️ Failed to write cookies file: {e}") print(f"⚠️ Failed to write cookies file: {e}")
cookies_path = None cookies_path = None
@ -471,15 +477,20 @@ def download_youtube_video(url, output_dir="."):
print("⚠️ YOUTUBE_COOKIES env var not found.") print("⚠️ YOUTUBE_COOKIES env var not found.")
ydl_opts_info = { ydl_opts_info = {
'quiet': True, 'quiet': False,
'no_warnings': True, 'verbose': True,
'no_warnings': False,
'cookiefile': cookies_path if cookies_path else None 'cookiefile': cookies_path if cookies_path else None
} }
with yt_dlp.YoutubeDL(ydl_opts_info) as ydl: with yt_dlp.YoutubeDL(ydl_opts_info) as ydl:
try:
info = ydl.extract_info(url, download=False) info = ydl.extract_info(url, download=False)
video_title = info.get('title', 'youtube_video') video_title = info.get('title', 'youtube_video')
sanitized_title = sanitize_filename(video_title) sanitized_title = sanitize_filename(video_title)
except Exception as e:
print(f"❌ Error extracting info: {e}")
raise e
output_template = os.path.join(output_dir, f'{sanitized_title}.%(ext)s') output_template = os.path.join(output_dir, f'{sanitized_title}.%(ext)s')
expected_file = os.path.join(output_dir, f'{sanitized_title}.mp4') expected_file = os.path.join(output_dir, f'{sanitized_title}.mp4')
@ -492,7 +503,8 @@ def download_youtube_video(url, output_dir="."):
'outtmpl': output_template, 'outtmpl': output_template,
'merge_output_format': 'mp4', 'merge_output_format': 'mp4',
'quiet': False, 'quiet': False,
'no_warnings': True, 'verbose': True,
'no_warnings': False,
'overwrites': True, 'overwrites': True,
'cookiefile': cookies_path if cookies_path else None 'cookiefile': cookies_path if cookies_path else None
} }