Files
server-configs/edge_tts_script.py
2026-02-13 22:24:27 +08:00

91 lines
2.5 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Edge TTS - 文字转语音
使用 Microsoft Edge 的免费 TTS 服务
"""
import asyncio
from edge_tts import Communicate, list_voices
import os
import sys
from datetime import datetime
# 默认配置
VOICE = "zh-CN-XiaoxiaoNeural"
OUTPUT_DIR = "/root/.openclaw/media/tts"
async def text_to_speech(text, voice=None, rate="+0%", pitch="+0Hz", output_file=None):
"""将文字转换为语音"""
voice = voice or VOICE
if output_file is None:
os.makedirs(OUTPUT_DIR, exist_ok=True)
output_file = os.path.join(OUTPUT_DIR, f"tts_{int(datetime.now().timestamp())}.mp3")
try:
communicate = Communicate(text, voice, rate=rate, pitch=pitch)
await communicate.save(output_file)
return output_file
except Exception as e:
print(f"❌ TTS 生成失败: {e}")
return None
async def main():
args = sys.argv[1:]
if not args or args[0] in ['--help', '-h']:
print("用法:")
print(" python edge_tts_script.py \"文本内容\"")
print(" python edge_tts_script.py \"文本\" --voice zh-CN-XiaoxiaoNeural --rate +10%")
print(" python edge_tts_script.py --list-voices")
print("\n选项:")
print(" --voice, -v 语音名称")
print(" --rate, -r 语速 (+/-百分比)")
print(" --pitch, -p 音调 (+/-Hz)")
print(" --output, -o 输出文件路径")
return
# 解析参数
text = args[0]
options = {
"voice": VOICE,
"rate": "+0%",
"pitch": "+0Hz",
"output_file": None
}
i = 1
while i < len(args):
key = args[i].replace("--", "")
value = args[i + 1] if i + 1 < len(args) else ""
if key == "voice" or key == "v":
options["voice"] = value
elif key == "rate" or key == "r":
options["rate"] = value
elif key == "pitch" or key == "p":
options["pitch"] = value
elif key == "output" or key == "o":
options["output_file"] = value
elif key == "list-voices" or key == "L":
voices = await list_voices()
print(f"\n可用语音 ({len(voices)} 个)")
return
i += 2
# 生成语音
output = await text_to_speech(
text,
voice=options["voice"],
rate=options["rate"],
pitch=options["pitch"],
output_file=options["output_file"]
)
if output:
print(f"✅ 生成成功: {output}")
if __name__ == "__main__":
asyncio.run(main())