我正在将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
在浏览完youtube-dl代码后,我发现大部分时间都花在了寻找正确InfoExtractor
的YT网址上。当下载第一个媒体项目时,框架会经过数百个可能的提取器(每个提取器都执行正则表达式),最后定位到正确的YT提取器,在我的情况下,该提取器位于位置1122!
这是我的快速技巧,可从RPi Zero上的流程中完全删除12秒:
import time
timer = time.time()
from youtube_dl import YoutubeDL
ydl = YoutubeDL({'format': 'bestaudio/best'})
# Get correct info extractor and replace the long existing list
ydl._ies = [ydl.get_info_extractor('Youtube')]
print(time.time() - timer)
timer = time.time()
# Super fast first download, yay!
ydl.download(['https://www.youtube.com/watch?v=xxxxxxxxxxx'])
print(time.time() - timer)
输出:
5.961918592453003
[youtube] xxxxxxxxxxx: Downloading webpage
[download] 100% of 4.09MiB in 00:01
3.7426917552948 <-- way faster!
也许有一种更常规的方法可以不覆盖半私有变量。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句