如何使用json将编码的音频字符串传递给字节?

时间:2019-06-28 10:41:53

标签: python python-3.x audio encoding python-requests

我一直在尝试用python实现以下shell代码。 我将要使用deepaffects speaker identification 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格式的音频样本。 这是deepAffects api reference,它们支持多种音频格式

1 个答案:

答案 0 :(得分:1)

根据documentation

  

音频文件的内容(字符串)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": "Pranshu Ranjan",
}

url = ...
r = requests.post(url, json=data)  # note json= here. Headers will be set automatically.