Search code examples
pythonyoutube-dldiscord

YoutubeDL Read Error with Discord.py


I'm making a bot for Discord using Discord.py, and I keep running into this error when the user requests to play a song, if it is longer than a certain time. The song cuts off and then the player plays the next song in the queue.

I'm using the default state.voice.create_ytdl_player() included with the library.

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

My code:

@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)

Solution

  • After doing some research, I found that adding before_options="-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5" to the create_ytdl_player function fixed the music being cutoff.

    • -reconnect 1 tells the player to reconnect if the connection to the video fails. Same with -reconnect_streamed 1, except with a stream.
    • -reconnect_delay_max 5 sets the re-connection timeout to 5 seconds. If the re-connection fails, then it aborts.