不和谐接收音频

Lar*_*rer 3 audio speech-recognition artificial-intelligence python-3.x discord.py

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

Loa*_*ahL 5

discord.py fork pycord 最近的更新增加了录制音频的可能性,然后您可以将其用于语音识别。要开始录制音频,您只需使用VoiceClient.start_recordinga进行调用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)
Run Code Online (Sandbox Code Playgroud)

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