unified_python/tts/tts_文字转语音.py

73 lines
2.6 KiB
Python
Raw Normal View History

2025-08-06 17:26:07 +08:00
# coding=utf-8
import base64
import json
import uuid
import requests
import os
text = ''' 群友们好啊!我是浑元形意太极门掌门人马保国。刚才有个后生问我:马老师,伟哥要我用马保国的语音包生成一段文字,代码写不出来怎么搞?我说年轻人要讲码德!​​耗子尾汁,好好反思!​​ 我这一招【闪电五连鞭】——(甩手噼啪响)
if-elif-elif-elif-else很快啊传统编程讲究化劲儿四两拨千斤你import一堆库有备而来 可try了半天except抓不住异常我大意了啊没有闪 一个return不写左正蹬500报错右鞭腿404崩溃不讲码德 我劝这位年轻人收势摇头好好编程莫搞偷袭'''
# 填写平台申请的appid, access_token以及cluster
appid = "9407991441"
access_token = "VBI4pixTt-GaARTdacAAdQPrHMY333Di"
cluster = "volcano_icl" # 使用您提供的Cluster ID
voice_type = "S_xQVFJrvA1" # 使用您提供的声音ID
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": "388808087185088"
},
"audio": {
"voice_type": voice_type,
"encoding": "mp3",
"speed_ratio": 1.0,
"volume_ratio": 1.0,
"pitch_ratio": 1.0,
},
"request": {
"reqid": str(uuid.uuid4()),
"text": text,
"text_type": "plain",
"operation": "query",
"with_frontend": 1,
"frontend_type": "unitTson"
}
}
if __name__ == '__main__':
try:
resp = requests.post(api_url, json.dumps(request_json), headers=header)
print(f"resp body: \n{resp.json()}")
if "data" in resp.json():
# 创建语音文件夹(如果不存在)
folder_path = "./语音"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# 生成文件名
filename = f"tts_output_{request_json['request']['reqid']}.mp3"
file_path = os.path.join(folder_path, filename)
# 保存音频文件
data = resp.json()["data"]
with open(file_path, "wb") as file_to_save:
file_to_save.write(base64.b64decode(data))
print(f"音频文件已保存至: {file_path}")
except Exception as e:
print(f"发生错误: {e}")
e.with_traceback()