'Discord receive audio
I want to receive Audio from Discord to make a speech recognition. I haven't found something in the python Discord APi. The speech recognition is no problem, but I have no idea how to receive Audio from Discord. Maybe someone can help me.
Solution 1:[1]
A recent update to the discord.py fork pycord added the possibility to record audio, which you could then use for speech recognition.
To start recording the audio, you simply need to call VoiceClient.start_recording with a Sink, a function that will be called after you stopped recording, and optional arguments for that function. An example would be:
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)
Because this is not an audio stream, you can't use it to make a virtual assistant that will automatically listen to you, but you can record the voice channel and get what was said in it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chuaat |
