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.
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".
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); ...
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.
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;
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