If you’re like most people, you probably have a bunch of
MP3s scattered around your hard disk. And if you’re like most people, you’ve
probably also wished you had a way to automatically catalog these MP3s and index them by title and
artist for easier lookup.
Well, with PHP
and the PHP Extension and Application Repository (PEAR), this task is
easier than you think. PEAR’s MP3_ID class provides ready-made tools to
read and extract metadata from MP3 files, making it a simple matter to identify
the title, artist and genre of a particular MP3 track. This ability can then be
coupled with PHP’s file manipulation capabilities to
efficiently (and automatically) build an index of all your music.
The first step is, naturally, to download and install the
PEAR MP3_ID package. This
package, currently maintained by Alexander Werz,
offers methods for reading and writing MP3 metadata using the ID3v1 standard.
You’ll be using these methods shortly to extract title and artist information
from your MP3 files.
You can install the package manually, by
downloading it from and extracting its contents to your PEAR base directory, or
by using the PEAR installer.
Next, create the following PHP script (Listing A) and save it under your Web server’s document root. Note: The script in Listing A is available in an easy to copy and paste from in the download
version of this article.
Listing A
<html>
<head></head>
<body>
<?php
function searchMp3($dir) {
// declare global variables
global $data, $id3;
// open handle
$dh = opendir($dir) or die (“Cannot open directory ‘$dir’!”);
// look for MP3s
while (($file = readdir($dh)) !== false) {
if ($file != “.” && $file != “..”) {
// if sub-directory: recursively process it
// if MP3 file: get descriptive info
if (is_dir($dir.”/”.$file)) {
searchMp3($dir.”/”.$file);
} else {
if (eregi(“.mp3$”, $file)) {
$id3->read($dir.”/”.$file);
$filename = $id3->getTag(‘file’); // file name
$artist = $id3->getTag(‘artists’) ? $id3->getTag(‘artists’) : “n/a”; // artist name
$title = $id3->getTag(‘name’) ? $id3->getTag(‘name’) : “n/a”; // track title
$genre = $id3->getTag(‘genre’) ? $id3->getTag(‘genre’) : “n/a”; // genre
$year = $id3->getTag(‘year’) ? $id3->getTag(‘year’) : “n/a”; // year of release
$data[] = array(‘file’ => $filename, ‘artist’ => $artist, ‘title’ => $title, ‘genre’ => $genre, ‘year’ => $year);
}
}
}
}
// close handle
closedir($dh);
}
// import MP3 class
include(“MP3/Id.php”);
// create new MP3_ID() class
$id3 = &new MP3_Id();
// define MP3 directory
$dir = “/usr/local/share”;
// start scanning for MP3s
// store extracted MP3 metadata in $data
searchMp3($dir);
?>
<table border=1>
<tr>
<td><b>File name</b></td>
<td><b>Title</b></td>
<td><b>Artist</b></td>
<td><b>Genre</b></td>
<td><b>Year</b></td>
</tr>
<?php
// print track data
foreach ($data as $d) {
echo “<tr>”;
echo “<td>” . $d[‘file’] . “</td>”;
echo “<td>” . $d[‘title’] . “</td>”;
echo “<td>” . $d[‘artist’] . “</td>”;
echo “<td>” . $d[‘genre’] . “</td>”;
echo “<td>” . $d[‘year’] . “</td>”;
echo “</tr>”;
}
?>
</table>
</body>
</html>
Break it down
This script is somewhat complex, so let’s look at it how it
works stage-by-stage:
-
Stage 1: The script
begins by importing the MP3_ID class file, and initializing an object of the
class. It also defines a base directory from which the script should start
searching for MP3 files (you should alter this to reflect your system’s file
paths). It then calls the searchMp3() function, and
passes it the base directory path as input. -
Stage 2: The searchMp3() function forms the core of this
script. It uses PHP’s directory functions to
recursively scan the supplied directory (and all its sub-directories) for files
ending with the .mp3 extension. Each time such a file is found, the MP3_ID class
is used to extract the title, artist, genre and track metadata via its getTag() method. This information is then
stored in the global $data array. Notice
that if a particular metadata element is not available, the script sets the
corresponding array element to “n/a”. -
Stage 3: Once all
sub-directories have been searched and no further recursion is possible, the searchMp3() function exits and returns control to
the main program. It is now a simple matter to iterate over the $data array to extract the stored metadata
and display it in tabular HTML format. That’s exactly what the latter half of
the script in Listing A
does. Its output is a table containing five columns: the full path to the MP3
file, the track title, the artist or band name, the music genre, and the year
of release. You can now save this listing to disk, or print it out for easy
reference.
And there you have it—an automatically generated MP3
catalog! As you add new music files to your collection, simply re-run the
script above and they will automatically show up in the catalog listing. Isn’t
that neat?