Get the play time and bitrate of an mp3 in php

By | February 16, 2013

If your php application is processing media files like mp3, then it might need to find details of the mp3 file like its playtime, bitrate, encoder etc. There is a very useful php library called Getid3 that can extract information from mp3 files. Its written in pure php and does not have any dependencies. Needs php 5.0+.

Get it here

http://getid3.sourceforge.net/

Here is a quick code example that shows how to find out the playtime of an mp3.

//Audio File information library
require_once('getid3/getid3.php');

$getID3 = new getID3;

$path = 'sample.mp3';
$mixinfo = $getID3->analyze( $path );

// Optional: copies data from all subarrays of [tags] into [comments] so
// metadata is all available in one location for all tag formats
// metainformation is always available under [tags] even if this is not called
getid3_lib::CopyTagsToComments($mixinfo);

// Output desired information in whatever format you want
// Note: all entries in [comments] or [tags] are arrays of strings
// See structure.txt for information on what information is available where
// or check out the output of /demos/demo.browse.php for a particular file
// to see the full detail of what information is returned where in the array
//echo @$ThisFileInfo['comments_html']['artist'][0]; // artist from any/all available tag formats
//echo @$ThisFileInfo['tags']['id3v2']['title'][0];  // title from ID3v2
$bit_rate = $mixinfo['audio']['bitrate'];           // audio bitrate
$play_time = $mixinfo['playtime_string'];            // playtime in minutes:seconds, formatted string

//print_r($mixinfo);

list($mins , $secs) = explode(':' , $play_time);

if($mins > 60)
{
	$hours = intval($mins / 60);
	$mins = $mins - $hours*60;
}

$play_time = sprintf("%02d:%02d:%02d" , $hours , $mins , $secs);

echo $play_time;
About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

4 Comments

Get the play time and bitrate of an mp3 in php
  1. Zaheer Abbas

    Notice: Undefined index: audio in E:xampphtdocsshaheenwp-contentthemesdamdaarbusiness-manager.php on line 73

    Notice: Undefined index: playtime_string in E:xampphtdocsshaheenwp-contentthemesdamdaarbusiness-manager.php on line 74

    Notice: Undefined offset: 1 in E:xampphtdocsshaheenwp-contentthemesdamdaarbusiness-manager.php on line 78

    Notice: Undefined variable: hours in E:xampphtdocsshaheenwp-contentthemesdamdaarbusiness-manager.php on line 86
    00:00:00

Leave a Reply

Your email address will not be published. Required fields are marked *