从 youtube_dl 中的播放列表中提取信息需要很长时间
Extracting info from a playlist in youtube_dl takes extremely long
我的 python 机器人可以在语音频道中播放音乐。我使用了 here 中的 YTDLSource class 并编辑了 class 以支持播放列表。这是我的 YTDLSource 代码:
youtube_dl.utils.bug_reports_message = lambda: ''
ytdl_format_options = {
'format': 'bestaudio/best',
'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
'restrictfilenames': True,
'noplaylist': False,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'default_search': 'auto',
'source_address': '0.0.0.0'
}
ffmpeg_options = {
'options': '-vn',
"before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5"
}
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, data, *, volume=0.5):
super().__init__(discord.FFmpegPCMAudio(data['url'], **ffmpeg_options), volume)
self.data = data
self.title = data.get('title')
self.url = data.get('webpage_url')
@classmethod
async def get(cls, url, *, volume=0.5):
loop = asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=False))
if 'entries' in data:
return [cls(entry, volume=volume) for entry in data['entries']]
else:
return cls(data, volume=volume)
代码运行正常,但速度太慢。我检查了 youtube_dl
从播放列表中提取信息需要多长时间:
time1 = time.time()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=False))
print(f"Extracted info in {time.time() - time1} seconds")
我得到了 Extracted info in 12.71654725074768 seconds
。我已经使用来自 SoundCloud 的包含 14 首曲目的播放列表对此进行了测试。
youtube_dl
这种行为正常吗?还是我做错了什么?
youtube_dl
应该从播放列表中的 每 首歌曲下载并解析数据,所以毫不奇怪,它会花费很多时间。
最好加载队列中下一首应该播放的歌曲,这样速度会快很多,问题也就解决了。
我的 python 机器人可以在语音频道中播放音乐。我使用了 here 中的 YTDLSource class 并编辑了 class 以支持播放列表。这是我的 YTDLSource 代码:
youtube_dl.utils.bug_reports_message = lambda: ''
ytdl_format_options = {
'format': 'bestaudio/best',
'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
'restrictfilenames': True,
'noplaylist': False,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'default_search': 'auto',
'source_address': '0.0.0.0'
}
ffmpeg_options = {
'options': '-vn',
"before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5"
}
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, data, *, volume=0.5):
super().__init__(discord.FFmpegPCMAudio(data['url'], **ffmpeg_options), volume)
self.data = data
self.title = data.get('title')
self.url = data.get('webpage_url')
@classmethod
async def get(cls, url, *, volume=0.5):
loop = asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=False))
if 'entries' in data:
return [cls(entry, volume=volume) for entry in data['entries']]
else:
return cls(data, volume=volume)
代码运行正常,但速度太慢。我检查了 youtube_dl
从播放列表中提取信息需要多长时间:
time1 = time.time()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=False))
print(f"Extracted info in {time.time() - time1} seconds")
我得到了 Extracted info in 12.71654725074768 seconds
。我已经使用来自 SoundCloud 的包含 14 首曲目的播放列表对此进行了测试。
youtube_dl
这种行为正常吗?还是我做错了什么?
youtube_dl
应该从播放列表中的 每 首歌曲下载并解析数据,所以毫不奇怪,它会花费很多时间。
最好加载队列中下一首应该播放的歌曲,这样速度会快很多,问题也就解决了。