如何将编码的音频字符串传递给JSON字节



我一直在尝试使用Python实现以下Shell代码。我即将利用扬声器识别API使用。因此,在使用它之前,我需要使用用户ID注册音频文件,在他们的文档中,没有给出python示例,而不是下面的shell命令。

curl -X POST "https://proxy.api.deepaffects.com/audio/generic/api/v1    /sync/diarization/enroll?apikey=<ACCESS_TOKEN>" -H 'content-type: application/json' -d @data.json
# contents of data.json
{"content": "bytesEncodedAudioString", "sampleRate": 8000, "encoding":   "FLAC", "languageCode": "en-US", "speakerId": "user1" }

到目前为止,我已经编写了以下代码。

 import requests
 url = 'https://proxy.api.deepaffects.com/audio/generic/api/v1   /sync/diarization/enroll?apikey=<3XY9aG7AbXZ4AuKyAip7SXfNNdc4mwq3>'
 data = {
     "content": "bytesEncodedAudioString", 
     "sampleRate": 8000, 
     "encoding": "FLAC",
     "languageCode": "en-US", 
     "speakerId": "Pranshu Ranjan",
  }
  headers = {'content-type': 'application/json'}
  r = requests.post(url, data=data, headers=headers) 
  print(r)

,但我不知道如何通过"content": "bytesEncodedAudioString"。我在本地目录中有MP3格式的音频样本。这是Deepaptects API参考,它们支持多个音频格式

根据文档:

content(string(base64音频文件的编码。

只使用内置的base64模块来编码音频文件:

import base64
import requests

filepath = "C:Audio...file.mp3"
with open(filepath, 'rb') as f:
    audio_encoded = base64.b64encode(f.read())  # read file into RAM and encode it
data = {
    "content": str(audio_encoded),  # base64 string
    "sampleRate": 8000, 
    "encoding": "FLAC",  # maybe "MP3" should be there?
    "languageCode": "en-US", 
    "speakerId": "My Name",
}
url = ...
r = requests.post(url, json=data)  # note json= here. Headers will be set automatically.

最新更新