You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Telegram API sendVoice方法时频谱图显示异常求助

Telegram API发送语音消息频谱图显示异常问题

尝试通过Telegram API发送语音消息时,遇到频谱图显示不正确的问题:音频中的完全静音段,在Telegram的频谱图中却显示有声音,但同一文件在Audacity中的频谱图显示正常。

Telegram API文档要求

使用此方法发送音频文件,若希望Telegram客户端将其显示为可播放的语音消息,音频必须为OPUS编码的.OGG文件。

文件转换代码

audio_segment = AudioSegment.from_file(io.BytesIO(audio_data), format="wav")
ogg_data_stream = io.BytesIO()
audio_segment.export(ogg_data_stream, format="ogg", codec="opus", bitrate="48k", parameters=['-strict', '-2'])
ogg_data = ogg_data_stream.getvalue()

消息发送代码

audio_stream = BytesIO(ogg_data)
audio_stream.name = "voice.ogg"
url = f"https://api.telegram.org/bot{Config.BOT_TOKEN}/sendVoice"
files = {'voice': ('voice.ogg', audio_stream, 'audio/ogg')}
data = {'chat_id': message.chat.id, 'duration': int(0.5 * len(audio_data) / 22050)}
response = requests.post(url, data=data, files=files)

频谱图示例

频谱图示例

音频文件信息

(.venv) sudden@MacBook-Pro project % mediainfo bot/voice.ogg
General
Complete name                            : bot/voice.ogg
Format                                   : Ogg
File size                                : 3.85 KiB
Duration                                 : 618 ms
Overall bit rate                         : 51.0 kb/s
Writing application                      : Lavc60.31.102 opus

Audio
ID                                       : 2244360202 (0x85C6380A)
Format                                   : Opus
Duration                                 : 618 ms
Channel(s)                               : 1 channel
Channel layout                           : M
Sampling rate                            : 48.0 kHz
Compression mode                         : Lossy
Writing library                          : Lavf60.16.100


(.venv) sudden@MacBook-Pro project % file --mime-type -b bot/voice.ogg
audio/ogg

已尝试的解决方向:

  • 更换转换编码选项(opus、libopus)
  • 通过ffmpeg子进程直接转换音频
  • 使用aiogram库或直接调用Telegram API发送

以上尝试均未解决问题,频谱图显示异常的情况一致。

内容的提问来源于stack exchange,提问作者SuddenDumb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 20:51:11