youtube_dl error: Failed to parse JSON (caused by JSONDecodeError('Expecting value: line 1 column 1 (char 0)')) disocrd error
youtube_dl error: Failed to parse JSON (caused by JSONDecodeError('Expecting value: line 1 column 1 (char 0)')) disocrd error
错误:
youtube_dl.utils.DownloadError:错误:查询“歌曲”:无法解析 JSON(由 JSONDecodeError('Expecting value: line 1 column 1 (char 0)')
引起
播放命令:
@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)
请指导我如何解决这个错误这个错误是我上周开始的,我的 youtube_dl 也更新了
如果您需要其他代码,请告诉我,但请解决我的问题
我也不知道如何获取包含堆栈跟踪的错误消息
您出现此错误是因为 youtube-dl
已被删除,这意味着 public 无法再访问它。
而不是直接使用 youtube-dl
,您可以使用像 pafy
这样的库,它使用 youtube-dl
的 command-line 版本:
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
不包括 youtube 搜索,因此您必须自己搜索。为此你有多种选择:
- 使用
bs4
进行一些网络抓取
- 使用
fast-youtube-search
这样的库
- 使用
aiohttp
(which is the async version of requests
) and re
查找视频 ID。最后一个选项可能是最快的。
如果你想要一个更简单的解决方案,你可以使用 pytube
但它速度较慢并且最多支持 python 3.7.
错误:
youtube_dl.utils.DownloadError:错误:查询“歌曲”:无法解析 JSON(由 JSONDecodeError('Expecting value: line 1 column 1 (char 0)')
引起播放命令:
@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)
请指导我如何解决这个错误这个错误是我上周开始的,我的 youtube_dl 也更新了
如果您需要其他代码,请告诉我,但请解决我的问题
我也不知道如何获取包含堆栈跟踪的错误消息
您出现此错误是因为 youtube-dl
已被删除,这意味着 public 无法再访问它。
而不是直接使用 youtube-dl
,您可以使用像 pafy
这样的库,它使用 youtube-dl
的 command-line 版本:
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
不包括 youtube 搜索,因此您必须自己搜索。为此你有多种选择:
- 使用
bs4
进行一些网络抓取
- 使用
fast-youtube-search
这样的库
- 使用
aiohttp
(which is the async version ofrequests
) andre
查找视频 ID。最后一个选项可能是最快的。
如果你想要一个更简单的解决方案,你可以使用 pytube
但它速度较慢并且最多支持 python 3.7.