0

错误:

youtube_dl.utils.DownloadError:错误:查询“歌曲”:无法解析 JSON(由 JSONDecodeError 引起('期望值:第 1 行第 1 列(字符 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 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 也已更新

如果您需要其他代码,请告诉我,但请解决我的问题

我也不知道如何获取包括堆栈跟踪在内的错误消息

4

1 回答 1

0

您有此错误是因为youtube-dl已被删除,这意味着它不再可供公众访问。

除了直接使用之外youtube-dl,您还可以使用这样的库pafy,使用命令行版本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不包括 youtube 搜索,所以你必须自己做。为此,您有多种选择:

  • 使用bs4
  • 使用类似的库fast-youtube-search
  • 使用aiohttp(这是 的异步版本requests)并re查找视频 ID。最后一个选项可能是最快的一个。

如果您想要一个更简单的解决方案,您可以使用它,pytube但它速度较慢,并且最多支持 python 3.7。

于 2020-10-25T12:41:12.387 回答