首页 > 解决方案 > 为什么使用 youtube-dl 的后续下载速度如此之快?

问题描述

我正在我的 RPi Zero 上下载多个 YouTube 视频并将其转换为纯音频文件。虽然初始化和第一次下载需要相当长的时间,但随后的下载速度要快得多。有什么方法可以“预热” yt-dl,即使是第一次下载也更快?我不介意任何额外的初始化时间。(更改 URL 的顺序无效。)

import time
t1 = time.time()

from youtube_dl import YoutubeDL
ydl = YoutubeDL({'format': 'bestaudio/best'}) 
t2 = time.time()
print(t2 - t1, flush=True)

ydl.download(['https://www.youtube.com/watch?v=xxxxxxxxxxx'])
t3 = time.time()
print(t3 - t2, flush=True)

ydl.download(['https://www.youtube.com/watch?v=yyyyyyyyyyy'])
t4 = time.time()
print(t4 - t3, flush=True)

ydl.download(['https://www.youtube.com/watch?v=zzzzzzzzzzz',])
t5 = time.time()
print(t5 - t4, flush=True)

输出:

5.889932870864868
[youtube] xxxxxxxxxxx: Downloading webpage
[download] 100% of 4.09MiB in 00:01
15.685529470443726
[youtube] yyyyyyyyyyy: Downloading webpage
[download] 100% of 3.58MiB in 00:00
2.526634693145752
[youtube] zzzzzzzzzzz: Downloading webpage
[download] 100% of 3.88MiB in 00:01
2.4716105461120605

标签: pythonpython-3.xyoutube-dl

解决方案


Alright, this should do. D/L's info from each vid, then retrieves those vids.

#! /usr/bin/env python3

import time
from youtube_dl import YoutubeDL

##  opts = { 'format': 'best[height<=720,ext=mp4]/best[height<=720]' }
opts = { 'format': 'bestaudio[ext=m4a]/bestaudio/best' }

ydl = YoutubeDL( opts )

videos = [ 'https://www.youtube.com/watch?v=cVsQLlk-T0s',
           'https://www.youtube.com/watch?v=3l2oi-X8P38',
           'https://www.youtube.com/watch?v=bPpcfH_HHH8' ]

items = []

for video in videos:
    timer = time .time()
    info = ydl .extract_info( video,  download = False )
    items .append( info )
    print( 'info:',  info['title'],  '--',  time .time() -timer,  flush = True )

for item in items:
    timer = time .time()
    ydl .process_video_result( item )
    print( 'vid:',  item['title'],  '--',  time .time() -timer,  flush = True )

推荐阅读