def get_voice(text: str, voice_type: int, is_long: bool = False) -> tuple: client = tts_client.TtsClient(cred, "ap-guangzhou", client_profile) req = models.TextToVoiceRequest( ) if not is_long else models.CreateTtsTaskRequest() params = { "Text": text, "SessionId": str(uuid.uuid4()), "ModelType": 1, "VoiceType": int(voice_type), "Volume": 10, "Codec": "wav" } req.from_json_string(json.dumps(params)) try: resp = client.TextToVoice( req) if not is_long else client.CreateTtsTask(req) except TencentCloudSDKException as err: logger.error(traceback.format_exc()) if err.get_code() == "UnsupportedOperation.TextTooLong": return Speak.get_voice(text, voice_type, is_long=True) return -1, str(err) if not is_long: voice = json.loads(resp.to_json_string())["Audio"] return 0, base64.b64decode(voice) else: return 1, json.loads(resp.to_json_string())["Data"]["TaskId"]
def synthesis(self, text, voiceType='0', speed='0'): try: # 获取腾讯云凭证 cred = credential.Credential(self.__secretId, self.__secretKey) # 请求的配置 httpProfile = HttpProfile() # 请求访问的域名 httpProfile.endpoint = "tts.tencentcloudapi.com" # sdk的配置 clientProfile = ClientProfile() # 设置sdk的请求配置 clientProfile.httpProfile = httpProfile # 腾讯tts句柄 # "ap-guangzhou" 该值可能为请求的服务器位置,这里为广州,可登录腾讯云控制台查看:https://console.cloud.tencent.com/api/explorer?Product=tts client = tts_client.TtsClient(cred, "ap-guangzhou", clientProfile) # 腾讯tts请求参数结构体 req = models.TextToVoiceRequest() params = '{"Text":"' + text + '","VoiceType":"' + voiceType + '","SessionId":"' + str( uuid.uuid4() ) + '","ModelType":1,"Speed":' + speed + ',"SampleRate":16000,"Codec":"wav","Volume":0}' req.from_json_string(params) # 生成base64语音 resp = client.TextToVoice(req) # 将base64的字符转换成二进制流 return base64.b64decode(resp.Audio) except TencentCloudSDKException as err: print(err)
def get_long_voice_status(task_id: str): client = tts_client.TtsClient(cred, "", client_profile) req = models.DescribeTtsTaskStatusRequest() params = {"TaskId": task_id} req.from_json_string(json.dumps(params)) resp = client.DescribeTtsTaskStatus(req) return json.loads(resp.to_json_string())["Data"]["Status"]
def auth(self): self.load_auth_file() cred = credential.Credential(self.auth_obj["id"], self.auth_obj["key"]) httpProfile = HttpProfile() httpProfile.endpoint = "tts.tencentcloudapi.com" clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile client = tts_client.TtsClient(cred, "ap-beijing", clientProfile) self.client = client
def __init__(self): cred = credential.Credential(SECRETID, SECRETKEY) httpProfile = HttpProfile() httpProfile.endpoint = "tts.tencentcloudapi.com" clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile self.client = tts_client.TtsClient(cred, "ap-beijing", clientProfile) self.req = models.TextToVoiceRequest()
def __init__(self): cred = credential.Credential("AKIDLWyFKBsNhw0jInuFD1g9YlnjU4QWqTK0", "hDYhFbs0uc8YmndeU9aqvpbCZlmbFtsf") # 实例化一个 http 选项,可选的,没有特殊需求可以跳过。 httpProfile = HttpProfile() httpProfile.endpoint = "tts.tencentcloudapi.com" # 实例化一个 client 选项,可选的,没有特殊需求可以跳过。 clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile # 实例化要请求产品 (以 cvm 为例) 的 client 对象,clientProfile 是可选的。 self.client = tts_client.TtsClient(cred, "ap-guangzhou", clientProfile) self.req = models.TextToVoiceRequest()
def __init__(self): cred = credential.Credential("xxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxx") #需要填上你自己的api! # 实例化一个 http 选项,可选的,没有特殊需求可以跳过。 httpProfile = HttpProfile() httpProfile.endpoint = "tts.tencentcloudapi.com" # 实例化一个 client 选项,可选的,没有特殊需求可以跳过。 clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile # 实例化要请求产品 (以 cvm 为例) 的 client 对象,clientProfile 是可选的。 self.client = tts_client.TtsClient(cred, "ap-guangzhou", clientProfile) self.req = models.TextToVoiceRequest()
def tx_voice(text,voice_id): try: cred = credential.Credential(SecretId, SecretKey) httpProfile = HttpProfile() httpProfile.endpoint = "tts.tencentcloudapi.com" clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile client = tts_client.TtsClient(cred, "ap-shanghai", clientProfile) req = models.TextToVoiceRequest() request_data = { 'Action': 'TextToVoice', 'Version': '2019-08-23', 'Region': 'ap-shanghai', 'Text': text, 'SessionId': str(uuid4()), 'ModelType': 0, 'Volume': 5, 'Speed': 1, # 控制语速 [-2.-1,0,1,2] 'ProjectId': 0, 'VoiceType': int(voice_id), # 设置发音角色 'PrimaryLanguage': 1, 'SampleRate': 16000, 'Codec': 'mp3', 'Expired': 3600 } req.from_json_string(json.dumps(request_data)) resp = client.TextToVoice(req) # content为base64解码后的二进制流 content = b64decode(resp.Audio) # I/O操作 filename = './data/' + str(voice_id) + '.mp3' with open(filename, 'wb') as f: f.write(content) except TencentCloudSDKException as err: print(err)
async def get_tx_voice(text, type=0) -> str: cred = credential.Credential(tts_config.tencent_secret_id, tts_config.tencent_secret_key) client = tts_client.TtsClient(cred, "ap-shanghai") req = models.TextToVoiceRequest() if type == 0: voice_type = 101016 else: voice_type = 101010 params = { "Text": text, "SessionId": str(uuid.uuid1()), "Volume": 5, "Speed": 0, "ProjectId": int(tts_config.tts_project_id), "ModelType": 1, "VoiceType": voice_type, } req.from_json_string(json.dumps(params)) resp = client.TextToVoice(req) return f"base64://{resp.Audio}"
from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException from tencentcloud.tts.v20190823 import tts_client, models SECRET_ID = "你的Secret ID" SECRET_KEY = "你的Secret Key" try: cred = credential.Credential(SECRET_ID, SECRET_KEY) httpProfile = HttpProfile() httpProfile.endpoint = "tts.tencentcloudapi.com" clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile client = tts_client.TtsClient(cred, "ap-beijing", clientProfile) req = models.TextToVoiceRequest() params = { "Text": "我已经把灯关了", "SessionId": "sessionid-geektime", "ModelType": 1, "ProjectId": 0, "VoiceType": 1002 } req.from_json_string(json.dumps(params)) resp = client.TextToVoice(req) print(resp.to_json_string()) if resp.Audio is not None: