I tried to send a voice message via the Telegram API, but I encountered a problem that the spectrogram was not displayed correctly.
The Telegram API documentation says:
Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .OGG file encoded with OPUS.
The problem is that the spectrogram is displayed, but does not reflect what is happening in the audio recording, that is, where there is complete silence in the audio recording, the spectrogram shows that there is sound (audacity shows the spectrogram for the same file correctly)
The way I convert the file:
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()
The way I send message:
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)
In this case spectogramm looks like:
but the problem is that in the middle of this recording there is a big gap with silence (which can be seen in audacity, but not in telegram)
If I save the file and view information about it, I see the following:
(.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
Tried various conversion options (opus, libopus), via ffmpeg subprocess, I tried sending via aiogram or directly via telegram API, the result is the same.
