Discord如何接收音频?

3

我希望能够从Discord接收音频以进行语音识别。 在Python Discord API中,我没有找到相关的内容。 语音识别不是问题,但我不知道如何从Discord接收音频。 希望有人能帮助我。

2个回答

5
最近discord.py的分支pycord更新了,添加了录制音频的功能,你可以将其用于语音识别。 要开始录制音频,你只需要使用VoiceClient.start_recording调用一个Sink,这个函数会在停止录音后被调用,并且可以为该函数提供可选参数。例如:
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)

因为这不是一个音频流,所以您不能使用它制作一个自动监听您的虚拟助手,但您可以记录语音频道并获取其中所说的内容。


我最近一直在研究这个。看着pycord的VoiceClient API,unpack_audio是如何与之配合的呢? - paultop6

4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接