[Bug] QQ语音消息(AMR)因 convert_audio_format 后缀短路导致 STT 失败 (HTTP 400)

Open Beginner friendly
#9,594 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
74/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
python

Research direction

Start in astrbot/core/utils/media_utils.py at convert_audio_format(), especially the output-extension shortcut around line 1130. Reproduce the mismatch using the AMR magic bytes and a .wav filename, then verify that audio sent through the STT path is converted correctly and that genuinely valid WAV files still avoid unnecessary conversion.

Written by the indexing model from the issue text.

Description

area:core bug
问题描述

通过 NapCat(aiocqhttp / OneBot v11)接入时,QQ 语音消息的语音转文字(STT)始终失败,上游 Whisper 兼容接口返回 HTTP 400。

原因

astrbot/core/utils/media_utils.pyconvert_audio_format() 在判断是否需要转换时,仅依据文件名后缀,不校验文件实际内容:

# astrbot/core/utils/media_utils.py:1130
if audio_path.lower().endswith(f".{output_format}"):
    return audio_path   # 仅凭后缀即返回,跳过 ffmpeg 转换

而 NapCat 把 QQ 语音(实际编码为 AMR,magic bytes #!AMR)存为本地文件时统一使用 .wav 后缀。当目标格式为 wav 时后缀匹配成立,函数直接返回原文件、跳过 ffmpeg 转换,AMR 字节流顶着 .wav 名被传给 STT 接口,上游无法识别而返回 400。

如何复现?
  1. Docker 部署 AstrBot(soulter/astrbot:latest),配置任意 OpenAI Whisper 兼容 STT provider(如硅基流动 TeleAI/TeleSpeechASR)
  2. 通过 NapCat 以 OneBot v11 接入(反向 WebSocket)
  3. 用 QQ 向机器人发送一条语音消息
  4. STT 失败,日志报 Speech-to-text failed: Error code: 400
AstrBot 版本

4.27.2

操作系统

Linux

部署方式

Docker (soulter/astrbot:4.27.2)

使用的消息平台适配器

OneBot v11 (NapCat,反向 WebSocket)

错误日志
[14:32:51.137] [Core] [INFO] [core.event_bus:74]: [default] [napcat(aiocqhttp)]  /1378720352: [ComponentType.Record]
[14:32:51.413] [Core] [ERRO] [preprocess_stage.stage:206]: Traceback (most recent call last):
  File "/AstrBot/astrbot/core/pipeline/preprocess_stage/stage.py", line 192, in _stt_record
    result = await stt_provider.get_text(audio_url=path)
  File "/AstrBot/astrbot/core/provider/sources/whisper_api_source.py", line 40, in get_text
    result = await self.client.audio.transcriptions.create(...)
openai.BadRequestError: Error code: 400
[14:32:51.413] [Core] [ERRO] [preprocess_stage.stage:208]: Speech-to-text failed: Error code: 400
辅助信息

stage2_fix_works_clean.log
stage1_bug_repro_clean.log

检查清单
  • 我已在 Issue 列表中搜索过相关问题仍无法解决,或该问题从未被报告过。
  • 我已尝试过禁用所有插件,排除了可能是因插件导致的问题。
  • 我报告的问题与 AstrBot 本体相关,而非在报告某一插件的问题。
  • 我已阅读并同意本项目的贡献者行为准则
  • (可选)我愿意提交 PR 以帮助修复该问题。
Dominant language
Python
Stars
40.7k
Forks
2.9k
Avg merge
1d 5h
Merged PRs (30d)
112

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from AstrBotDevs/AstrBot

All issues in AstrBotDevs/AstrBot

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.