【问题标题】:Discord receive audioDiscord 接收音频
【发布时间】:2018-07-15 18:20:32
【问题描述】:

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

【问题讨论】:

    标签: python-3.x audio artificial-intelligence speech-recognition discord.py


    【解决方案1】:

    这个功能实际上并不存在。有一个VoiceClient.poll_voice_ws 协程,但是那个just reads directly from the socket。您必须想出某种方法将其解码为音频并自行进一步处理。

    【讨论】:

      【解决方案2】:

      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)
      

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

      【讨论】:

      • 最近几天我一直在研究这个问题。查看 pycord 的 VoiceClient API,unpack_audio 是如何输入的?
      猜你喜欢
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 2012-04-28
      • 2021-01-26
      • 2020-11-04
      • 2019-09-20
      • 1970-01-01
      • 2018-07-13
      相关资源
      最近更新 更多