Tweet
submit to reddit
<? mildcoder ?>
Home | About Me | Contact Us
Categories
  • Security
  • Research
  • Classes
  • Downloads
  • Tutorials
  • News
Search
Advertisements

Read MP3 metadata (ID3 tags) using PHP

Recently i had to work on a music related application. In that i had to extract the information TAGs like song, artists, year and genre from the MP3 files. After researching i found a way to read those metadatas. Following example will help you to fetch those details.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php

    $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");

         $mp3 = "1.mp3"; //Select a mp3 file.
      
         $filesize = filesize($mp3);
         $file = fopen($mp3, "r");
     
         fseek($file, -128, SEEK_END); // It reads the mp3 file
          
         $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"] = $genre[ord(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");    
         }
 ?>

The above piece of code will get the information like this:

  • song: Somebody’s Me
  • artist: Enrique Iglesias
  • album: Insomniac
  • year: 2007
  • comment: Salsan’s Collections
  • genre: Blues