我正在使用Python来检查特定的频道是否正在直播,方法是使用"https://www.youtube.com/channel/channel_id/live"
通过使用youtube-dl的"is_live"功能,我已经成功获取了所需信息。
但是,如果该频道没有在直播状态,下载整个频道包括所有播放列表需要很长时间。
我希望在下载时间超过5秒时停止下载。有什么方法可以实现这一点吗?
我正在使用Python来检查特定的频道是否正在直播,方法是使用"https://www.youtube.com/channel/channel_id/live"
通过使用youtube-dl的"is_live"功能,我已经成功获取了所需信息。
但是,如果该频道没有在直播状态,下载整个频道包括所有播放列表需要很长时间。
我希望在下载时间超过5秒时停止下载。有什么方法可以实现这一点吗?
您可以将代码添加到此模板的下载功能中:
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秒的超时时间是在asyncio
的wait_for
函数中定义的。
ydl = youtube_dl.YoutubeDL(ydl_opts)
将{socket_timeout: 5}
添加为ydl_opts
,其中的5表示秒数。 - undefined