华为语音合成SDK使用,换成自己的KEY和ID就能用

本文档提供了一个使用华为云TTS服务进行语音合成的Python代码示例,包括初始化客户端、构造请求及发送请求的过程。该示例能够读取文本文件内容,合成MP3音频并保存至指定路径。同时,程序会检查输入文本长度,确保不超过500字的限制。
摘要由CSDN通过智能技术生成

已处理完成拿走就能用

# -*- 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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洙洺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值