Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how convert ogg file to telegram voice format?

I'm trying to send a voice message through SendVoice method in telegram bot, but it sends the voice as a document file (not play).

ogg file by ffmpeg converted to opus encoding.

https://api.telegram.org/bot<token>/sendVoice?chat_id=x&voice=http://music-farsi.ir/ogg/voice.ogg

What is the difference between the my ogg file and telegram voice message?

My ogg file: ogg file

like image 353
M P Avatar asked Jun 18 '17 14:06

M P


1 Answers

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:

screenshot

You'll need to install ffmpeg using the package manager of your choice.

like image 82
Besi Avatar answered Sep 22 '22 06:09

Besi