YoutubeDL 读取错误 Discord.py

YoutubeDL Read Error with Discord.py

我正在使用 Discord.py 为 Discord 创建一个机器人,如果用户请求播放歌曲的时间超过特定时间,我会将 运行 保存到此错误中。歌曲中断,然后播放器播放队列中的下一首歌曲。

我正在使用库中包含的默认 state.voice.create_ytdl_player()

The error:

[tls @ 0000000000eb9ca0] Unable to read from socket
[matroska,webm @ 0000000000eb7620] Read error
[tls @ 0000000000eb9ca0] Unable to read from socket
    Last message repeated 1 times
[tls @ 0000000000eb9ca0] Failed to send close message
WARNING: unable to extract uploader nickname
WARNING: unable to extract uploader nickname

我的代码:

@commands.command(pass_context=True, no_pm=True)
async def play(self, ctx, *, song : str):
    state = self.get_voice_state(ctx.message.server)
    opts = {
        'default_search': 'auto',
        'quiet': True,
    }

    if state.voice is None:
        success = await ctx.invoke(self.summon)
        if not success:
            return

    try:
        tmp = await self.bot.send_message(ctx.message.channel, "Searching for `" + song + "`...")
        player = await state.voice.create_ytdl_player(song, ytdl_options=opts, after=state.toggle_next)
    except Exception as e:
        print(debugging.ERROR + "ERROR in 'play' command: " + str(e))
        fmt = ':warning: An error occurred while processing this request: ```py\n{}: {}\n```'
        await self.bot.send_message(ctx.message.channel, fmt.format(type(e).__name__, e))
    else:
        player.volume = 0.6
        entry = VoiceEntry(ctx.message, player)
        await self.bot.edit_message(tmp, ':notes: Added ' + str(entry) + ' to the song queue.')
        await state.songs.put(entry)

经过一些研究,我发现在 create_ytdl_player 函数中添加 before_options="-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5" 可以修复音乐被切断的问题。

  • -reconnect 1 告诉播放器在视频连接失败时重新连接。与 -reconnect_streamed 1 相同,但流除外。
  • -reconnect_delay_max 5 将重新连接超时设置为 5 秒。如果重新连接失败,则中止。