已处理完成拿走就能用
# -*- coding: utf-8 -*-
##版本升级规划,多线程升级,可以做到判断文本是否符合要求,读取输入和输出文件位置6-6
from huaweicloud_sis.client.tts_client import TtsCustomizationClient
from huaweicloud_sis.bean.tts_request import TtsCustomRequest
from huaweicloud_sis.bean.sis_config import SisConfig
from huaweicloud_sis.exception.exceptions import ClientException
from huaweicloud_sis.exception.exceptions import ServerException
import json
def ttsc_example(txt_in,audio_out):
""" 语音合成demo """
ak = '写自己的'
sk = '写自己的'
region = 'cn-east-3' # region,如cn-north-4
project_id = '写自己' # 同region一一对应。登录管理控制台,鼠标移动到右上角的用户名上,在下拉列表中选择我的凭证。在我的凭证页面,可以查看用户名、帐号名,在项目列表中查看项目。多项目时,展开“所属区域”,从“项目ID”列获取子项目ID。
text = txt_in # 待合成文本,不超过500字
path = audio_out # 保存路径,需要具体到音频文件,如D:/test.wav,可在设置中选择不保存本地
# step1 初始化客户端
config = SisConfig()
config.set_connect_timeout(10) # 设置连接超时,单位s
config.set_read_timeout(10) # 设置读取超时,单位s
# 设置代理,使用代理前一定要确保代理可用。 代理格式可为[host, port] 或 [host, port, username, password]
# config.set_proxy(proxy)
ttsc_client = TtsCustomizationClient(ak, sk, region, project_id, sis_config=config)
# step2 构造请求
ttsc_request = TtsCustomRequest(text)
# 设置请求,所有参数均可不设置,使用默认参数
# 设置属性字符串, language_speaker_domain, 默认chinese_xiaoyan_common
ttsc_request.set_property('chinese_xiaoyan_common')
# 设置音频格式,默认wav,可选mp3和pcm
ttsc_request.set_audio_format('mp3')
# 设置采样率,8000 or 16000, 默认8000
ttsc_request.set_sample_rate('16000')
# 设置音量,[0, 100],默认50
ttsc_request.set_volume(50)
# 设置音高, [-500, 500], 默认0
ttsc_request.set_pitch(0)
# 设置音速, [-500, 500], 默认0
ttsc_request.set_speed(-50)
# 设置是否保存,默认False
ttsc_request.set_saved(True)
# 设置保存路径,只有设置保存,此参数才生效
ttsc_request.set_saved_path(path)
# step3 发送请求,返回结果。如果设置保存,可在指定路径里查看保存的音频。
result = ttsc_client.get_ttsc_response(ttsc_request)
#print(json.dumps(result, indent=2, ensure_ascii=False))
print('已经合成完毕!')
while True:
print('\n欢迎使用智能语音合成助手,单次只能转换500字一下,超过将无法转换!\n')
print('\n退出程序运行请输入 q \n')
file_path = input("请输入文件路径 \n")
if file_path == 'q':
break
with open(file_path,encoding='utf-8') as file_object:
contents = file_object.read()
words = contents.rstrip()
num_words = len(words)
if num_words<= 500:
words = contents.rstrip()
num_words = len(words)
print(file_path + '共' + str(num_words) + '字')
else:
print('指定的文本内容超过500字需要调整文件或使用其它版本的语音助手功能!')
###以上为读取指定路径下面TXT文本并传递给contents
#设定一个变量,该变量为指定路径,windows系统下D盘 注意windows系统中使用‘\\’用于区分系统路径
#判断D盘下是否有document\整理输出数据,如何没有则创建
dir_name = 'D:\\Document\\整理语音数据\\'
file_name = input('请输入要保持的文件名: \n')
if file_path == 'q':
break
out_path = dir_name + file_name + '.mp3'
ttsc_example(contents,out_path)