import os
import urllib2
import json
import base64
Api_Key = "***"
Secrect_Key = "***"
url = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id='+Api_Key+'&client_secret='+Secrect_Key
res = urllib2.urlopen(url).read()
data = json.loads(res)
token = data['access_token'] #获取的token
print 'token获取成功:'+`token`
VOICE_RATE = 8000
WAVE_FILE = './data/voice2250102769878738808.wav'
USER_ID = "***"
WAVE_TYPE = 'wav'
#其它参数可参考sdk文档
f = open(WAVE_FILE,'r')
speech = base64.b64encode(f.read())
size = os.path.getsize(WAVE_FILE)
update = json.dumps({'format':WAVE_TYPE,'rate':VOICE_RATE,'channel':1,'cuid':USER_ID,'token':token,'speech':speech,'len':size})
url1="http://vop.baidu.com/server_api"
r = urllib2.urlopen(url1,update)
t = r.read()
result = json.loads(t)
print result
if result['err_msg']=='success.':
word = result['result'][0].encode('utf-8')
if word!='':
if word[len(word)-3:len(word)]==',':
print word[0:len(word)-3]
print word[0:len(word)-3]
else:
print word
else:
print "音频文件不存在或格式错误"
else:
print "错误"
代码如上,语音文件是微信的语音文件,文件格式为WAV,请问为什么会说我参数中的语音参数错误?
ps:我用python通过微信获取语音的api webwxgetvoice获取的语音,直接将文件格式改成了wav,在本地能播放。
谢谢好心人
回复小弯和小萌:
您好,建议自己先查一下音频格式,如果仍不行,发送音频到voice_feedback@baidu.com
追加,sn=17125232111488529011,不知道是否有用