【问题标题】:Why are subsequent downloads with youtube-dl so much faster?为什么使用 youtube-dl 的后续下载速度如此之快?
【发布时间】:2020-11-16 04:30:25
【问题描述】:

我正在我的 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

【问题讨论】:

    标签: python python-3.x youtube-dl


    【解决方案1】:

    好的,这应该可以。从每个 vid 中获取 D/L 的信息,然后检索这些 vid。

    #! /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 )
    

    【讨论】:

    • 谢谢!这有助于我现在知道如何在不下载实际内容的情况下检索媒体信息。因此,出于我的需要,我将不得不获取随机视频的信息,然后下载都非常快。不完全是我想要的,但我可能可以使用它。
    【解决方案2】:

    单步执行 youtube-dl 代码后,我发现大部分时间都花在为 YT 网址找到正确的 InfoExtractor 上。下载第一个媒体项目时,框架会经过数百个可能的提取器(每个提取器都执行正则表达式),最后找到正确的 YT 提取器,在我的例子中它位于位置 1122

    这是我的快速破解,它完全从我的 RPi 零上的过程中删除了 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!
    

    也许有一种更常规的方法,无需覆盖半私有变量。

    【讨论】:

      【解决方案3】:

      如果没有更快的互联网服务,我认为您无法加快速度。那是网络时间,与 YouTube 协商安全套接字。

      可能,您可以通过测试当前 DNS 的速度并查看是否有更快的可用来加快它一些

      https://www.serverwatch.com/guides/two-tools-for-testing-dns-server-speeds

      【讨论】:

      • 感谢您的回复。如果在我尝试下载媒体之前发生,我不介意套接字协商的时间。有没有办法让 yt-dl 事先做到这一点?编辑:像ydl.init_socket(...) 这样的东西会很有帮助......
      • 我刚刚发现网络的东西不是这里的瓶颈,看我的回答。无论如何感谢您的意见!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      • 2020-10-01
      • 2015-07-11
      相关资源
      最近更新 更多