discord.py and youtube dl audio playback without jumping

How can you play audio in a bot using the youtube_dl library, but without installing the audio itself in the bot's directory (because of this installation, it is impossible to start streams with music like lo-fi) and if not how, then what library can be used for this (there are bots like MEE6, Rythm, Groove, which play streams calmly and instantly, and given that the audio launch is instant, and bots are very popular, then clearly this happens without jumping video)

Author: Maxim Filonenko, 2020-12-06

2 answers

This is how it is done:

from youtube_dl import YoutubeDL
from asyncio import sleep

YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist':'False'}
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}


@bot.command()
async def play(ctx, arg):
    global vc

    try:
        voice_channel = ctx.message.author.voice.channel
        vc = await voice_channel.connect()
    except:
        print('Уже подключен или не удалось подключиться')

    if vc.is_playing():
        await ctx.send(f'{ctx.message.author.mention}, музыка уже проигрывается.')

    else:
        with YoutubeDL(YDL_OPTIONS) as ydl:
            info = ydl.extract_info(arg, download=False)

        URL = info['formats'][0]['url']

        vc.play(discord.FFmpegPCMAudio(executable="ffmpeg\\ffmpeg.exe", source = URL, **FFMPEG_OPTIONS))
                
        while vc.is_playing():
            await sleep(1)
        if not vc.is_paused():
            await vc.disconnect()

To implement such a command, you need FFMPEG - the. exe file required to start the sound. This executor is run via the Discord API.

You can download it here. You can also download the archive (The download will start immediately)

There should be 3 files in the folder: ffmpeg.exe, ffplay.exe, ffprobe.exe.

Runs that explicitly specify the executable file ffmpeg.exe:

voice.play(discord.FFmpegPCMAudio(executable="путь\\к\\файлу\\ffmpeg.exe", source = URL, **FFMPEG_OPTIONS))
 1
Author: denisnumb, 2020-12-08 04:21:16

I figured out how to solve this, just specify in the parameters:

YDL_OPTIONS = {'format': 'worstaudio/best',
               'noplaylist': 'True', 'simulate': 'True', 'preferredquality': '192', 'preferredcodec': 'mp3', 'key': 'FFmpegExtractAudio'}
 0
Author: Maxim Filonenko, 2020-12-10 06:48:56