QQ Bot 语音消息自动识别 v2.0。自动解码 QQ Silk V3 格式,Whisper medium 模型识别,Gateway 集成,用户确认流程。
自动识别 QQ Bot 收到的语音消息,将 Silk V3 编码的 .amr 文件转换为文字。
bash
bash
修改 gateway.ts,在附件处理逻辑中添加:
typescript
} else if (localPath.endsWith(.amr)) {
const { exec } = await import(node:child_process);
try {
// 1. 去掉第一个字节 (0x02)
const data = fs.readFileSync(localPath);
const fixedPath = localPath + .fixed;
fs.writeFileSync(fixedPath, data.slice(1));
// 2. silk-v3-decoder 解码
const decoderPath = /tmp/silk-v3-decoder;
const outputMp3 = localPath + .mp3;
await new Promise
exec(bash ${decoderPath}/converter.sh ${fixedPath} mp3, (err) => {
if (err) reject(err);
else resolve();
});
});
// 3. Whisper 识别文字
const transcript = await new Promise
exec(whisper --model base --language zh ${outputMp3} --output_dir /tmp, (err) => {
if (err) reject(err);
else {
const txtPath = outputMp3 + .txt;
fs.readFile(txtPath, utf-8, (err, data) => {
if (err) reject(err);
else resolve(data.trim());
});
}
});
});
// 4. 添加到消息内容
if (transcript) {
audioTranscripts.push(\n🎤 语音消息:${transcript}\n);
}
// 5. 清理
fs.unlinkSync(fixedPath);
} catch (err: any) {
log?.error([qqbot] 语音处理失败:${err.message});
}
}
QQ 语音文件 (.amr)
↓
去掉 0x02 字节头
↓
silk-v3-decoder 解码 → MP3
↓
Whisper 识别 → 文字
↓
添加到 QQ Bot 消息
QQ 语音文件结构:
┌────────┬─────────────┬──────────┬──────────┐
│ 0x02 │ #!SILK_V3 │ 参数 (3B) │ 音频数据 │
│ 1 字节 │ 9 字节 │ 3 字节 │ 变长 │
└────────┴─────────────┴──────────┴──────────┘
QQ 在标准 Silk V3 格式前添加了 0x02 字节标记,去掉后才是标准格式。
bash
bash
| 语音时长 | 处理时间 | 备注 |
|---|---|---|
| 10 秒 | 10-15 秒 | base 模型 |
| 30 秒 |
首次运行需要编译 silk-v3-decoder,额外 +30 秒。
qqbot-voice-transcribe/
├── SKILL.md # 本文件
├── scripts/
│ ├── processqqvoice.py # 主处理脚本
│ └── batch_process.py # 批量处理脚本
├── examples/
│ └── gateway-integration.ts # QQ Bot 集成示例
└── README.md # 详细文档
bash
新增功能:
修复问题:
性能提升:
typescript
// 自动判断附件类型
const ext = path.extname(localPath).toLowerCase();
const mimeType = att.content_type?.toLowerCase() || ;
if (ext === .amr || mimeType.includes(amr)) {
// 自动识别流程
}
识别效果:
🎤 语音消息识别结果:今天天气如何
请确认是否正确,我将按此执行
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 qqbot-voice-transcribe-1775878681 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 qqbot-voice-transcribe-1775878681 技能
skillhub install qqbot-voice-transcribe-1775878681
文件大小: 16.48 KB | 发布时间: 2026-4-12 11:09