perf(download): extract the video once per attempt, not twice

ydl.download([url]) re-ran the whole extraction (player clients, API JSON,
m3u8) that extract_info had just done: 4-9 s of round trips per attempt.
The unprocessed info is now handed to process_ie_result. Same file (MD5)
on a real 1080p video; falls back to ydl.download for playlists and
anything without formats.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
victormanuel.caverogracia 2026-09-25 20:39:21 +02:00
parent 6e660d8f92
commit 991cdef3ba

10
main.py
View file

@ -931,8 +931,11 @@ def download_youtube_video(url, output_dir="."):
def _attempt(extractor_args, fmt, proxy, cookies=True): def _attempt(extractor_args, fmt, proxy, cookies=True):
_dl_bytes["total"] = 0 _dl_bytes["total"] = 0
_dl_bytes["partial"] = 0 _dl_bytes["partial"] = 0
# Extracted ONCE, unprocessed, and handed to the download below: a
# plain ydl.download([url]) ran the whole extraction again (player
# clients, API JSON, m3u8), 4-9 s of round trips per attempt.
with yt_dlp.YoutubeDL(_base_opts(extractor_args, proxy, cookies)) as ydl: with yt_dlp.YoutubeDL(_base_opts(extractor_args, proxy, cookies)) as ydl:
info = ydl.extract_info(url, download=False) info = ydl.extract_info(url, download=False, process=False)
sanitized = sanitize_filename(info.get('title', 'youtube_video')) sanitized = sanitize_filename(info.get('title', 'youtube_video'))
expected = os.path.join(output_dir, f'{sanitized}.mp4') expected = os.path.join(output_dir, f'{sanitized}.mp4')
if os.path.exists(expected): if os.path.exists(expected):
@ -945,7 +948,10 @@ def download_youtube_video(url, output_dir="."):
'progress_hooks': [_progress_hook], 'progress_hooks': [_progress_hook],
} }
with yt_dlp.YoutubeDL(dl_opts) as ydl: with yt_dlp.YoutubeDL(dl_opts) as ydl:
ydl.download([url]) if info.get('_type', 'video') == 'video' and info.get('formats'):
ydl.process_ie_result(info, download=True)
else:
ydl.download([url])
return sanitized return sanitized
# DIRECT_FIRST=1: try the server's own IP before spending proxy bandwidth. # DIRECT_FIRST=1: try the server's own IP before spending proxy bandwidth.