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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With