module.tag.id3v1.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. /////////////////////////////////////////////////////////////////
  7. // See readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.tag.id3v1.php //
  11. // module for analyzing ID3v1 tags //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_id3v1
  16. {
  17. function getid3_id3v1(&$fd, &$ThisFileInfo) {
  18. if ($ThisFileInfo['filesize'] >= pow(2, 31)) {
  19. $ThisFileInfo['warning'][] = 'Unable to check for ID3v1 because file is larger than 2GB';
  20. return false;
  21. }
  22. fseek($fd, -256, SEEK_END);
  23. $preid3v1 = fread($fd, 128);
  24. $id3v1tag = fread($fd, 128);
  25. if (substr($id3v1tag, 0, 3) == 'TAG') {
  26. $ThisFileInfo['avdataend'] = $ThisFileInfo['filesize'] - 128;
  27. $ParsedID3v1['title'] = $this->cutfield(substr($id3v1tag, 3, 30));
  28. $ParsedID3v1['artist'] = $this->cutfield(substr($id3v1tag, 33, 30));
  29. $ParsedID3v1['album'] = $this->cutfield(substr($id3v1tag, 63, 30));
  30. $ParsedID3v1['year'] = $this->cutfield(substr($id3v1tag, 93, 4));
  31. $ParsedID3v1['comment'] = substr($id3v1tag, 97, 30); // can't remove nulls yet, track detection depends on them
  32. $ParsedID3v1['genreid'] = ord(substr($id3v1tag, 127, 1));
  33. // If second-last byte of comment field is null and last byte of comment field is non-null
  34. // then this is ID3v1.1 and the comment field is 28 bytes long and the 30th byte is the track number
  35. if (($id3v1tag{125} === "\x00") && ($id3v1tag{126} !== "\x00")) {
  36. $ParsedID3v1['track'] = ord(substr($ParsedID3v1['comment'], 29, 1));
  37. $ParsedID3v1['comment'] = substr($ParsedID3v1['comment'], 0, 28);
  38. }
  39. $ParsedID3v1['comment'] = $this->cutfield($ParsedID3v1['comment']);
  40. $ParsedID3v1['genre'] = $this->LookupGenreName($ParsedID3v1['genreid']);
  41. if (!empty($ParsedID3v1['genre'])) {
  42. unset($ParsedID3v1['genreid']);
  43. }
  44. if (empty($ParsedID3v1['genre']) || (@$ParsedID3v1['genre'] == 'Unknown')) {
  45. unset($ParsedID3v1['genre']);
  46. }
  47. foreach ($ParsedID3v1 as $key => $value) {
  48. $ParsedID3v1['comments'][$key][0] = $value;
  49. }
  50. // ID3v1 data is supposed to be padded with NULL characters, but some taggers pad with spaces
  51. $GoodFormatID3v1tag = $this->GenerateID3v1Tag(
  52. $ParsedID3v1['title'],
  53. $ParsedID3v1['artist'],
  54. $ParsedID3v1['album'],
  55. $ParsedID3v1['year'],
  56. (isset($ParsedID3v1['genre']) ? $this->LookupGenreID($ParsedID3v1['genre']) : false),
  57. $ParsedID3v1['comment'],
  58. @$ParsedID3v1['track']);
  59. $ParsedID3v1['padding_valid'] = true;
  60. if ($id3v1tag !== $GoodFormatID3v1tag) {
  61. $ParsedID3v1['padding_valid'] = false;
  62. $ThisFileInfo['warning'][] = 'Some ID3v1 fields do not use NULL characters for padding';
  63. }
  64. $ParsedID3v1['tag_offset_end'] = $ThisFileInfo['filesize'];
  65. $ParsedID3v1['tag_offset_start'] = $ParsedID3v1['tag_offset_end'] - 128;
  66. $ThisFileInfo['id3v1'] = $ParsedID3v1;
  67. }
  68. if (substr($preid3v1, 0, 3) == 'TAG') {
  69. // The way iTunes handles tags is, well, brain-damaged.
  70. // It completely ignores v1 if ID3v2 is present.
  71. // This goes as far as adding a new v1 tag *even if there already is one*
  72. // A suspected double-ID3v1 tag has been detected, but it could be that
  73. // the "TAG" identifier is a legitimate part of an APE or Lyrics3 tag
  74. if (substr($preid3v1, 96, 8) == 'APETAGEX') {
  75. // an APE tag footer was found before the last ID3v1, assume false "TAG" synch
  76. } elseif (substr($preid3v1, 119, 6) == 'LYRICS') {
  77. // a Lyrics3 tag footer was found before the last ID3v1, assume false "TAG" synch
  78. } else {
  79. // APE and Lyrics3 footers not found - assume double ID3v1
  80. $ThisFileInfo['warning'][] = 'Duplicate ID3v1 tag detected - this has been known to happen with iTunes';
  81. $ThisFileInfo['avdataend'] -= 128;
  82. }
  83. }
  84. return true;
  85. }
  86. function cutfield($str) {
  87. return trim(substr($str, 0, strcspn($str, "\x00")));
  88. }
  89. function ArrayOfGenres($allowSCMPXextended=false) {
  90. static $GenreLookup = array(
  91. 0 => 'Blues',
  92. 1 => 'Classic Rock',
  93. 2 => 'Country',
  94. 3 => 'Dance',
  95. 4 => 'Disco',
  96. 5 => 'Funk',
  97. 6 => 'Grunge',
  98. 7 => 'Hip-Hop',
  99. 8 => 'Jazz',
  100. 9 => 'Metal',
  101. 10 => 'New Age',
  102. 11 => 'Oldies',
  103. 12 => 'Other',
  104. 13 => 'Pop',
  105. 14 => 'R&B',
  106. 15 => 'Rap',
  107. 16 => 'Reggae',
  108. 17 => 'Rock',
  109. 18 => 'Techno',
  110. 19 => 'Industrial',
  111. 20 => 'Alternative',
  112. 21 => 'Ska',
  113. 22 => 'Death Metal',
  114. 23 => 'Pranks',
  115. 24 => 'Soundtrack',
  116. 25 => 'Euro-Techno',
  117. 26 => 'Ambient',
  118. 27 => 'Trip-Hop',
  119. 28 => 'Vocal',
  120. 29 => 'Jazz+Funk',
  121. 30 => 'Fusion',
  122. 31 => 'Trance',
  123. 32 => 'Classical',
  124. 33 => 'Instrumental',
  125. 34 => 'Acid',
  126. 35 => 'House',
  127. 36 => 'Game',
  128. 37 => 'Sound Clip',
  129. 38 => 'Gospel',
  130. 39 => 'Noise',
  131. 40 => 'Alt. Rock',
  132. 41 => 'Bass',
  133. 42 => 'Soul',
  134. 43 => 'Punk',
  135. 44 => 'Space',
  136. 45 => 'Meditative',
  137. 46 => 'Instrumental Pop',
  138. 47 => 'Instrumental Rock',
  139. 48 => 'Ethnic',
  140. 49 => 'Gothic',
  141. 50 => 'Darkwave',
  142. 51 => 'Techno-Industrial',
  143. 52 => 'Electronic',
  144. 53 => 'Pop-Folk',
  145. 54 => 'Eurodance',
  146. 55 => 'Dream',
  147. 56 => 'Southern Rock',
  148. 57 => 'Comedy',
  149. 58 => 'Cult',
  150. 59 => 'Gangsta Rap',
  151. 60 => 'Top 40',
  152. 61 => 'Christian Rap',
  153. 62 => 'Pop/Funk',
  154. 63 => 'Jungle',
  155. 64 => 'Native American',
  156. 65 => 'Cabaret',
  157. 66 => 'New Wave',
  158. 67 => 'Psychedelic',
  159. 68 => 'Rave',
  160. 69 => 'Showtunes',
  161. 70 => 'Trailer',
  162. 71 => 'Lo-Fi',
  163. 72 => 'Tribal',
  164. 73 => 'Acid Punk',
  165. 74 => 'Acid Jazz',
  166. 75 => 'Polka',
  167. 76 => 'Retro',
  168. 77 => 'Musical',
  169. 78 => 'Rock & Roll',
  170. 79 => 'Hard Rock',
  171. 80 => 'Folk',
  172. 81 => 'Folk/Rock',
  173. 82 => 'National Folk',
  174. 83 => 'Swing',
  175. 84 => 'Fast-Fusion',
  176. 85 => 'Bebob',
  177. 86 => 'Latin',
  178. 87 => 'Revival',
  179. 88 => 'Celtic',
  180. 89 => 'Bluegrass',
  181. 90 => 'Avantgarde',
  182. 91 => 'Gothic Rock',
  183. 92 => 'Progressive Rock',
  184. 93 => 'Psychedelic Rock',
  185. 94 => 'Symphonic Rock',
  186. 95 => 'Slow Rock',
  187. 96 => 'Big Band',
  188. 97 => 'Chorus',
  189. 98 => 'Easy Listening',
  190. 99 => 'Acoustic',
  191. 100 => 'Humour',
  192. 101 => 'Speech',
  193. 102 => 'Chanson',
  194. 103 => 'Opera',
  195. 104 => 'Chamber Music',
  196. 105 => 'Sonata',
  197. 106 => 'Symphony',
  198. 107 => 'Booty Bass',
  199. 108 => 'Primus',
  200. 109 => 'Porn Groove',
  201. 110 => 'Satire',
  202. 111 => 'Slow Jam',
  203. 112 => 'Club',
  204. 113 => 'Tango',
  205. 114 => 'Samba',
  206. 115 => 'Folklore',
  207. 116 => 'Ballad',
  208. 117 => 'Power Ballad',
  209. 118 => 'Rhythmic Soul',
  210. 119 => 'Freestyle',
  211. 120 => 'Duet',
  212. 121 => 'Punk Rock',
  213. 122 => 'Drum Solo',
  214. 123 => 'A Cappella',
  215. 124 => 'Euro-House',
  216. 125 => 'Dance Hall',
  217. 126 => 'Goa',
  218. 127 => 'Drum & Bass',
  219. 128 => 'Club-House',
  220. 129 => 'Hardcore',
  221. 130 => 'Terror',
  222. 131 => 'Indie',
  223. 132 => 'BritPop',
  224. 133 => 'Negerpunk',
  225. 134 => 'Polsk Punk',
  226. 135 => 'Beat',
  227. 136 => 'Christian Gangsta Rap',
  228. 137 => 'Heavy Metal',
  229. 138 => 'Black Metal',
  230. 139 => 'Crossover',
  231. 140 => 'Contemporary Christian',
  232. 141 => 'Christian Rock',
  233. 142 => 'Merengue',
  234. 143 => 'Salsa',
  235. 144 => 'Trash Metal',
  236. 145 => 'Anime',
  237. 146 => 'JPop',
  238. 147 => 'Synthpop',
  239. 255 => 'Unknown',
  240. 'CR' => 'Cover',
  241. 'RX' => 'Remix'
  242. );
  243. static $GenreLookupSCMPX = array();
  244. if ($allowSCMPXextended && empty($GenreLookupSCMPX)) {
  245. $GenreLookupSCMPX = $GenreLookup;
  246. // http://www.geocities.co.jp/SiliconValley-Oakland/3664/alittle.html#GenreExtended
  247. // Extended ID3v1 genres invented by SCMPX
  248. // Note that 255 "Japanese Anime" conflicts with standard "Unknown"
  249. $GenreLookupSCMPX[240] = 'Sacred';
  250. $GenreLookupSCMPX[241] = 'Northern Europe';
  251. $GenreLookupSCMPX[242] = 'Irish & Scottish';
  252. $GenreLookupSCMPX[243] = 'Scotland';
  253. $GenreLookupSCMPX[244] = 'Ethnic Europe';
  254. $GenreLookupSCMPX[245] = 'Enka';
  255. $GenreLookupSCMPX[246] = 'Children\'s Song';
  256. $GenreLookupSCMPX[247] = 'Japanese Sky';
  257. $GenreLookupSCMPX[248] = 'Japanese Heavy Rock';
  258. $GenreLookupSCMPX[249] = 'Japanese Doom Rock';
  259. $GenreLookupSCMPX[250] = 'Japanese J-POP';
  260. $GenreLookupSCMPX[251] = 'Japanese Seiyu';
  261. $GenreLookupSCMPX[252] = 'Japanese Ambient Techno';
  262. $GenreLookupSCMPX[253] = 'Japanese Moemoe';
  263. $GenreLookupSCMPX[254] = 'Japanese Tokusatsu';
  264. //$GenreLookupSCMPX[255] = 'Japanese Anime';
  265. }
  266. return ($allowSCMPXextended ? $GenreLookupSCMPX : $GenreLookup);
  267. }
  268. function LookupGenreName($genreid, $allowSCMPXextended=true) {
  269. switch ($genreid) {
  270. case 'RX':
  271. case 'CR':
  272. break;
  273. default:
  274. $genreid = intval($genreid); // to handle 3 or '3' or '03'
  275. break;
  276. }
  277. $GenreLookup = getid3_id3v1::ArrayOfGenres($allowSCMPXextended);
  278. return (isset($GenreLookup[$genreid]) ? $GenreLookup[$genreid] : false);
  279. }
  280. function LookupGenreID($genre, $allowSCMPXextended=false) {
  281. $GenreLookup = getid3_id3v1::ArrayOfGenres($allowSCMPXextended);
  282. $LowerCaseNoSpaceSearchTerm = strtolower(str_replace(' ', '', $genre));
  283. foreach ($GenreLookup as $key => $value) {
  284. foreach ($GenreLookup as $key => $value) {
  285. if (strtolower(str_replace(' ', '', $value)) == $LowerCaseNoSpaceSearchTerm) {
  286. return $key;
  287. }
  288. }
  289. return false;
  290. }
  291. return (isset($GenreLookup[$genreid]) ? $GenreLookup[$genreid] : false);
  292. }
  293. function StandardiseID3v1GenreName($OriginalGenre) {
  294. if (($GenreID = getid3_id3v1::LookupGenreID($OriginalGenre)) !== false) {
  295. return getid3_id3v1::LookupGenreName($GenreID);
  296. }
  297. return $OriginalGenre;
  298. }
  299. function GenerateID3v1Tag($title, $artist, $album, $year, $genreid, $comment, $track='') {
  300. $ID3v1Tag = 'TAG';
  301. $ID3v1Tag .= str_pad(trim(substr($title, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
  302. $ID3v1Tag .= str_pad(trim(substr($artist, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
  303. $ID3v1Tag .= str_pad(trim(substr($album, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
  304. $ID3v1Tag .= str_pad(trim(substr($year, 0, 4)), 4, "\x00", STR_PAD_LEFT);
  305. if (!empty($track) && ($track > 0) && ($track <= 255)) {
  306. $ID3v1Tag .= str_pad(trim(substr($comment, 0, 28)), 28, "\x00", STR_PAD_RIGHT);
  307. $ID3v1Tag .= "\x00";
  308. if (gettype($track) == 'string') {
  309. $track = (int) $track;
  310. }
  311. $ID3v1Tag .= chr($track);
  312. } else {
  313. $ID3v1Tag .= str_pad(trim(substr($comment, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
  314. }
  315. if (($genreid < 0) || ($genreid > 147)) {
  316. $genreid = 255; // 'unknown' genre
  317. }
  318. switch (gettype($genreid)) {
  319. case 'string':
  320. case 'integer':
  321. $ID3v1Tag .= chr(intval($genreid));
  322. break;
  323. default:
  324. $ID3v1Tag .= chr(255); // 'unknown' genre
  325. break;
  326. }
  327. return $ID3v1Tag;
  328. }
  329. }
  330. ?>