为什么使用 youtube-dl 的后续下载速度如此之快?
Why are subsequent downloads with youtube-dl so much faster?
我正在我的 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 协商安全套接字。
可能,您可以通过测试当前 DNS 的速度并查看是否有更快的速度来加快速度一些。
https://www.serverwatch.com/guides/two-tools-for-testing-dns-server-speeds
好的,应该可以了。 D/L 来自每个视频的信息,然后检索这些视频。
#! /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 )
在逐步执行 youtube-dl 代码后,我发现大部分时间都花在了为 YT URL 找到正确的 InfoExtractor
上。当下载第一个媒体项目时,框架会经过数百个可能的提取器(对每个提取器执行正则表达式)并最终确定在正确的 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!
也许有一种更常规的方法,无需覆盖半私有变量。
我正在我的 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 协商安全套接字。
可能,您可以通过测试当前 DNS 的速度并查看是否有更快的速度来加快速度一些。
https://www.serverwatch.com/guides/two-tools-for-testing-dns-server-speeds
好的,应该可以了。 D/L 来自每个视频的信息,然后检索这些视频。
#! /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 )
在逐步执行 youtube-dl 代码后,我发现大部分时间都花在了为 YT URL 找到正确的 InfoExtractor
上。当下载第一个媒体项目时,框架会经过数百个可能的提取器(对每个提取器执行正则表达式)并最终确定在正确的 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!
也许有一种更常规的方法,无需覆盖半私有变量。