如何设置youtube-dl下载时间限制

3

我正在使用Python来检查特定的频道是否正在直播,方法是使用"https://www.youtube.com/channel/channel_id/live"

通过使用youtube-dl的"is_live"功能,我已经成功获取了所需信息。

但是,如果该频道没有在直播状态,下载整个频道包括所有播放列表需要很长时间。

我希望在下载时间超过5秒时停止下载。有什么方法可以实现这一点吗?


这个有帮助吗?:https://www.mankier.com/1/youtube-dl#--socket-timeout - undefined
你可以在实例中通过 ydl = youtube_dl.YoutubeDL(ydl_opts){socket_timeout: 5} 添加为 ydl_opts,其中的5表示秒数。 - undefined
@mama 我尝试了套接字超时,但它不起作用。我猜它只在调用页面时起作用。 - undefined
我个人会选择线程终止而不是子进程,但我们的知识库中有两种方法的答案。 - undefined
1个回答

0

您可以将代码添加到此模板的下载功能中:

import asyncio

async def download(url):
    await asyncio.sleep(3600)
    print(url)

async def youtbe(url):
    try:
        await asyncio.wait_for(download(url), timeout=5.0)
    except asyncio.TimeoutError:
        print(f'timeout on {url}')



async def main(urls):
    await asyncio.gather(*[youtbe(url) for url in urls])

urls = [
        'hej00',
        'hej01',
        'hej02',
        'hej03',
        'hej04',
        'hej05',
        'hej06',
        'hej07',
        'hej08',
        'hej09',
        'hej10',
        ]
asyncio.run(main(urls))

5秒的超时时间是在asynciowait_for函数中定义的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接