I made a Telegram robot, and one of its jobs is to create samples from audio files. Now for most audios that is sent to it, the sample is perfectly fine; something like this:
However, for some audios, the sample looks a bit odd:
As you can see, the waves in this file are not shown! (I can assure you that the voice is not empty)
For creating the sample, I use pydub
(Thanks, James!). Here's the part that I create the sample:
song = AudioSegment.from_mp3('song.mp3')
sliced = song[start*1000:end*1000]
sliced.export('song.ogg', format='ogg', parameters=["-acodec", "libopus"])
And then I send the sample using bot.send_voice
method. Like this:
bot.send_voice(
chat_id=update.message.chat.id,
voice=open('song.ogg', 'rb'),
caption=settings.caption,
parse_mode=ParseMode.MARKDOWN,
timeout=1000
)
The documentation of Telegram Bot API 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 (other formats may be sent as Audio or Document).
That's why in this line of code:
sliced.export('song.ogg', format='ogg', parameters=["-acodec", "libopus"])
I used parameters=["-acodec", "libopus"]
.
Can anyone tell me what I'm doing wrong? Thanks in advance!