K
K
kirigosh2021-08-27 20:33:40
PHP
kirigosh, 2021-08-27 20:33:40

How to speed up ogg to mp3 conversion via ffmpeg?

There is a telegram bot to which you send a voice message (oga format), and it sends you an mp3 file in response. I decided to implement the same thing: I installed ffmpeg on the virtual machine, fed the voice to the bot and got mp3. Everything worked out, but I was confused by the speed of the script. In my case, the script was executed in 3-4 seconds, and in the case of someone else's bot, it was less than a second (very fast, it seems that somewhere around 300ms). I contacted the developer of that bot and he said that the bot was written in Python (I have PHP) and most importantly, that the conversion was done asynchronously, since it was the slowest part of the script. I took measurements of my bot and got this picture:
(important moment microtime was multiplied by 1000, that is, 1860 is 1.86 seconds)

Результат таймера
file_path - 312.7338886261
get link - 0.0030994415283203
ffmpeg - 1860.6932163239
send file - 916.67604446411
global - 3090.4860496521

I have the slowest moment - also converting the file.

Question: is it possible somehow to run ffmpeg asynchronously?

Bot script

1. я не заморачивался с названием и просто брал текущую метку времени для его имени.
2. я закомментировал строки с замерами, чтобы не отвлекали
$tga = json_decode(file_get_contents('php://input'), true);
function tlgrm($method, $data, $client = 'получатель') {
  $data["parse_mode"] = "html";
  $data["chat_id"] = $client;
  $ch = curl_init("https://api.telegram.org/апи бота/$method");
  curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => 1,CURLOPT_CUSTOMREQUEST => "POST",CURLOPT_POSTFIELDS => $data]);
  $r = curl_exec($ch);
  curl_close($ch);
  return json_decode($r, true);
}

function tgfile($path) {
  return "https://api.telegram.org/file/апи бота/$path";
}
$time = time();
$sec = "Результат таймера\n";


// ПОЛУЧЕНИЕ #FILE_PATH
# $t = microtime(1);
$path = tlgrm('getFile',['file_id' => $tga['message']['voice']['file_id']]);
# $nt = (microtime(1) - $t)*1000;
# $sec.= "file_path - $nt\n";

// ПОЛУЧЕНИЕ ССЫЛКИ НА ФАЙЛ #GET LINK
# $t = microtime(1);
$link = tgfile($path['result']['file_path']);
# $nt = (microtime(1) - $t)*1000;
# $sec.= "get link - $nt\n";

// SHELL EXEC #FFMPEG
# $t = microtime(1);
shell_exec("sudo ffmpeg -i $link tmp/$time.mp3");
# $nt = (microtime(1) - $t)*1000;
# $sec.= "ffmpeg - $nt\n";

// ОТПРАВКА ФАЙЛА #SEND FILE
# $t = microtime(1);
$data['audio'] = "https://mywebsite.com/tmp/$time.mp3"; // telegram отказался принимать относительную ссылку 
tlgrm('sendAudio', $data);
# $nt = (microtime(1) - $t)*1000;
# $sec.= "send file - $nt\n";

// ОТПРАВКА ОТЧЁТА
$gt = (microtime(1) - $gt)*1000;
$sec.= "global - $gt";
tlgrm('sendMessage', $sec);


ps I, to be honest, was confused by the programmer's answer when he said that it was a matter of asynchrony. Because this action is linear: receiving a file, converting, sending. You cannot convert a file before you have received it. But most likely it's the initialization of ffmpeg. If it was already "charged", then perhaps the speed would be higher.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Kokoulin, 2021-08-28
@Kokoulin

Most likely you run into server performance (my conversion via ffmpeg is quite briskly performed)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question