From 84d47fc75339e4059a313cfcd0c7fa4c12ae7e49 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 30 Mar 2026 01:33:44 +0800 Subject: [PATCH] fix: update dingtalk_tts.sh with Python hex parsing --- scripts/dingtalk_tts.sh | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/scripts/dingtalk_tts.sh b/scripts/dingtalk_tts.sh index 7cc4275..f2c4f18 100755 --- a/scripts/dingtalk_tts.sh +++ b/scripts/dingtalk_tts.sh @@ -62,8 +62,19 @@ TTS_RESPONSE=$(curl -s -X POST "https://api.minimaxi.com/v1/t2a_v2" \ } }") -HEX_AUDIO=$(echo "$TTS_RESPONSE" | python3 -c "import sys,json; print(json.load(sys.stdin).get('data',{}).get('audio',''))") -AUDIO_LENGTH=$(echo "$TTS_RESPONSE" | python3 -c "import sys,json; print(json.load(sys.stdin).get('extra_info',{}).get('audio_length',0))") +# 用 Python 解析响应(处理特殊字符) +TTS_RESULT=$(python3 << PYEOF +import json +import sys +data = json.loads("""$TTS_RESPONSE""") +hex_audio = data.get('data', {}).get('audio', '') +audio_len = data.get('extra_info', {}).get('audio_length', 0) +print(f"{hex_audio}\n{audio_len}") +PYEOF +) + +HEX_AUDIO=$(echo "$TTS_RESULT" | head -1) +AUDIO_LENGTH=$(echo "$TTS_RESULT" | tail -1) if [ -z "$HEX_AUDIO" ]; then echo "❌ TTS 调用失败: $TTS_RESPONSE" @@ -76,11 +87,11 @@ echo "[3/6] 转换格式..." echo "$HEX_AUDIO" | xxd -r -p > "$TMP_DIR/voice.mp3" # 获取时长(秒) -DURATION_SEC=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$TMP_DIR/voice.mp3" 2>/dev/null | awk '{printf "%.0f", $1}') -if [ -z "$DURATION_SEC" ] || [ "$DURATION_SEC" = "0" ]; then - DURATION_SEC=$(echo "scale=0; $AUDIO_LENGTH / 1000" | bc) +DURATION=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$TMP_DIR/voice.mp3" 2>/dev/null | awk '{printf "%.0f", $1}') +if [ -z "$DURATION" ] || [ "$DURATION" = "0" ]; then + DURATION=$(echo "scale=0; $AUDIO_LENGTH / 1000" | bc) fi -echo "✓ MP3 生成完成,时长: ${DURATION_SEC}秒" +echo "✓ MP3 生成完成,时长: ${DURATION}秒" # Step 4: 上传 MP3 到钉钉 echo "[4/6] 上传音频到钉钉..." @@ -103,7 +114,7 @@ SEND_RESPONSE=$(curl -s -X POST "https://api.dingtalk.com/v1.0/robot/oToMessages \"robotCode\": \"$DINGTALK_APP_KEY\", \"userIds\": [\"$USER_ID\"], \"msgKey\": \"sampleAudio\", - \"msgParam\": \"{\\\"mediaId\\\":\\\"$MEDIA_ID\\\",\\\"duration\\\":\\\"$DURATION_SEC\\\"}\" + \"msgParam\": \"{\\\"mediaId\\\":\\\"$MEDIA_ID\\\",\\\"duration\\\":\\\"$DURATION\\\"}\" }") PROCESS_KEY=$(echo "$SEND_RESPONSE" | python3 -c "import sys,json; print(json.load(sys.stdin).get('processQueryKey',''))")