火山引擎语音合成

试用额度

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

额度并发有效期
语音合成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}")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值