Cruft - (occasionally kruft) is jargon for computer software, computer hardware or internet information of poor quality. The term originates from software code that is rewritten leaving irrelevant or unwanted data within the code.

Reading ID3 tags with PHP (kludge)

Posted: April 8th, 2011 | Author: | Filed under: php | Tags: , | 2 Comments »

The other day a co-worker faced the issue of determining the total duration of multiple mp3 and mp4 files spread across an arbitrary number of directories on the file server. Now we could’ve opened a group of mp3s into Media Player or the like and determined the total duration of that playlist but I couldn’t quickly think of a way to do this for mp4 and right clicking for the file properties was out of the question. Given the fact that this task would become more regular than just a one-time gig I decided to set out and create a script to automate this process. Since I am in bed with PHP I figured I’d go ahead and write this script using just that and run from command prompt.

You will need to download the terrific getID3 class from, this will enable you to perform the necessary file functions required to extract ID3 tags. Once you have downloaded you will need to extract to your working directory from which we will be running our script. By default it will be in a getID3 folder so we will use this for our example.

Ok so let’s make some kludge, open up Notepad or whichever IDE you are comfortable with and start this schniz.

Include ID3 and create instance of ID3

//Require our newly downloaded getid3, main file is getid3.php.
//Under the Linux Environment you would use forward slashes,
//e.g. require_once('/getid3/getid3.php');
//Create instance of ID3
$getID3 = new getID3;

Since I am running this script from the command prompt I am going to ask the user to input the directory they wish to search.

Prompt user for directory they wish to search

//Prompt user for directory location and store in $directoryToSearch
echo "Input the directory to search (C:\\\BLAH\\\FOO):";
$directoryToSearch = trim(fgets(STDIN));

We will declare a variable that will contain our desired file type, this will be used in a check later on in the script. Also we will declare a variable to hold our total duration.

File type and Time variable declaration

//Declare file type we wish to find
$fileType = "mp3";
$totalTime = 0;

We will read through the inputted directory and grab each file one by one. Once we grab a file we will utilize the analyze function of ID3

Traverse Directory and analyze file if found, placing tags into array

//Check to make sure directory is valid
//Directory is valid, open it.
$dir = opendir($directoryToSearch);
//Loop through each file in the directory
while (($file = readdir($dir)) !== false) {
     if (is_file($fileName)) {
          $fileName = realpath($directoryToSearch.'/'.$file);
          //Put tag information into an array for output

There are many different tags you can retrieve using ID3, I am just interested in the file type and the total duration so we will use $array['playtime_seconds']. For more information on what information is available review the structure.txt file or view them here.

Here will double check that this file is our desired file format and if so accumulate the duration.

Check for file type match and accumulate time

//Check to see if the file matches types declared above
//if so accumulate total.
if($currentFile['fileformat'] == $fileType){
    $totalTime = $totalTime + $currentFile['playtime_seconds'];

Now remember this script was made to traverse the Windows Environment; so if you are compiling on Linux make sure you correct your slashes. Also the $directoryToSearch is input at the console, you may wish to change this to a static variable or maybe even a form field if you are migrating this to a web application.

In the real world version of this script I included file operations to output information to a tab delimited file for use in Excel. You may want to add some of this functionality to your script as well or maybe even store to a DBMS of your choice.