how convert ogg file to telegram voice format?
Asked Answered
D

4

18

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

Diskin answered 18/6, 2017 at 14:27 Comment(3)
how did you create your ogg?Stannwood
Also is conversion to another format (like mp3) acceptable?Stannwood
ogg file created by online convertor.yes another format is accepted by telegramDiskin
H
5

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

Holism answered 19/6, 2017 at 10:15 Comment(0)
W
14

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.

Wop answered 24/11, 2019 at 17:22 Comment(2)
Until I started to convert ogg vorbis to opus I was getting weird results. Sometimes telegram was showing audio files as voice messages, other times as a audio files you need to download and play in external player.Detumescence
This works but only if uploaded from Android, if uploaded from PC it doesn't have the bars. BTW: no need for Python, I called ffmpeg directly from commandline… ffmpeg -i input.mp3 -acodec libopus output.ogg -yMariannmarianna
H
5

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

Holism answered 19/6, 2017 at 10:15 Comment(0)
C
5

Telegram has two APIs for sending Audio files: sendVoice and sendAudio

To have spectrogram, you need to use the sendVoice API.

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 (other formats may be sent as Audio or Document). On success, the sent Message is returned. Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.

Also, note the other limitations thats mentioned in the docs:

To use sendVoice, the file must have the type audio/ogg and be no more than 1MB in size. 1-20MB voice notes will be sent as files.

Therefore, if you send a ogg file in opus format that is less than 1MB in size, you will get spectogram. If the ogg file is larger than 1MB, it will be still uploaded as a voice however with no spectogram bars.

You can use ffmpeg in linux with a low bitrate to reduce the size of your file:

ffmpeg -i input.mp3 -vn -acodec libopus -b:a 16k audio.ogg

Here is a sample python code that uses to send a sample ogg file to a channel using sendVoice

import os, sys
import requests

# Bot token
token = ''

if len(sys.argv) <=1:
    print("Usage: send_voice.py destchannelid filename")
    print("\te.g.")
    print("\tsend_voice.py -10011111111 audio.ogg")
    print("Convert audio files to ogg using the following command")
    print("ffmpeg -i input.mp3 -vn -acodec libopus -b:a 16k audio.ogg")
    exit()

upload_audio_url = "https://api.telegram.org/bot{}/sendVoice?chat_id={}".format(token,chat_id)
audio_path_ogg = os.getcwd() + "/" + sys.argv[2]

with open(audio_path_ogg, 'rb') as f:
    data = f.read()

file = {'voice': ('Message.ogg', data)}
result = requests.post(upload_audio_url, files=file)
print(result.content)
Citric answered 13/3, 2022 at 12:47 Comment(0)
C
1

if you want it to display the audio spectrogram, make sure the ogg is encoded with opus codec

Crinkly answered 16/10, 2018 at 12:53 Comment(1)
I was wondering how to achieve the spectrogram. See my answer.Wop

© 2022 - 2024 — McMap. All rights reserved.