gpt4 book ai didi

python-3.x - Discord 接收音频

转载 作者:行者123 更新时间:2023-12-02 22:22:18 31 4
gpt4 key购买 nike

我想从 Discord 接收音频以进行语音识别。我还没有在 python Discord APi 中找到任何东西。语音识别没问题,但我不知道如何从 Discord 接收音频。也许有人可以帮助我。

最佳答案

discord.py fork pycord 的最新更新增加了录制音频的可能性,然后您可以将其用于语音识别。要开始录制音频,您只需使用 Sink 调用 VoiceClient.start_recording,该函数将在您停止录制后调用,以及该函数的可选参数。一个例子是:

import discord

bot = discord.Bot(debug_guilds=[719996466290098180])

@bot.command()
async def start_record(ctx):
await ctx.author.voice.channel.connect() # Connect to the voice channel of the author
ctx.voice_client.start_recording(discord.sinks.MP3Sink(), finished_callback, ctx) # Start the recording
await ctx.respond("Recording...")

async def finished_callback(sink, ctx):
# Here you can access the recorded files:
recorded_users = [
f"<@{user_id}>"
for user_id, audio in sink.audio_data.items()
]
files = [discord.File(audio.file, f"{user_id}.{sink.encoding}") for user_id, audio in sink.audio_data.items()]
await ctx.channel.send(f"Finished! Recorded audio for {', '.join(recorded_users)}.", files=files)

@bot.command()
async def stop_recording(ctx):
ctx.voice_client.stop_recording() # Stop the recording, finished_callback will shortly after be called
await ctx.respond("Stopped!")


bot.run(Token)

因为这不是音频流,所以你不能用它来制作一个会自动听你说话的虚拟助手,但你可以录制语音 channel 并获取其中所说的内容。

关于python-3.x - Discord 接收音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51350976/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com