Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the length of an audio file php

Tags:

php

audio

How I can get the length of an audio file in php.

If it's too hard to do in php then any other way should work alright.

like image 431
Belgin Fish Avatar asked Jun 18 '10 12:06

Belgin Fish


People also ask

How to get audio file duration in php?

php $mp3file = new MP3File("npr_304314290. mp3");//http://www.npr.org/rss/podcast.php?id=510282 $duration1 = $mp3file->getDurationEstimate();//(faster) for CBR only $duration2 = $mp3file->getDuration();//(slower) for VBR (or CBR) echo "duration: $duration1 seconds".

How to get mp3 duration in php?

php class MP3File { protected $filename; public function __construct($filename) { $this->filename = $filename; } public static function formatTime($duration) //as hh:mm:ss { //return sprintf("%d:%02d", $duration/60, $duration%60); $hours = floor($duration / 3600); $minutes = floor( ($duration - ($hours * 3600)) / 60); ...

How do I find the length of a mp3?

Using Audio Tag. The easiest way to obtain the duration of an audio file is through the embed Audio tag, used to embed sound content in documents. It may contain one or more audio sources, represented using the src attribute or the <source> element nested on the audio tag.


1 Answers

If you're using linux / unix and have ffmpeg installed, just do this:

$time = exec("ffmpeg -i " . escapeshellarg($path) . " 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//");
list($hms, $milli) = explode('.', $time);
list($hours, $minutes, $seconds) = explode(':', $hms);
$total_seconds = ($hours * 3600) + ($minutes * 60) + $seconds;
like image 113
Stephen Fuhry Avatar answered Oct 07 '22 17:10

Stephen Fuhry