码迷,mamicode.com
首页 > 编程语言 > 详细

讯飞云 API 语音听写 python3 调用例程

时间:2018-08-30 11:10:52      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:utf-8   上传   wav   调用   content   ram   json   cat   控制台   

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import requests
import time
import gzip
import urllib
import json
import hashlib
import base64


def audio_dictation():
    """
    讯飞语音听写 API 调用例程
    注意:使用前需要在讯飞云控制台中的IP白名单中加入本机IP!
    参考:讯飞云官方 API 文档 https://doc.xfyun.cn/rest_api/语音听写.html
    """
    
    # 读取 APPID:
    with open(‘./appid‘, mode=‘r‘, encoding=‘ASCII‘) as appid_file:
        x_appid = appid_file.read()
    
    # 读取APIKey:
    with open(‘./apikey‘, mode=‘r‘, encoding=‘ASCII‘) as apikey_file:
        api_key = apikey_file.read()
    
    # API URL:
    url = ‘http://api.xfyun.cn/v1/service/v1/iat‘
    
    # 读取音频文件内容 (将 `zhngjb_clip.wav` 替换成需要上传的音频文件):
    with open(‘./zhngjb_clip.wav‘, mode=‘rb‘) as audio_file:
        file_content = audio_file.read()
    
    # 对音频文件进行 base64 音频编码:
    base64_audio = base64.b64encode(file_content)
    body = urllib.parse.urlencode({‘audio‘: base64_audio})
    
    # 采样率 16k,编码格式 未压缩:
    param = {"engine_type": "sms16k", "aue": "raw"} 
    
    # 构建 header:
    x_param = base64.b64encode(json.dumps(param).replace(‘ ‘, ‘‘).encode())
    x_time = str(int(int(round(time.time() * 1000)) / 1000))
    x_checksum = hashlib.md5(api_key.encode() + str(x_time).encode() + x_param).hexdigest()
    x_header = {‘X-Appid‘: x_appid,
                ‘X-CurTime‘: x_time,
                ‘X-Param‘: x_param,
                ‘X-CheckSum‘: x_checksum}
    
    req_header = {‘Content-Type‘: ‘application/x-www-form-urlencoded‘,
                  ‘charset‘: ‘utf-8‘}
    
    headers = {**req_header, **x_header}   
    
    # 发送请求:
    start = time.time()
    response = requests.post(url, headers=headers, data=body)
    duration = time.time() - start
    
    with open(‘result‘, mode=‘w‘, encoding=‘utf-8‘) as result_file:
        print(f‘Request sent. Duration: {duration}s\n‘
              f‘status code = {response.status_code}\n‘
              f‘headers = {response.headers}\n‘
              f‘content = {response.content.decode("utf-8")}‘, file=result_file)

if __name__ == ‘__main__‘:
    audio_dictation() 

讯飞云 API 语音听写 python3 调用例程

标签:utf-8   上传   wav   调用   content   ram   json   cat   控制台   

原文地址:https://www.cnblogs.com/LexLuc/p/9557849.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!