Posted By

ginoplusio on 02/22/10


Tagged

mp3 id3 tag id3v2


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

ginoplusio
gummiball
NyX


How to read ID3 tag of mp3 files with PHP


 / Published in: PHP
 

URL: http://www.barattalo.it/2010/02/22/reading-mp3-informations-with-php-id3-tags/

Inside mp3 files there are often some usefull informations stored. Those data are called “id3 tags” and deal with Author name, Title and Album name. There are also some other informations sometimes: such as the length of the track, or the track number of the album, and more, this code will help read those files!

  1. <?
  2. // From here: http://www.autistici.org/ermes/index.php?pag=1&post=15
  3. // and fixed here: http://www.barattalo.it
  4. // ------------------------------
  5. // example:
  6. // print_r( tagReader ("myfile.mp3") );
  7. // ------------------------------
  8. function tagReader($file){
  9. $id3v23 = array("TIT2","TALB","TPE1","TRCK","TDRC","TLEN","USLT");
  10. $id3v22 = array("TT2","TAL","TP1","TRK","TYE","TLE","ULT");
  11. $fsize = filesize($file);
  12. $fd = fopen($file,"r");
  13. $tag = fread($fd,$fsize);
  14. $tmp = "";
  15. fclose($fd);
  16. if (substr($tag,0,3) == "ID3") {
  17. $result['FileName'] = $file;
  18. $result['TAG'] = substr($tag,0,3);
  19. $result['Version'] = hexdec(bin2hex(substr($tag,3,1))).".".hexdec(bin2hex(substr($tag,4,1)));
  20. }
  21. if($result['Version'] == "4.0" || $result['Version'] == "3.0"){
  22. for ($i=0;$i<count($id3v23);$i++){
  23. if (strpos($tag,$id3v23[$i].chr(0))!= FALSE){
  24. $pos = strpos($tag, $id3v23[$i].chr(0));
  25. $len = hexdec(bin2hex(substr($tag,($pos+5),3)));
  26. $data = substr($tag, $pos, 9+$len);
  27. for ($a=0;$a<strlen($data);$a++){
  28. $char = substr($data,$a,1);
  29. if($char >= " " && $char <= "~") $tmp.=$char;
  30. }
  31. if(substr($tmp,0,4) == "TIT2") $result['Title'] = substr($tmp,4);
  32. if(substr($tmp,0,4) == "TALB") $result['Album'] = substr($tmp,4);
  33. if(substr($tmp,0,4) == "TPE1") $result['Author'] = substr($tmp,4);
  34. if(substr($tmp,0,4) == "TRCK") $result['Track'] = substr($tmp,4);
  35. if(substr($tmp,0,4) == "TDRC") $result['Year'] = substr($tmp,4);
  36. if(substr($tmp,0,4) == "TLEN") $result['Lenght'] = substr($tmp,4);
  37. if(substr($tmp,0,4) == "USLT") $result['Lyric'] = substr($tmp,7);
  38. $tmp = "";
  39. }
  40. }
  41. }
  42. if($result['Version'] == "2.0"){
  43. for ($i=0;$i<count($id3v22);$i++){
  44. if (strpos($tag,$id3v22[$i].chr(0))!= FALSE){
  45. $pos = strpos($tag, $id3v22[$i].chr(0));
  46. $len = hexdec(bin2hex(substr($tag,($pos+3),3)));
  47. $data = substr($tag, $pos, 6+$len);
  48. for ($a=0;$a<strlen($data);$a++){
  49. $char = substr($data,$a,1);
  50. if($char >= " " && $char <= "~") $tmp.=$char;
  51. }
  52. if(substr($tmp,0,3) == "TT2") $result['Title'] = substr($tmp,3);
  53. if(substr($tmp,0,3) == "TAL") $result['Album'] = substr($tmp,3);
  54. if(substr($tmp,0,3) == "TP1") $result['Author'] = substr($tmp,3);
  55. if(substr($tmp,0,3) == "TRK") $result['Track'] = substr($tmp,3);
  56. if(substr($tmp,0,3) == "TYE") $result['Year'] = substr($tmp,3);
  57. if(substr($tmp,0,3) == "TLE") $result['Lenght'] = substr($tmp,3);
  58. if(substr($tmp,0,3) == "ULT") $result['Lyric'] = substr($tmp,6);
  59. $tmp = "";
  60. }
  61. }
  62. }
  63. return $result;
  64. }
  65. ?>

Report this snippet  

You need to login to post a comment.