加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mp3_to_script.py 2.02 KB
一键复制 编辑 原始数据 按行查看 历史
清真奶片 提交于 2021-09-15 08:21 . 语言文件转 文案 脚本
# -*- coding: UTF-8 -*-
# Python 2.x引入httplib模块
# import httplib
# Python 3.x引入http.client模块
import http.client
import json
#https://help.aliyun.com/document_detail/187161.html
def process(request, audioFile) :
# 读取音频文件
with open(audioFile, mode = 'rb') as f:
audioContent = f.read()
host = 'nls-gateway.cn-shanghai.aliyuncs.com'
# 设置HTTP请求头部
httpHeaders = {
'Content-Length': len(audioContent)
}
# Python 2.x使用httplib
# conn = httplib.HTTPConnection(host)
# Python 3.x使用http.client
conn = http.client.HTTPConnection(host)
conn.request(method='POST', url=request, body=audioContent, headers=httpHeaders)
response = conn.getresponse()
print('Response status and response reason:')
print(response.status ,response.reason)
body = response.read()
try:
print('Recognize response is:')
body = json.loads(body)
print(body)
status = body['status']
if status == 20000000 :
result = body['result']
print('Recognize result: ' + result)
else :
print('Recognizer failed!')
except ValueError:
print('The response is not json format string')
conn.close()
appKey = '您的appkey'
token = '您的token'
from constant import appKey,token
# 服务请求地址
url = 'https://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/FlashRecognizer'
# 音频文件,下载地址:https://gw.alipayobjects.com/os/bmw-prod/0574ee2e-f494-45a5-820f-63aee583045a.wav
audioFile = 'nls-sample-16k.wav'
audioFile = 'getvoice.mp3'
format = audioFile.split('.')[-1]
sampleRate = 16000
print(type(sampleRate))
enablePunctuationPrediction = True
enableInverseTextNormalization = True
enableVoiceDetection = False
# 设置RESTful请求参数
request = url + '?appkey=' + appKey
request = request + '&token=' + token
request = request + '&format=' + format
request = request + '&sample_rate=' + str(sampleRate)
print('Request: ' + request)
process(request, audioFile)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化