I have my python bot that can play music in voice channels. I used YTDLSource class from here and edited that class to support playlists. Here is my YTDLSource code:
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)
The code is working, but too slowly. I checked how much time it takes youtube_dl
to extract the info from a playlist as follows:
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")
And I got Extracted info in 12.71654725074768 seconds
. I have tested this with a playlist with 14 tracks from SoundCloud.
Is this behaviour normal with youtube_dl
or am I doing something wrong?
youtube_dl
should download and parse data from every song in playlist, so it's not surprisingly, it takes so much time.
It's better to load a song that should play next in the queue, so it will be much faster and the problem will be solved.