1

i want to stream audio for a discord bot instead of downloading the file and extracting the audio file from it. i have tried code from Is there a way to directly stream audio from a youtube video using youtube-dl or pafy library in python 3.7?

1 Answers1

2

Yes Simple

  1. Install yt_dlp
  2. Import it

Use This Code :

ffmpeg_options = {'options': '-vn'}
ydl_opts = {'format': 'bestaudio'}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
    song_info = ydl.extract_info(url, download=False)

ctx.voice_client.play(discord.FFmpegPCMAudio(song_info["url"], **ffmpeg_options))    

Example:

async def streamx(ctx, url):
voiceChannel = ctx.message.author.voice.channel //get Message Sender Channel. When you want it to join without a seperat function.
await voiceChannel.connect() //same applies to this
ffmpeg_options = {'options': '-vn'}
ydl_opts = {'format': 'bestaudio'}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
    song_info = ydl.extract_info(url, download=False)

ctx.voice_client.play(discord.FFmpegPCMAudio(song_info["url"], **ffmpeg_options))    

You can adapt the ydl_opts with the documentation from yt_dlp.

Greetings!