Error:
youtube_dl.utils.DownloadError: ERROR: query "song": Failed to parse JSON (caused by JSONDecodeError('Expecting value: line 1 column 1 (char 0)')
Play command:
@commands.command(name='play',aliases=['p'] )
async def _play(self, ctx: commands.Context, *, search: str):
async with ctx.typing():
try:
source = await YTDLSource.create_source(ctx, search, loop=self.bot.loop)
except YTDLError as e:
await ctx.send('Error: {}'.format(str(e)))
else:
song = Song(source)
await ctx.voice_state.songs.put(song)
Youtube-DL Class:
class YTDLSource(discord.PCMVolumeTransformer):
YTDL_OPTIONS = {
'format': 'bestaudio/best',
'extractaudio': True,
'audioformat': 'mp3',
'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
'restrictfilenames': True,
'noplaylist': True,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'default_search': 'auto',
'source_address': '0.0.0.0',
}
FFMPEG_OPTIONS = {
'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
'options': '-vn',
}
ytdl = youtube_dl.YoutubeDL(YTDL_OPTIONS)
Please guide me how to fix this error This error started for me last week and my youtube_dl is also updated
If you need another code, tell me, but please fix my problem
I also do not know how to get error message including stack trace
You have this error because youtube-dl
was taken down, which means that it's not accessible to public anymore.
Instead of directly using youtube-dl
, you can use libraries like pafy
that uses the command-line version of youtube-dl
:
from pafy import new
@client.command(pass_context=True)
async def play(ctx):
ffmpeg_opts = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
channel = ctx.author.voice.channel
voice = await channel.connect()
video = new("youtube video link")
audio = video.getbestaudio().url
voice.play(FFmpegPCMAudio(audio, **ffmpeg_opts))
voice.is_playing()
pafy
doesn't include youtube search so you'll have to do your own. For this you have multiple choices:
bs4
fast-youtube-search
aiohttp
(which is the async version of requests
) and re
to find the video id. This last option is probably the fastest one.If you want an easier solution, you can use pytube
but it's slower and supports up to python 3.7.