我在一个小型服务器上安装了discord.py bot。我已经设置了一个音乐命令,可以从YouTube下载一首歌曲并将其输出到VC中。目前,该命令下载完整歌曲,将其转换,然后将其输出到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()
您已经在使用
youtube_dl
(从ydl_opts
变量判断)。您可以做的是:pip install youtube-dl
)李>pip install requests
)video
变量包含什么,可以打印它李>FFMPEG_OPTS
变量。它会将机器人重新连接到源,这样它仍然能够流式传输视频,当这种情况发生时,您的终端中会出现一条您不必担心的奇怪消息李>相关问题 更多 >
编程相关推荐