我如何将音频从pytube传输到FFMPEG和discord.py,而无需事先下载和转换

问题描述

我在我的一台小型服务器中安装了discord.py机器人程序。我已经设置了一个音乐命令,可以从YouTube下载歌曲并将其输出到VC。目前,该命令下载了完整的歌曲,将其转换,然后 then 将其输出到VC中,但是此过程非常缓慢。我将如何直接将音频流传输到VC?我愿意使用youtube_dl代替pytube3。我不太关心较小的代码优化,因为对于一些朋友和我来说,这只是一个小机器人。

谢谢您的输入!

@bot.command()
async def play(ctx,*song):
    if ctx.author.voice is None or ctx.author.voice.channel is None:
        await ctx.send("You aren't in a VC!")
        return
    print(song) #debugging
    os.system("rm music.mp3")
    ydl_opts = {
        'noplaylist': True,'outtmpl': 'music','postprocessors': [{
            'key': 'FFmpegExtractAudio','preferredcodec': 'mp3','preferredquality': '128',}],'format': '139',}
    youtube = pytube.YouTube(str(song).strip("(,)'"))
    video = youtube.streams.filter(only_audio=True).first()
    await ctx.send("downloading")
    video.download(filename="music")
    await ctx.send("converting...")
    os.system("ffmpeg -i music.mp4 -map 0:a:0 -b:a 96k music.mp3")
    
    voice_channel = ctx.author.voice.channel
    vc = await voice_channel.connect()
    vc.play(discord.FFmpegPCMAudio('music.mp3'),after=lambda e: print('done',e))
    while vc.is_playing():
        await asyncio.sleep(1)
    await ctx.voice_client.disconnect()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)