Read mp3 tags with php
I was working on music application , so i needed a script that can read the tags from the mp3 file . Tags holds all the necessary information about the mp3 file like the song,artists,year,genre etc
Here is the script
<?php
$mp3 = "1.mp3"; //The mp3 file.
$filesize = filesize($mp3);
$file = fopen("1.mp3", "r");
fseek($file, -128, SEEK_END); // It reads the
$tag = fread($file, 3);
if($tag == "TAG")
{
$data["song"] = trim(fread($file, 30));
$data["artist"] = trim(fread($file, 30));
$data["album"] = trim(fread($file, 30));
$data["year"] = trim(fread($file, 4));
$data["comment"] = trim(fread($file, 30));
$data["genre"] = trim(fread($file, 1));
}
else
die("MP3 file does not have any ID3 tag!");
fclose($file);
while(list($key, $value) = each($data))
{
print("$key: $value<br>\r\n");
}
?>
This code will show data like this
song: If u were a sailboat
artist: Katie Melua
album: Pictures
year: 2007
comment: sumon’s collection
genre: ÿ
But to get the more information about the Genre we can add an array and change the code a bit.
$genre = array("Blues","Classic Rock","Country","Dance","Disco","Funk","Grunge",
"Hip-Hop","Jazz","Metal","New Age","Oldies","Other","Pop","R&B",
"Rap","Reggae","Rock","Techno","Industrial","Alternative","Ska",
"Death Metal","Pranks","Soundtrack","Euro-Techno","Ambient",
"Trip-Hop","Vocal","Jazz+Funk","Fusion","Trance","Classical",
"Instrumental","Acid","House","Game","Sound Clip","Gospel",
"Noise","AlternRock","Bass","Soul","Punk","Space","Meditative",
"Instrumental Pop","Instrumental Rock","Ethnic","Gothic",
"Darkwave","Techno-Industrial","Electronic","Pop-Folk",
"Eurodance","Dream","Southern Rock","Comedy","Cult","Gangsta",
"Top 40","Christian Rap","Pop/Funk","Jungle","Native American",
"Cabaret","New Wave","Psychadelic","Rave","Showtunes","Trailer",
"Lo-Fi","Tribal","Acid Punk","Acid Jazz","Polka","Retro",
"Musical","Rock & Roll","Hard Rock","Folk","Folk-Rock",
"National Folk","Swing","Fast Fusion","Bebob","Latin","Revival",
"Celtic","Bluegrass","Avantgarde","Gothic Rock","Progressive Rock",
"Psychedelic Rock","Symphonic Rock","Slow Rock","Big Band",
"Chorus","Easy Listening","Acoustic","Humour","Speech","Chanson",
"Opera","Chamber Music","Sonata","Symphony","Booty Bass","Primus",
"Porn Groove","Satire","Slow Jam","Club","Tango","Samba",
"Folklore","Ballad","Power Ballad","Rhythmic Soul","Freestyle",
"Duet","Punk Rock","Drum Solo","Acapella","Euro-House","Dance Hall");
//Change the $data['genre'] inside the if tag
$data["genre"] = $genre[ord(trim(fread($file, 1)))];
Nice hun ? Have fun .











What about for a whole folder of mp3′s?
I am trying to get it down but its not quite right. Any ideas?
<?php
$dir = TEMPLATEPATH . "/flash/music/";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($fileread = readdir($dh)) !== false) {
if($fileread == "." || $fileread == ".." || $fileread == "index.php" )
continue;
$fullpath = $dir . $fileread;
$musicfile = fopen($fullpath, 'r');
fseek($musicfile, -128, SEEK_END);
$tag = fread($musicfile, 3);
if($tag == "TAG")
{
$data["song"] = trim(fread($musicfile, 30));
$data["artist"] = trim(fread($musicfile, 30));
$data["album"] = trim(fread($musicfile, 30));
}
else
{
echo "MP3 file does not have any ID3 tag!";
}
fclose($musicfile);
while(list($key, $value) = each($data))
{
print("$key: $value\r\n”);
}
}
}
}
?>
why u need something like ?
u can use C for that .
Hi,
very useful tag reader.. just one question, how to read other tags?!? Like Publisher (TPUB), PIcture (APIC), etc?
Thanks and regards,
I’ll be damned. It works..! Thank you very much, I’m going to torture-test your code, but I’m really amazed that on the first try it spits out all the meta-info… Where did you found the documentation as to where exactly to look in the file for tags (that -128 thing is bits from the start of the file, is it) ?
Austin, you want to use this
what would the end code look like to get what Austin was looking for?
i am getting ????? on ID3 tags with arabic text
any solution ?
thanks