File: mp3_id3.php3

Recommend this page to a friend!
  Classes of marty sejba  >  Mp3 & ID3 tag parser  >  mp3_id3.php3  >  Download  
File: mp3_id3.php3
Role: ???
Content type: text/plain
Description: Get header info and ID3 tag form Mp3 file.
Class: Mp3 & ID3 tag parser
Author: By
Last change:
Date: 19 years ago
Size: 8,304 bytes


Class file image Download

// This function parse ID3 tag from MP3 file. It's quite fast.
// syntax mp3_id(filename)
// function will return -1 if file not exists or no frame cynch found at the beginning of file. i realized that some songs downloaded thru gnutella have about four lines of text info at the beginning. it seepms players can handle. so i will implement it later.
// variable bitrates are not yet implemented, as they are quite slow to check. you can find them to read lot of first frames and check their bitrates. If theyre not the same, its variable bitrate. and also you then cannot compute real song lenght, unless you will scan the whole file for frames and compute its lenght... (at least what i read)
// there is second version of ID3 tag which is tagged at the beginning of the file and its quite large. you can learnt more about at i dont finding this so interesting. there are too good things on new version: you can write more than 30 chars at field and the tag is on the beginning of file. there are so many fields in v2 that i found really unusefull in many case. while it seems that id3v2 will still write tag v1 at the end, i can see no reason why to implement it, cos it is really 'slow' to parse all these informations.

// You can use 'genres' to determine what means the 'genreid' number. if you think you will not need it, delete it to. And also we need to specify all variables for mp3 header.

 $genres=Array("Not Set","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","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");
 $version=Array("00"=>2.5, "10"=>2, "11"=>1);
 $layer  =Array("01"=>3, "10"=>2, "11"=>1);
 $crc=Array("Yes", "No");
 $mode=Array("00"=>"Stereo","01"=>"Joint stereo","10"=>"Dual channel","11"=>"Mono");

// here goes the function

 function mp3_id($file) {
   global $version, $layer, $crc, $bitrate, $bitindex, $freq, $mode, $copy, $genres
   if(!$f=@fopen($file, "r")) { return -1; break; } else {

// read first 4 bytes from file and determine if it is wave file if so, header begins five bytes after word 'data'

     if($tmp=="RIFF") {
       fseek($f, 0);
       $x=StrPos($tmp, "data");
       fseek($f, $x+8);

// now convert those four bytes to BIN. maybe it can be faster and easier. dunno how yet. help?

     for($y=0;$y<4;$y++) {
       for($i=0;$i<(8-StrLen($x));$i++) {$x.="0";}

// every mp3 framesynch begins with eleven ones, lets look for it. if not found continue looking for some 1024 bytes (you can search multiple for it or you can disable this, it will speed up and not many mp3 are like this. anyways its not standart)

//     if(substr($bajt,1,11)!="11111111111") {
//        return -1;
//        break;
//     }
     if(substr($bajt,1,11)!="11111111111") {
       fseek($f, 4);       
           if(ord($tmp[$i])==255 && substr(decbin(ord($tmp[$i+1])),0,3)=="111") {
              $tmp=substr($tmp, $i,4);
              for($y=0;$y<4;$y++) {
                for($i=0;$i<(8-StrLen($x));$i++) {$x.="0";}
     if($bajt=="") {
        return -1;

// now parse all the info from frame header


// lets count lenght of the song

     if($idtag["layer"]==1) {
       $fsize=(12*($idtag["bitrate"]*1000)/$idtag["frequency"]+$idtag["padding"])*4; }
     else {

// now lets see at the end of the file for id3 tag. if exists then  parse it. if file doesnt have an id 3 tag if will return -1 in field 'tag' and if title is empty it returns file name.

     if(!$len) $len=filesize($file);
     fseek($f, $len-128);
     $tag = fread($f, 128);
     if(Substr($tag,0,3)=="TAG") {
     } else {

// close opened file and return results.

   if(!$idtag["title"]) {
     $idtag["title"]=Str_replace("\\","/", $file);
     $idtag["title"]=substr($idtag["title"],strrpos($idtag["title"],"/")+1, 255);
   return $idtag;

//-------- Function ends HERE

// now some example

 function tisk($id) {
   if($id==-1) { echo "File not exists or invalid header.<br>";} else {
     echo "<Table border=1 celpadding=1 cellspacing=1>";
     while (list ($key, $val) = each ($id)) {
        echo "<tr><td><font size=1 face=tahoma>$key </td><td><font size=1 face=tahoma>$val</td></tr>";
     echo "</table>";
     if($id==0) echo "File doesn't have an ID3 tag.";

 while ($file = readdir($handle)) {
 if(strstr($file,".mp"))  {
echo $file."<br>";
 } }

echo "<br>".(time()-$start);
// author:


For more information send a message to info at phpclasses dot org.