腾讯云语音合成



语音合成 计费概述(在线版)-购买指南-文档中心-腾讯云 (tencent.com)

语音合成资源包 - 语音技术 - 控制台 (tencent.com)

合成音频 - 语音技术 - 控制台 (tencent.com)

访问密钥 - 控制台 (tencent.com)

免费额度

只要您开通了通用语音合成服务(长文本暂不支持),无论您选择预付费还是后付费的计费方式,都可以享受免费调用额度,免费调用额度将以免费资源包的形式配送,需要您在 语音合成控制台 领取,领取成功后会在计费结算时优先扣减。

免费额度如下:

语音合成免费额度为800万字符,仅支持通用语音合成接口,暂不支持长文本语音合成接口。免费资源包自您领取成功后起三个月内有效,一个账号只能领取一次。

预付费(基础资源包)

通用语音合成和长文本语音合成的标准音色和精品音色均支持预付费模式,预付费包若购买7天内无使用,可通过 资源包管理 申请退款。若超过7天,无论是否有使用,一律不予退款。如需购买,请单击 购买资源包

服务名称

预付费包有效期

预付费包大小(万字符)

预付费包价格(元)

单价(元/万字符)

通用语音合成-标准音色

1年

10,000

1,400

0.14

100,000

12,000

0.12

1000,000

100,000

0.10

通用语音合成-精品音色

1年

10,000

2,100

0.21

100,000

18,000

0.18

1000,000

150,000

0.15

import json
import os
import base64
import time
from datetime import datetime
from random import choice
from tencentcloud.common import credential
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

# 定义音频输出目录和输入文本文件路径
output_dir = r"G:\tencent"
input_file = r"G:\1.txt"

# 确保输出目录存在
os.makedirs(output_dir, exist_ok=True)

# 读取文本文件
with open(input_file, "r", encoding="utf-8") as f:
    lines = f.readlines()

# 实例化一个认证对象
# https://console.cloud.tencent.com/cam/capi
# https://cloud.tencent.com/document/product/1073/37995
# https://cloud.tencent.com/document/product/1073/92668

cred = credential.Credential("SecretId", "SecretKey")
httpProfile = HttpProfile()
httpProfile.endpoint = "tts.tencentcloudapi.com"

clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = tts_client.TtsClient(cred, "ap-beijing", clientProfile)

# 声音类型和情感类别映射
voice_emotion_map = {
    "10510000": ["neutral"],
    "1001": ["neutral"],
    "1002": ["neutral"],
    "1003": ["neutral"],
    "1004": ["neutral"],
    "1005": ["neutral"],
    "1007": ["neutral"],
    "1008": ["neutral"],
    "1009": ["neutral"],
    "1010": ["neutral"],
    "1017": ["neutral"],
    "1018": ["neutral"],
    "1050": ["neutral"], # 支持英文
    "1051": ["neutral"],
    "100510000": ["neutral"],
    "101001": ["neutral"],
    "101002": ["neutral"],
    "101003": ["neutral"],
    "101004": ["neutral"],
    "101005": ["neutral"],
    "101006": ["neutral"],
    "101007": ["neutral"],
    "101008": ["neutral"],
    "101009": ["neutral"],
    "101010": ["neutral"],
    "101011": ["neutral"],
    "101012": ["neutral"],
    "101013": ["neutral"],
    "101014": ["neutral"],
    "101015": ["neutral"],
    "101016": ["neutral"],
    "101017": ["neutral"],
    "101018": ["neutral"],
    "101019": ["neutral"],
    "101020": ["neutral"],
    "101021": ["neutral"],
    "101022": ["neutral"],
    "101023": ["neutral"],
    "101024": ["neutral"],
    "101025": ["neutral"],
    "101026": ["neutral"],
    "101027": ["neutral"],
    "101028": ["neutral"],
    "101029": ["neutral"],
    "101030": ["neutral"],
    "101031": ["neutral"],
    "101032": ["neutral"],
    "101033": ["neutral"],
    "101081": ["neutral"],
    "101080": ["neutral"],
    "101034": ["neutral"],
    "101035": ["neutral"],
    "101040": ["neutral"],
    "101050": ["neutral"], # 支持英文
    "101051": ["neutral"], # 支持英文
    "101052": ["neutral"],
    "101053": ["neutral"],
    "101054": ["neutral"],
    "101055": ["neutral"],
    "101056": ["neutral"],
    "101057": ["neutral"], # 支持日文
    "301000": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301001": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301002": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301003": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301004": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301005": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301006": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301007": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301008": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301009": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301010": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301011": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301012": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301013": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301014": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301015": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301016": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301017": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301018": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301019": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301020": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301021": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301022": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301023": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301024": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301025": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301026": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301027": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301028": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301029": ["neutral", "happy", "angry", "sad", "fear", "news", "story", "radio", "poetry", "call"],
    "301030": ["neutral"],
    "301031": ["neutral", "happy", "angry", "sad", "fear", "amaze", "sajiao", "disgusted"],
    "301032": ["neutral", "happy", "angry", "sad", "fear", "amaze", "sajiao", "disgusted"],
    "301033": ["neutral", "happy", "angry", "sad", "fear", "amaze", "sajiao", "disgusted"],
    "301034": ["neutral", "happy", "angry", "sad", "fear", "amaze", "sajiao", "disgusted"],
    "301035": ["neutral", "happy", "angry", "sad", "fear", "amaze", "sajiao", "disgusted"],
    "301036": ["neutral", "happy", "angry", "sad", "fear", "amaze", "sajiao", "disgusted"],
    "301037": ["neutral"],
    "301038": ["neutral", "peaceful", "exciting", "poetry", "sad", "angry", "fear", "sajiao", "disgusted"],
    "301039": ["neutral", "happy", "angry", "sad", "fear", "radio", "peaceful", "exciting", "sajiao", "aojiao"],
    "301040": ["neutral", "happy", "angry", "sad", "fear", "radio", "exciting", "jieshuo"],
    "301041": ["neutral", "angry", "sad", "fear", "sajiao", "disgusted"]
}

