python - 为什么使用 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
解决方案
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 )
推荐阅读
- excel - 通过更新图表生成多个PowerPoint文件
- python - 写入txt文件多个字符串,只保存最后一个字符串?
- java - 在容器中插入 JLabel
- javascript - 如何监控 Javascript / Node 中的内存使用情况?
- javascript - 在陈述新的承诺之前,我如何等待 2 个检查 URL 状态的承诺完成
- authentication - 如何在 RASA 中并行管理多个频道的唯一 user_id?
- ansible - 为什么复制似乎需要永远
- javascript - 在 sapper 中在 Polka 中创建单独的后端 API 或使用相同的 Polka server.js
- python - Python中的计划函数未启动
- php - Laravel 7 迁移:语法错误或访问冲突:1068 定义了多个主键