how convert ogg file to telegram voice format?
if you want it to display the audio spectrogram, make sure the ogg is encoded with opus codec
Thanks to YoilyL I'm able to send voice messages with the spectrogram.
Here's my python script which converts my .wav
file to .ogg
:
import os
import requests
import subprocess
token = YYYYYYY
chat_id = XXXXXXXX
upload_audio_url = "https://api.telegram.org/bot%s/sendAudio?chat_id=%s" % (token, chat_id)
audio_path_wav = '/Users/me/some-file.wav'
# Convert the file from wav to ogg
filename = os.path.splitext(audio_path_wav)[0]
audio_path_ogg = filename + '.ogg'
subprocess.run(["ffmpeg", '-i', audio_path_wav, '-acodec', 'libopus', audio_path_ogg, '-y'])
with open(audio_path_ogg, 'rb') as f:
data = f.read()
# An arbitrary .ogg filename has to be present so that the spectogram is shown
file = {'audio': ('Message.ogg', data)}
result = requests.post(upload_audio_url, files=file)
This results in the following rendering of the voice message:
You'll need to install ffmpeg
using the package manager of your choice.
The MIME-Type for the sendVoice method must be set to audio/ogg. Your sample is set to video/ogg.
More infos on the sendVoice method with an url can be found here