1
0

id3-tag-reader.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * dolphin. Collection of useful PHP skeletons.
  4. * Copyright (C) 2009 Johannes 'Banana' Keßler
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  8. *
  9. * You should have received a copy of the
  10. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  11. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  12. */
  13. /**
  14. * read the id3 tags from a mp3 file
  15. * base on : http://www.script-tutorials.com/id3-tags-reader-with-php/
  16. */
  17. class ID3TagsReader {
  18. // variables
  19. var $aTV23 = array( // array of possible sys tags (for last version of ID3)
  20. 'TIT2',
  21. 'TALB',
  22. 'TPE1',
  23. 'TPE2',
  24. 'TRCK',
  25. 'TYER',
  26. 'TLEN',
  27. 'USLT',
  28. 'TPOS',
  29. 'TCON',
  30. 'TENC',
  31. 'TCOP',
  32. 'TPUB',
  33. 'TOPE',
  34. 'WXXX',
  35. 'COMM',
  36. 'TCOM'
  37. );
  38. var $aTV23t = array( // array of titles for sys tags
  39. 'Title',
  40. 'Album',
  41. 'Author',
  42. 'AlbumAuthor',
  43. 'Track',
  44. 'Year',
  45. 'Length', # Lenght
  46. 'Lyric',
  47. 'Desc',
  48. 'Genre',
  49. 'Encoded',
  50. 'Copyright',
  51. 'Publisher',
  52. 'OriginalArtist',
  53. 'URL',
  54. 'Comments',
  55. 'Composer'
  56. );
  57. var $aTV22 = array( // array of possible sys tags (for old version of ID3)
  58. 'TT2',
  59. 'TAL',
  60. 'TP1',
  61. 'TRK',
  62. 'TYE',
  63. 'TLE',
  64. 'ULT'
  65. );
  66. var $aTV22t = array( // array of titles for sys tags
  67. 'Title',
  68. 'Album',
  69. 'Author',
  70. 'Track',
  71. 'Year',
  72. 'Length', # Lenght
  73. 'Lyric'
  74. );
  75. // constructor
  76. function __construct() {
  77. }
  78. // functions
  79. function getTagsInfo($sFilepath) {
  80. // read source file
  81. $iFSize = filesize($sFilepath);
  82. $vFD = fopen($sFilepath,'r');
  83. $sSrc = fread($vFD,$iFSize);
  84. fclose($vFD);
  85. // obtain base info
  86. if (substr($sSrc,0,3) == 'ID3') {
  87. $aInfo['FileName'] = $sFilepath;
  88. $aInfo['Version'] = hexdec(bin2hex(substr($sSrc,3,1))).'.'.hexdec(bin2hex(substr($sSrc,4,1)));
  89. }
  90. // passing through possible tags of idv2 (v3 and v4)
  91. if ($aInfo['Version'] == '4.0' || $aInfo['Version'] == '3.0') {
  92. for ($i = 0; $i < count($this->aTV23); $i++) {
  93. if (strpos($sSrc, $this->aTV23[$i].chr(0)) != FALSE) {
  94. $s = '';
  95. $iPos = strpos($sSrc, $this->aTV23[$i].chr(0));
  96. $iLen = hexdec(bin2hex(substr($sSrc,($iPos + 5),3)));
  97. $data = substr($sSrc, $iPos, 9 + $iLen);
  98. for ($a = 0; $a < strlen($data); $a++) {
  99. $char = substr($data, $a, 1);
  100. if ($char >= ' ' && $char <= '~')
  101. $s .= $char;
  102. }
  103. if (substr($s, 0, 4) == $this->aTV23[$i]) {
  104. $iSL = 4;
  105. if ($this->aTV23[$i] == 'USLT') {
  106. $iSL = 7;
  107. } elseif ($this->aTV23[$i] == 'TALB') {
  108. $iSL = 5;
  109. } elseif ($this->aTV23[$i] == 'TENC') {
  110. $iSL = 6;
  111. }
  112. $aInfo[$this->aTV23t[$i]] = substr($s, $iSL);
  113. }
  114. }
  115. }
  116. }
  117. // passing through possible tags of idv2 (v2)
  118. if($aInfo['Version'] == '2.0') {
  119. for ($i = 0; $i < count($this->aTV22); $i++) {
  120. if (strpos($sSrc, $this->aTV22[$i].chr(0)) != FALSE) {
  121. $s = '';
  122. $iPos = strpos($sSrc, $this->aTV22[$i].chr(0));
  123. $iLen = hexdec(bin2hex(substr($sSrc,($iPos + 3),3)));
  124. $data = substr($sSrc, $iPos, 6 + $iLen);
  125. for ($a = 0; $a < strlen($data); $a++) {
  126. $char = substr($data, $a, 1);
  127. if ($char >= ' ' && $char <= '~')
  128. $s .= $char;
  129. }
  130. if (substr($s, 0, 3) == $this->aTV22[$i]) {
  131. $iSL = 3;
  132. if ($this->aTV22[$i] == 'ULT') {
  133. $iSL = 6;
  134. }
  135. $aInfo[$this->aTV22t[$i]] = substr($s, $iSL);
  136. }
  137. }
  138. }
  139. }
  140. return $aInfo;
  141. }
  142. }
  143. ?>