# -*- coding: utf-8 -*- import base64 import json import uuid import requests # 填写平台申请的appid, access_token以及cluster appid = "9889713181" access_token= "hWJeGQA9M1xfEGkKWoELsY2TD5crV_3g" cluster = "volcano_tts" voice_type = "BV051_streaming" host = "openspeech.bytedance.com" api_url = f"https://{host}/api/v1/tts" header = {"Authorization": f"Bearer;{access_token}"} request_json = { "app": { "appid": appid, "token": "access_token", "cluster": cluster }, "user": { "uid": "takwayai" }, "audio": { "voice_type": voice_type, "encoding": "wav", "speed_ratio": 1.2, "volume_ratio": 1.0, "pitch_ratio": 1.0, }, "request": { "reqid": str(uuid.uuid4()), "text": "", "text_type": "plain", "operation": "query", "with_frontend": 1, "frontend_type": "unitTson" } } def generate_audio(text,count): try: request_json["request"]["text"] = text resp = requests.post(api_url, json.dumps(request_json), headers=header) if "data" in resp.json(): data = resp.json()["data"] file_to_save = open(f"output{str(count)}.wav", "wb") file_to_save.write(base64.b64decode(data)) else: with open('error.txt','a',encoding='utf-8') as ef: ef.write(f"{text}\n") except Exception as e: with open('error.txt','a',encoding='utf-8') as ef: ef.write(f"{text}\n") print(str(e)) with open('text.txt','r',encoding='utf-8') as file: count = 0 while True: line = file.readline() if not line: break generate_audio(line,count) count += 1