# 生成音频文件
for voice_type, emotions in voice_emotion_map.items():
    for emotion in emotions:
        try:
            # 随机选择一行文本
            line = choice(lines).strip()

            # 实例化一个请求对象
            req = models.TextToVoiceRequest()
            params = {
                "Text": line,
                "SessionId": f"SecretId_{voice_type}_{emotion}",
                "Volume": 1,
                "Speed": 1,
                "Codec": "wav",
                "PrimaryLanguage": 2, # 1 for Chinese, 2 for English
                "VoiceType": int(voice_type),
                "EmotionCategory": emotion
            }
            req.from_json_string(json.dumps(params))

            # 发送请求并接收响应
            resp = client.TextToVoice(req)
            print(resp.to_json_string())

            # 提取base64编码的音频内容
            audio_base64 = resp.Audio
            audio_data = base64.b64decode(audio_base64)

            # 保存解码后的音频数据到WAV文件
            output_filename = os.path.join(output_dir, f"fake_{voice_type}_{emotion}.wav")
            with open(output_filename, "wb") as audio_file:
                audio_file.write(audio_data)

            print(f"WAV file saved as {output_filename}")

            # 延时1秒防止请求频率过高
            time.sleep(1)

        except TencentCloudSDKException as err:
            print(f"SDK Exception for voice_type {voice_type}, emotion {emotion}: {err}")
        except Exception as e:
            print(f"An error occurred for voice_type {voice_type}, emotion {emotion}: {e}")

好的,您可以参考以下代码实现: ```javascript const fs = require('fs'); const request = require('request'); // 从腾讯云获取语音合成的结果并保存为MP3文件 function saveTTSResultToMP3(ttsResultUrl, fileName) { return new Promise((resolve, reject) => { const writeStream = fs.createWriteStream(fileName); request(ttsResultUrl) .pipe(writeStream) .on('close', () => { resolve(); }) .on('error', (err) => { reject(err); }); }); } // 调用腾讯云语音合成接口 function callTTSAPI() { // 这里省略调用腾讯云语音合成接口的代码 // 获取到语音合成的结果url后,假设为ttsResultUrl const ttsResultUrl = 'http://xxx.xxx.xxx.xxx/xxx.mp3'; const fileName = 'ttsResult.mp3'; saveTTSResultToMP3(ttsResultUrl, fileName) .then(() => { console.log('语音合成结果已保存为MP3文件:' + fileName); }) .catch((err) => { console.error('保存语音合成结果为MP3文件时出错:' + err); }); } callTTSAPI(); ``` 在上面的代码中,我们使用`request`模块从腾讯云获取到语音合成的结果url后,将其保存为MP3文件。`saveTTSResultToMP3`函数返回一个Promise对象,用于异步保存语音合成结果。在`callTTSAPI`函数中,我们调用腾讯云语音合成接口并获取到语音合成的结果url后,调用`saveTTSResultToMP3`函数将其保存为MP3文件。 注意,在使用这段代码时,您需要将`ttsResultUrl`替换为您从腾讯云获取到的语音合成结果url。另外,需要安装`request`模块,可以使用`npm install request`命令进行安装。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值