Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get duration of audios in ffmpeg in php

I am using PHP-FFMpeg library to work on audio and video files.

I know that to get duration a specific video can like this :

$ffprobe    = \FFMpeg\FFProbe::create();
$duration   = $ffprobe->format('path/to/file')->get('duration');

But I know that how can I do that for a given audio file.

Did anyone know a solution for that ?

like image 605
A.B.Developer Avatar asked Jan 23 '26 23:01

A.B.Developer


1 Answers

It should work exactly the same way for audio as for video.

For example this code:

$ffprobe    = \FFMpeg\FFProbe::create();
$durationMp3   = $ffprobe->format('test.mp3')->get('duration');
$durationFlac   = $ffprobe->format('test.flac')->get('duration');

echo "$durationMp3\n";
echo "$durationFlac\n";

outputs duration in seconds for both mp3 and FLAC formats:

255.477551
255.477551

Is it possible that your audio file is not encoded properly and, therefore, is not recognized by ffmpeg itself?

You could try running a cli command:

ffmpeg -i test.mp3 2>&1 | grep Duration | awk '{print $2}' | tr -d ,

for the same test.mp3 it produces

00:04:15.48

which is the same 255.48 seconds the php code produced

If running ffmpeg directly does not produce anything useful, then the problem is with the audio file itself. You can then either fix the file or test with other (properly encoded) files.

like image 167
paulz Avatar answered Jan 26 '26 15:01

paulz