火山引擎语音合成

试用额度

语音合成服务提供一定量的试用额度,试用额度的用量、可使用范围、有效期等详情以控制台领取页面显示为准。试用额度在额度用尽、试用到期或服务开通为正式版后失效。

额度并发有效期
语音合成2000次2半年

按调用次数后付费

日调用量阶梯(千次)按调用量后付费(元/千次)
【0<调用次数<=1000】5.5
【1000<调用次数<=5000】5
【5000<调用次数<=10000】4.5
【调用次数>10000】4

合成音色列表

import base64
import json
import uuid
import requests
import os
import time
import random


# 填写平台申请的appid, access_token以及cluster
appid = "xxxx"
access_token= "xxxx"
cluster = "xxxx"

voice_types = [
    "BV700_V2_streaming", "BV705_streaming", "BV701_V2_streaming", "BV001_V2_streaming", "BV700_streaming",
    "BV406_V2_streaming", "BV406_streaming", "BV407_V2_streaming", "BV407_streaming", "BV001_streaming",
    "BV002_streaming", "BV701_streaming", "BV123_streaming", "BV120_streaming", "BV119_streaming", "BV115_streaming",
    "BV107_streaming", "BV100_streaming", "BV104_streaming", "BV004_streaming", "BV113_streaming", "BV102_streaming",
    "BV405_streaming", "BV007_streaming", "BV009_streaming", "BV419_streaming", "BV415_streaming", "BV008_streaming",
    "BV408_streaming", "BV426_streaming", "BV428_streaming", "BV403_streaming", "BV158_streaming", "BV157_streaming",
    "BR001_streaming", "BV410_streaming", "BV411_streaming", "BV437_streaming", "BV412_streaming", "BV159_streaming",
    "BV418_streaming", "BV120_streaming", "BV142_streaming", "BV143_streaming", "BV056_streaming", "BV005_streaming",
    "BV064_streaming", "BV051_streaming", "BV063_streaming", "BV417_streaming", "BV050_streaming", "BV061_streaming",
    "BV401_streaming", "BV402_streaming", "BV006_streaming", "BV011_streaming", "BV012_streaming", "BV034_streaming",
    "BV033_streaming", "BV511_streaming", "BV505_streaming", "BV138_streaming", "BV027_streaming", "BV502_streaming",
    "BV503_streaming", "BV504_streaming", "BV421_streaming", "BV702_streaming", "BV506_streaming", "BV040_streaming",
    "BV516_streaming", "BV520_streaming", "BV521_streaming", "BV421_streaming", "BV522_streaming", "BV702_streaming",
    "BV700_streaming", "BV524_streaming", "BV531_streaming", "BV530_streaming", "BV421_streaming", "BV702_streaming",
    "BV700_streaming", "BV065_streaming", "BV421_streaming", "BV702_streaming", "BV700_streaming", "BV421_streaming",
    "BV421_streaming", "BV421_streaming", "BV702_streaming", "BV700_streaming", "BV021_streaming", "BV020_streaming",
    "BV704_streaming", "BV210_streaming", "BV704_streaming", "BV217_streaming", "BV704_streaming", "BV213_streaming",
    "BV704_streaming", "BV025_streaming", "BV227_streaming", "BV704_streaming", "BV026_streaming", "BV424_streaming",
    "BV704_streaming", "BV212_streaming", "BV019_streaming", "BV221_streaming", "BV423_streaming", "BV704_streaming",
    "BV214_streaming", "BV226_streaming", "BV216_streaming", "BV700_V2_streaming", "BV705_streaming",
    "BV701_V2_streaming", "BV700_streaming", "BV701_streaming", "BV001_streaming", "BV406_streaming", "BV123_streaming",
    "BV120_streaming", "BV119_streaming", "BV115_streaming", "BV107_streaming", "BV100_streaming", "BV104_streaming",
    "BV004_streaming", "BV113_streaming", "BV102_streaming", "BV405_streaming", "BV009_streaming", "BV008_streaming",
    "BV064_streaming", "BV437_streaming", "BV511_streaming", "BV040_streaming", "BV138_streaming", "BV704_streaming",
    "BV702_streaming", "BV421_streaming"
    
]
host = "openspeech.bytedance.com"
api_url = f"https://{host}/api/v1/tts"

header = {"Authorization": f"Bearer;{access_token}"}

