xiaowei-system/scripts/mimo_tts.sh

62 lines
1.6 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 小米 MiMo TTS 封装脚本 — Hermes command provider 调用
# 输出: Opus格式音频文件飞书语音条兼容
INPUT="$1"
OUTPUT="$2"
VOICE="${3:-冰糖}"
KEY="tp-c374efhzmz9npodjz5wj1cm008lg4czce6v170nqxwemp12v"
API="https://token-plan-cn.xiaomimimo.com/v1/chat/completions"
TEXT=$(cat "$INPUT")
TMPWAV="/tmp/mimo_tts_$$.wav"
TMP_OPUS="/tmp/mimo_tts_$$.opus"
# 调用小米TTS API
RESP=$(curl -s -X POST "$API" \
-H "Authorization: Bearer $KEY" \
-H "Content-Type: application/json" \
-d "$(python3 -c "
import json, sys
payload = {
'model': 'mimo-v2.5-tts',
'messages': [
{'role': 'user', 'content': '请用温柔自然的语气,正常语速朗读'},
{'role': 'assistant', 'content': sys.argv[1]}
],
'audio': {'format': 'wav', 'voice': sys.argv[2]}
}
print(json.dumps(payload, ensure_ascii=False))
" "$TEXT" "$VOICE")")
# 解析base64音频到临时wav文件
echo "$RESP" | python3 -c "
import json, base64, sys
try:
d = json.load(sys.stdin)
audio = d['choices'][0]['message']['audio']['data']
with open('$TMPWAV', 'wb') as f:
f.write(base64.b64decode(audio))
except Exception as e:
print(f'错误: {e}', file=sys.stderr)
sys.exit(1)
"
if [ ! -f "$TMPWAV" ]; then
echo "错误: WAV文件未生成" >&2
exit 1
fi
# 转换为Opus格式
if ! python3 ~/.hermes/scripts/to_opus.py "$TMPWAV" "$TMP_OPUS" 2>&1; then
rm -f "$TMPWAV"
echo "错误: Opus转码失败" >&2
exit 1
fi
# 覆盖Hermes期望的输出路径即使扩展名是.wav内容是opus
mv "$TMP_OPUS" "$OUTPUT"
rm -f "$TMPWAV"
exit 0