使用yt-dlp-kn-python流式传输音频,而不是下载文件



我想为discord bot流式传输音频,而不是下载文件并从中提取音频文件。我尝试了来自的代码。有没有一种方法可以使用python 3.7中的youtube dl或pafy库直接从youtube视频流式传输音频?

是简单

  1. 安装yt_dlp
  2. 导入它

使用此代码:

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

示例:

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

您可以使用yt_dlp中的文档调整ydl_opts。

问候!

最新更新