将ByteIO编写为与Bluemix一起使用的音频python

问题描述 投票:1回答:1

我使用cURL发出请求。它返回的是音频,但首先将其存储在ByteIO对象中。我的问题是如何将ByteIO保存为音频文件?我已经成功保存了音频文件,但是,无法使用音频播放器读取它。

这是我到目前为止的内容:

import pycurl, json
from io import BytesIO

with open('cred.json') as data_file:
    data = json.load(data_file)

user = data["credentials"]['username']
password = data["credentials"]['password']

buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, "https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize")
c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: audio/mp3'])
c.setopt(pycurl.USERNAME, user)
c.setopt(pycurl.PASSWORD, password)
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, '{\"text\":\"Hello World\"}')
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()

open('helloworld.mp3', 'wb').write(buffer.getvalue())

编辑:我正在使用cURL,因为我正试图与IBM Bluemix系统上托管的服务进行对话。它说使用cURL向服务发出请求。我正在尝试使用IBM的语音合成服务。我请求将文本转换为语音(Hello World部分),然后返回音频。但是,当我保存收到的缓冲区并尝试打开文件时,它说它无法读取它。希望能回答您的问题。

python python-3.x audio ibm-cloud
1个回答
2
投票

没有选择生成MP3;唯一可用的选项是生成Ogg,WAV或FLAC文件。将accept参数指定为标题(如对pycurl接口所做的操作)或作为URL查询参数。使用JSON参数发布它对我造成了400错误。

这里不要使用pycurl界面。它是一个[[hugely繁琐的API。您最好在这里使用requests library

requests
© www.soinside.com 2019 - 2024. All rights reserved.