1 Star 0 Fork 11

白飞飞/视频生成工具

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
voicedemo.py 3.91 KB
一键复制 编辑 原始数据 按行查看 历史
清真奶片 提交于 2021-09-12 14:50 . update voicedemo.py.
# -*- coding: UTF-8 -*-
# Python 2.x引入httplib模块。
# import httplib
# Python 3.x引入http.client模块。
import http.client
# Python 2.x引入urllib模块。
# import urllib
# Python 3.x引入urllib.parse模块。
import urllib.parse
import json
def processGETRequest(appKey, token, text, audioSaveFile, format, sampleRate) :
host = 'nls-gateway.cn-shanghai.aliyuncs.com'
url = 'https://' + host + '/stream/v1/tts'
# 设置URL请求参数
url = url + '?appkey=' + appKey
url = url + '&token=' + token
url = url + '&text=' + text
url = url + '&format=' + format
url = url + '&sample_rate=' + str(sampleRate)
#voice 发音人,可选,默认是xiaoyun。
#url = url + '&voice=' + 'qingqing'
#url = url + '&voice=' + 'ailun'
url = url + '&voice=' + 'maoxiaomei'
# volume 音量,范围是0~100,可选,默认50。
# url = url + '&volume=' + str(50)
# speech_rate 语速,范围是-500~500,可选,默认是0。
# url = url + '&speech_rate=' + str(0)
# pitch_rate 语调,范围是-500~500,可选,默认是0。
# url = url + '&pitch_rate=' + str(0)
print(url)
# Python 2.x请使用httplib。
# conn = httplib.HTTPSConnection(host)
# Python 3.x请使用http.client。
conn = http.client.HTTPSConnection(host)
conn.request(method='GET', url=url)
# 处理服务端返回的响应。
response = conn.getresponse()
print('Response status and response reason:')
print(response.status ,response.reason)
contentType = response.getheader('Content-Type')
print(contentType)
body = response.read()
if 'audio/mpeg' == contentType :
with open(audioSaveFile, mode='wb') as f:
f.write(body)
print('The GET request succeed!')
print(audioSaveFile)
else :
print('The GET request failed: ' + str(body))
conn.close()
def processPOSTRequest(appKey, token, text, audioSaveFile, format, sampleRate) :
host = 'nls-gateway.cn-shanghai.aliyuncs.com'
url = 'https://' + host + '/stream/v1/tts'
# 设置HTTPS Headers。
httpHeaders = {
'Content-Type': 'application/json'
}
# 设置HTTPS Body。
body = {'appkey': appKey, 'token': token, 'text': text, 'format': format, 'sample_rate': sampleRate}
body = json.dumps(body)
print('The POST request body content: ' + body)
# Python 2.x请使用httplib。
# conn = httplib.HTTPSConnection(host)
# Python 3.x请使用http.client。
conn = http.client.HTTPSConnection(host)
conn.request(method='POST', url=url, body=body, headers=httpHeaders)
# 处理服务端返回的响应。
response = conn.getresponse()
print('Response status and response reason:')
print(response.status ,response.reason)
contentType = response.getheader('Content-Type')
print(contentType)
body = response.read()
if 'audio/mpeg' == contentType :
with open(audioSaveFile, mode='wb') as f:
f.write(body)
print('The POST request succeed!')
else :
print('The POST request failed: ' + str(body))
conn.close()
from constant import appKey,token
# 采用RFC 3986规范进行urlencode编码。
def get_url_encode(text):
textUrlencode = text
# Python 2.x请使用urllib.quote。
# textUrlencode = urllib.quote(textUrlencode, '')
# Python 3.x请使用urllib.parse.quote_plus。
textUrlencode = urllib.parse.quote_plus(textUrlencode)
textUrlencode = textUrlencode.replace("+", "%20")
textUrlencode = textUrlencode.replace("*", "%2A")
textUrlencode = textUrlencode.replace("%7E", "~")
return textUrlencode
#print('text: ' + textUrlencode)
audioSaveFile = 'syAudio1.wav'
format = 'wav'
text = '盘点近期特别新品'
sampleRate = 16000
# GET请求方式
#processGETRequest(appKey, token, get_url_encode(text), audioSaveFile, format, sampleRate)
# POST请求方式
# processPOSTRequest(appKey, token, text, audioSaveFile, format, sampleRate)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/byunfei/video-generation-tool.git
git@gitee.com:byunfei/video-generation-tool.git
byunfei
video-generation-tool
视频生成工具
master

搜索帮助