def synthesize_text(text, output_path, voice_type):
    request_json = {
        "app": {
            "appid": appid,
            "token": access_token,
            "cluster": cluster
        },
        "user": {
            "uid": "388808087185088"
        },
        "audio": {
            "voice": "other",
            "voice_type": voice_type,
            "encoding": "mp3",
            "speed": 10,
            "volume": 10,
            "pitch": 10
        },
        "request": {
            "reqid": str(uuid.uuid4()),
            "text": text,
            "text_type": "plain",
            "operation": "query",
            "with_frontend": 1,
            "frontend_type": "unitTson"
        }
    }

    try:
        print(f"Request Header: {header}")  # 添加调试信息
        print(f"Request JSON: {request_json}")  # 添加调试信息
        resp = requests.post(api_url, json=request_json, headers=header)
        print(f"Response Status Code: {resp.status_code}")  # 添加调试信息
        resp_json = resp.json()
        print(f"Response JSON for text '{text}': {resp_json}")  # 添加调试信息
        if "data" in resp_json:
            data = resp_json["data"]
            with open(output_path, "wb") as file_to_save:
                file_to_save.write(base64.b64decode(data))
                print("save file success")
            print(f"Saved file: {output_path}")
            return True
        else:
            print(f"No data in response for text: {text}")
    except Exception as e:
        print(f"Error: {e}")
    return False

if __name__ == '__main__':
    input_txt_path = r"G:\huoshan\1.txt"  # 指定输入txt文件路径
    output_dir = r"G:\huoshan"  # 指定输出目录

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    synthesized_count = 0

    with open(input_txt_path, "r", encoding="utf-8") as file:
        lines = file.readlines()
        time.sleep(5)
        print(f"Total lines in file: {len(lines)}")  # 添加调试信息
        for i, line in enumerate(lines):
            line = line.strip()
            if line:
                voice_type = random.choice(voice_types)  # 在循环中选择语音参数
                print(f"Synthesizing line {i+1} with voice type {voice_type}: {line}")  # 添加调试信息
                output_path = os.path.join(output_dir, f"fake_en_{voice_type}_20240612_{i+1}.mp3")
                if synthesize_text(line, output_path, voice_type):
                    synthesized_count += 1

    print(f"Total synthesized lines: {synthesized_count}")

### 火山引擎语音合成 Python SDK 使用教程 要在 Python 中使用火山引擎语音合成功能,可以按照以下方法操作: #### 1. 获取 API Key 和 Secret Key 在使用火山引擎语音合成功能之前,需要先登录到火山引擎控制台并获取 `API Key` 和 `Secret Key`。这些密钥用于身份验证和授权访问服务[^1]。 #### 2. 安装依赖库 为了简化 HTTP 请求处理过程,建议安装第三方库 `requests` 来发送请求。如果尚未安装该库,可以通过运行以下命令来完成安装: ```bash pip install requests ``` #### 3. 构建签名机制 火山引擎的服务通常会要求通过 HMAC-SHA256 或其他方式构建签名字符串以确保安全性。以下是创建签名的一个简单示例代码片段: ```python import hmac import base64 from hashlib import sha256 def generate_signature(secret_key, message): key_bytes = secret_key.encode('utf-8') msg_bytes = message.encode('utf-8') signature = hmac.new(key_bytes, msg_bytes, digestmod=sha256).digest() return base64.b64encode(signature).decode('utf-8') ``` 此函数接受两个参数:一个是用户的 `secret_key`;另一个是要加密的消息体(通常是 URL 参数串)。它返回经过编码后的签名值。 #### 4. 调用语音合成接口 下面是一个完整的例子,展示如何利用上述工具向火山引擎发起一次 POST 请求来进行文本转语音的任务: ```python import json import time import uuid import requests class VoiceSynthesisClient: def __init__(self, api_key, secret_key): self.api_key = api_key self.secret_key = secret_key self.url = 'https://open.volcengineapi.com/tts/synthesis' def synthesize(self, text, voice_name='zh-CN-Yunxi'): timestamp = str(int(time.time())) nonce = str(uuid.uuid4()).replace('-', '')[:16] query_string = f'timestamp={timestamp}&nonce={nonce}' headers = { 'Authorization': f'Bearer {generate_signature(self.secret_key, query_string)}', 'Content-Type': 'application/json' } payload = { "text": text, "voice_name": voice_name, "sample_rate": "16000", "format": "wav" } response = requests.post( url=self.url + '?' + query_string, data=json.dumps(payload), headers=headers ) if response.status_code == 200: audio_content = response.content with open(f'{uuid.uuid4()}.wav', 'wb') as file_out: file_out.write(audio_content) print("Audio saved successfully.") else: error_message = response.json().get('message', '') raise Exception(f'Request failed: {error_message}') if __name__ == '__main__': client = VoiceSynthesisClient(api_key="your_api_key", secret_key="your_secret_key") try: client.synthesize(text="你好,这是来自火山引擎的文字转语音测试。") except Exception as e: print(e) ``` 在此脚本中定义了一个类 `VoiceSynthesisClient` ,其中包含了初始化方法以及实际执行 TTS 的逻辑部分。注意替换掉 `"your_api_key"` 和 `"your_secret_key"` 占位符为你自己的凭证信息。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值