**我用的是Centos7系统
1、注册百度账号并登陆
https://login.bce.baidu.com/
2、找到所需要的服务
3、创建应用
创建完成会生成以下信息
4、需要安装的包有
pip3 install pydub
pip3 install wave(这个包好像是python自带的,import wave报错再装)
需要录音的话需要安装以下包:
pip3 install pyaudio
安装pyaudio之前需要安装portaudio
[添加链接描述](http://portaudio.com/download.html)下载 pa_stable_v190600_20161030.tgz (或其他版本)
解压:
tar zxvf pa_stable_v190600_20161030.tgz
移动到/usr/local文件夹:
mv portaudio /usr/local
进入portaudio:
cd /usr/local/portaudio
执行以下命令:
./configure
make
make install
5、音频格式转换还需要安装ffmpeg
1)安装Nux Dextop Yum 源
sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
2)安装FFmpeg 和 FFmpeg开发包
sudo yum install ffmpeg ffmpeg-devel -y
6、编写代码(我新建的子应用)
1)在utils.py里添加以下代码
import base64
import datetime
import json
import os
import wave
import requests
from pyaudio import PyAudio, paInt16
from pydub import AudioSegment
# 获取token
def get_access_token():
API_Key = 'eINfdS6lsyGOliLXeqtsp5dd' # 换成你自己的
Secret_Key = 'GKgz239UmCB8GS3B98h686ZUVxd998WH' # 换成你自己的
url = 'https://openapi.baidu.com/oauth/2.0/token?'
payload = {
"grant_type": "client_credentials",
"client_id": API_Key,
"client_secret": Secret_Key
}
response = requests.get(url, params=payload, timeout=3)
json_str = json.loads(response.text)
access_token = json_str['access_token']
return access_token
framerate = 16000
NUM_SAMPLES = 2000
channels = 1
sampwidth = 2
TIME = 2
# 前端传来的语音文件写入方式
def save_wave_file(filename, data):
wf = wave.open(filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(sampwidth)
wf.setframerate(framerate)
wf.writeframes(b"".join(data))
wf.close()
# 音频格式转换(mp3转wav)
def trans_mp3_to_wav():
# 转换前的语音地址
song = AudioSegment.from_mp3('/home/wwwroot/zcc/01.mp3')
# 转换后的语音地址
song.export("/home/wwwroot/zcc/01.wav", format="wav")
# 百度音频转换接口
def get_word(file_name, get_vido):
# 音频文件写入到服务器
save_wave_file(file_name, get_vido)
# 音频为mp3格式转为wav格式
trans_mp3_to_wav()
with open('/home/wwwroot/zcc/01.wav', "rb") as f:
speech = base64.b64encode(f.read()).decode('utf8')
size = os.path.getsize(r'/home/wwwroot/zcc/01.wav')
headers = {'Content-Type': 'application/json'}
access_token = get_access_token()
url = 'https://vop.baidu.com/pro_api'
data = {
"format": "wav",
"rate": 16000,
"channel": 1,
"cuid": 'zhichacha',
"dev_pid": 80001,
"token": access_token,
"len": size,
"speech": speech,
}
req = requests.post(url, json.dumps(data), headers)
result = json.loads(req.text)
return result
# 录音(微信小程序开发腾讯有录音功能所以这个没用上)
def my_record():
pa = PyAudio()
stream = pa.open(format=paInt16, channels=1,
rate=framerate, input=True,
frames_per_buffer=NUM_SAMPLES)
my_buf = []
count = 0
# print('.')
# 控制录音时间
while count < TIME * 10:
string_audio_data = stream.read(NUM_SAMPLES)
my_buf.append(string_audio_data)
count += 1
save_wave_file('01.wav', my_buf)
stream.close()
2)视图逻辑
from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from baiduapi.utils import get_word
class GetWordview(GenericAPIView):
"""
音频转换
"""
def post(self, request):
# 获取音频文件
get_audio = request.data.get('audio', None)
# 定义路径
file_name = '/home/wwwroot/zcc/01.mp3'
# 是否为空
if get_audio:
# 百度音频接口
data = get_word(file_name, get_audio)
if data['err_no'] == 0:
# 返回数据
data = {
'status': 200,
'msg': 'OK',
'data': data['result'][0].replace('。', '')
}
return Response(data=data, status=status.HTTP_200_OK)
else:
# 返回数据
data = {
'status': data['err_no'],
'msg': data['err_msg'],
}
return Response(data=data, status=status.HTTP_200_OK)
else:
data = {
'status': 202,
'msg': '参数缺失'
}
return Response(data=data, status=status.HTTP_200_OK)
*****小程序开发如若测试成功,但体验版失败请在小程序后台需配置uploadFile合法域名
大功告成!!!!!!!!