Reading MP3 file tags in PHP

I recently had a small task to scan some MP3 files on a server to check whether the files had any metadata in them, and add them if necessary. All MP3’s have a metadata section that allows information such as the title, artist, album, track number etc. to be stored in the MP3 file itself. The metadata is stored in the ID3 format. Of course I could have used a desktop tag editor, but the files where on the server, which frequently kept changing. So a server solution was the only way to go.

For the solution I used the Pear MP3_Id package. There are many PHP libraries around that support manipulation of MP3 metadata, but this one was the one I narrowed down. The following examples show how you can use the package to manipulate mp3’s metadata.

Installing MP3_Id

MP3_Id being a Pear package we will use the Pear installer. Or you can manually download the package from Pear MP3_Id.

pear install MP3_Id-1.2.1

Reading mp3 tags

The following function reads the ID3v1 data from all the mp3 files in the given directory, returning the data as an array.

<?php
 
require_once("MP3/Id.php");
 
function read_mp3_tags($dir)
{
    static $result = array();
    static $i = 0;
 
    $tag_string = "";
 
    $mp3 = &new MP3_Id();
 
    // Tags supported by the MP3_Id class
    $tags = array(
                  "name", "artists", "album",
                  "year", "comment", "track",
                  "genre", "genreno"
                  );
 
 
    // Read the current directory
    $d = dir($dir);
 
    // Loop through all the files in the current directory:
    while (false !== ($file = $d->read()))
    {
        // Skip '.' and '..'
        if (($file == '.') || ($file == '..'))
        {
            continue;
        }
 
        // If this is a directory, then recursively call it
        if (is_dir("{$dir}/{$file}"))
        {
            read_mp3_tags("{$dir}/{$file}");
        }
        else
        {
            // It's a mp3 file so read the tags
            if(strtolower(substr($file, strlen($file) - 3, 3)) == "mp3") 
            {
                $data = $mp3->read("{$dir}/{$file}");
 
                // OOPs, some error occured, just save the filename
                if (PEAR::isError($data))
                { 
                    $result[$i]['filename'] = $file;
                    $result[$i]['directory'] = $dir;
                }
                else
                {
                    $result[$i]['filename'] = $file;
                    $result[$i]['directory'] = $dir;
 
                    // Read all the tags of the particular file
                    foreach($tags as $tag)
                    {
                        $result[$i][$tag] = $mp3->getTag($tag);
                    }
                }
                $i++;
            }
        }
    }
 
    return $result;
}
 
 
// Call the above function on your mp3 directory
$results = read_mp3_tags(PATH_TO_YOUR_MP3_DIRECTORY);
print_r($results);
 
?>

Writing or updating tags in an MP3

After you have read ID3 tags, you may want to change or add some new tag information to the files. The following code for example adds the missing album name to the ID3 metadata.

Before you modify any files, be sure you backup them until you are sure your code works fine.

<?php
 
require_once("MP3/Id.php");
 
$mp3 = &new MP3_Id();
 
// Read the MP3 file
$mp3->read("Tears For Fears - Shout.mp3");
 
// Add the album name to the 'album' tag
$mp3->setTag("album", "Songs from the Big Chair");
 
// This is essential. We need to write back the changes
$mp3->write();
 
?>

If you have duplicate files of the same song, you can also quickly copy tags from one file to the other.

<?php
 
require_once("MP3/Id.php");
 
$mp3_1 = &new MP3_Id();
$mp3_2 = &new MP3_Id();
 
$mp3_1->read("Tears For Fears - Shout.mp3");
$mp3_2->read("Tears For Fears.mp3");
 
// Copy tags from 'Tears For Fears - Shout.mp3' to 'Tears For Fears.mp3'
$mp3_2->copy($mp3_1);
$mp3_2->write();
 
?>

Some other PHP libraries for ID3 manipulation

http://code.google.com/p/php-reader/wiki/ID3v2
http://getid3.sourceforge.net/

Download Source
Downloads : 3452 / File size : 0 B
mp3

3 thoughts on “Reading MP3 file tags in PHP

  1. hi

    i have used this class file but when i am downloading the updated mp3 file it is not reflecting the newly updated values instead it is showing the old values

    ???

    can somebody give me the answer

    thanks

  2. i am using ubuntu 12.04.i have my site hosted at /var/www/some_site/.
    so i am using /var/www/some_site/metadata.php which is ur code given above. But it is not showing anything as the line
    require_once(“MP3/Id.php”);

    is not found… as it is not dre… what changed i have to make to get things work?

Comments are closed.