Skip to content
March 15, 2010 / ranacse05

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 .

9 Comments

Leave a Comment
  1. Austin / Jul 3 2010 5:08 pm

    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”);
    }
    }
    }
    }

    ?>

    • ranacse05 / Aug 6 2010 9:42 am

      why u need something like ?
      u can use C for that .

  2. Massimo / Oct 5 2010 11:12 am

    Hi,

    very useful tag reader.. just one question, how to read other tags?!? Like Publisher (TPUB), PIcture (APIC), etc?

    Thanks and regards,

  3. xaccrocheur / Nov 29 2010 1:19 pm

    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) ?

  4. xaccrocheur / Nov 29 2010 1:21 pm

    Austin, you want to use this

    • Glen BodingtonGlen / Jan 2 2012 8:03 pm

      what would the end code look like to get what Austin was looking for?

  5. ismail / Jan 26 2012 8:30 pm

    i am getting ????? on ID3 tags with arabic text
    any solution ?
    thanks

  6. muchlisoft / Dec 24 2013 5:32 am

    Nice code, very useful for me , thanks

Trackbacks

  1. Read mp3 tags with… - Electronik – Free dance music mixes mp3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 29 other followers

%d bloggers like this: