getid3.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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. // //
  8. // Please see readme.txt for more information //
  9. // ///
  10. /////////////////////////////////////////////////////////////////
  11. // Defines
  12. define('GETID3_VERSION', '1.7.9-20090308');
  13. define('GETID3_FREAD_BUFFER_SIZE', 16384); // read buffer size in bytes
  14. class getID3
  15. {
  16. // public: Settings
  17. var $encoding = 'ISO-8859-1'; // CASE SENSITIVE! - i.e. (must be supported by iconv())
  18. // Examples: ISO-8859-1 UTF-8 UTF-16 UTF-16BE
  19. var $encoding_id3v1 = 'ISO-8859-1'; // Should always be 'ISO-8859-1', but some tags may be written in other encodings such as 'EUC-CN'
  20. var $tempdir = '*'; // default '*' should use system temp dir
  21. // public: Optional tag checks - disable for speed.
  22. var $option_tag_id3v1 = true; // Read and process ID3v1 tags
  23. var $option_tag_id3v2 = true; // Read and process ID3v2 tags
  24. var $option_tag_lyrics3 = true; // Read and process Lyrics3 tags
  25. var $option_tag_apetag = true; // Read and process APE tags
  26. var $option_tags_process = true; // Copy tags to root key 'tags' and encode to $this->encoding
  27. var $option_tags_html = true; // Copy tags to root key 'tags_html' properly translated from various encodings to HTML entities
  28. // public: Optional tag/comment calucations
  29. var $option_extra_info = true; // Calculate additional info such as bitrate, channelmode etc
  30. // public: Optional calculations
  31. var $option_md5_data = false; // Get MD5 sum of data part - slow
  32. var $option_md5_data_source = false; // Use MD5 of source file if availble - only FLAC and OptimFROG
  33. var $option_sha1_data = false; // Get SHA1 sum of data part - slow
  34. var $option_max_2gb_check = true; // Check whether file is larger than 2 Gb and thus not supported by PHP
  35. // private
  36. var $filename;
  37. // public: constructor
  38. function getID3()
  39. {
  40. $this->startup_error = '';
  41. $this->startup_warning = '';
  42. // Check for PHP version >= 4.2.0
  43. if (phpversion() < '4.2.0') {
  44. $this->startup_error .= 'getID3() requires PHP v4.2.0 or higher - you are running v'.phpversion();
  45. }
  46. // Check memory
  47. $memory_limit = ini_get('memory_limit');
  48. if (eregi('([0-9]+)M', $memory_limit, $matches)) {
  49. // could be stored as "16M" rather than 16777216 for example
  50. $memory_limit = $matches[1] * 1048576;
  51. }
  52. if ($memory_limit <= 0) {
  53. // memory limits probably disabled
  54. } elseif ($memory_limit <= 3145728) {
  55. $this->startup_error .= 'PHP has less than 3MB available memory and will very likely run out. Increase memory_limit in php.ini';
  56. } elseif ($memory_limit <= 12582912) {
  57. $this->startup_warning .= 'PHP has less than 12MB available memory and might run out if all modules are loaded. Increase memory_limit in php.ini';
  58. }
  59. // Check safe_mode off
  60. if ((bool) ini_get('safe_mode')) {
  61. $this->warning('WARNING: Safe mode is on, shorten support disabled, md5data/sha1data for ogg vorbis disabled, ogg vorbos/flac tag writing disabled.');
  62. }
  63. // define a constant rather than looking up every time it is needed
  64. if (!defined('GETID3_OS_ISWINDOWS')) {
  65. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
  66. define('GETID3_OS_ISWINDOWS', true);
  67. } else {
  68. define('GETID3_OS_ISWINDOWS', false);
  69. }
  70. }
  71. // Get base path of getID3() - ONCE
  72. if (!defined('GETID3_INCLUDEPATH')) {
  73. foreach (get_included_files() as $key => $val) {
  74. if (basename($val) == 'getid3.php') {
  75. define('GETID3_INCLUDEPATH', dirname($val).DIRECTORY_SEPARATOR);
  76. break;
  77. }
  78. }
  79. }
  80. // Load support library
  81. if (!include_once(GETID3_INCLUDEPATH.'getid3.lib.php')) {
  82. $this->startup_error .= 'getid3.lib.php is missing or corrupt';
  83. }
  84. // Needed for Windows only:
  85. // Define locations of helper applications for Shorten, VorbisComment, MetaFLAC
  86. // as well as other helper functions such as head, tail, md5sum, etc
  87. // IMPORTANT: This path cannot have spaces in it. If neccesary, use the 8dot3 equivalent
  88. // ie for "C:/Program Files/Apache/" put "C:/PROGRA~1/APACHE/"
  89. // IMPORTANT: This path must include the trailing slash
  90. if (GETID3_OS_ISWINDOWS && !defined('GETID3_HELPERAPPSDIR')) {
  91. $helperappsdir = GETID3_INCLUDEPATH.'..'.DIRECTORY_SEPARATOR.'helperapps'; // must not have any space in this path
  92. if (!is_dir($helperappsdir)) {
  93. $this->startup_error .= '"'.$helperappsdir.'" cannot be defined as GETID3_HELPERAPPSDIR because it does not exist';
  94. } elseif (strpos(realpath($helperappsdir), ' ') !== false) {
  95. $DirPieces = explode(DIRECTORY_SEPARATOR, realpath($helperappsdir));
  96. foreach ($DirPieces as $key => $value) {
  97. if ((strpos($value, '.') !== false) && (strpos($value, ' ') === false)) {
  98. if (strpos($value, '.') > 8) {
  99. $value = substr($value, 0, 6).'~1';
  100. }
  101. } elseif ((strpos($value, ' ') !== false) || strlen($value) > 8) {
  102. $value = substr($value, 0, 6).'~1';
  103. }
  104. $DirPieces[$key] = strtoupper($value);
  105. }
  106. $this->startup_error .= 'GETID3_HELPERAPPSDIR must not have any spaces in it - use 8dot3 naming convention if neccesary (on this server that would be something like "'.implode(DIRECTORY_SEPARATOR, $DirPieces).'" - NOTE: this may or may not be the actual 8.3 equivalent of "'.$helperappsdir.'", please double-check). You can run "dir /x" from the commandline to see the correct 8.3-style names.';
  107. }
  108. define('GETID3_HELPERAPPSDIR', realpath($helperappsdir).DIRECTORY_SEPARATOR);
  109. }
  110. }
  111. // public: setOption
  112. function setOption($optArray) {
  113. if (!is_array($optArray) || empty($optArray)) {
  114. return false;
  115. }
  116. foreach ($optArray as $opt => $val) {
  117. //if (isset($this, $opt) === false) {
  118. if (isset($this->$opt) === false) {
  119. continue;
  120. }
  121. $this->$opt = $val;
  122. }
  123. return true;
  124. }
  125. // public: analyze file - replaces GetAllFileInfo() and GetTagOnly()
  126. function analyze($filename) {
  127. if (!empty($this->startup_error)) {
  128. return $this->error($this->startup_error);
  129. }
  130. if (!empty($this->startup_warning)) {
  131. $this->warning($this->startup_warning);
  132. }
  133. // init result array and set parameters
  134. $this->info = array();
  135. $this->info['GETID3_VERSION'] = GETID3_VERSION;
  136. // Check encoding/iconv support
  137. if (!function_exists('iconv') && !in_array($this->encoding, array('ISO-8859-1', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'UTF-16'))) {
  138. $errormessage = 'iconv() support is needed for encodings other than ISO-8859-1, UTF-8, UTF-16LE, UTF16-BE, UTF-16. ';
  139. if (GETID3_OS_ISWINDOWS) {
  140. $errormessage .= 'PHP does not have iconv() support. Please enable php_iconv.dll in php.ini, and copy iconv.dll from c:/php/dlls to c:/windows/system32';
  141. } else {
  142. $errormessage .= 'PHP is not compiled with iconv() support. Please recompile with the --with-iconv switch';
  143. }
  144. return $this->error($errormessage);
  145. }
  146. // Disable magic_quotes_runtime, if neccesary
  147. $old_magic_quotes_runtime = get_magic_quotes_runtime(); // store current setting of magic_quotes_runtime
  148. if ($old_magic_quotes_runtime) {
  149. set_magic_quotes_runtime(0); // turn off magic_quotes_runtime
  150. if (get_magic_quotes_runtime()) {
  151. return $this->error('Could not disable magic_quotes_runtime - getID3() cannot work properly with this setting enabled');
  152. }
  153. }
  154. // remote files not supported
  155. if (preg_match('/^(ht|f)tp:\/\//', $filename)) {
  156. return $this->error('Remote files are not supported in this version of getID3() - please copy the file locally first');
  157. }
  158. $filename = str_replace('/', DIRECTORY_SEPARATOR, $filename);
  159. $filename = preg_replace('#'.preg_quote(DIRECTORY_SEPARATOR).'{2,}#', DIRECTORY_SEPARATOR, $filename);
  160. // open local file
  161. if (file_exists($filename) && ($fp = @fopen($filename, 'rb'))) {
  162. // great
  163. } else {
  164. return $this->error('Could not open file "'.$filename.'"');
  165. }
  166. // set parameters
  167. $this->info['filesize'] = filesize($filename);
  168. // option_max_2gb_check
  169. if ($this->option_max_2gb_check) {
  170. // PHP doesn't support integers larger than 31-bit (~2GB)
  171. // filesize() simply returns (filesize % (pow(2, 32)), no matter the actual filesize
  172. // ftell() returns 0 if seeking to the end is beyond the range of unsigned integer
  173. fseek($fp, 0, SEEK_END);
  174. if ((($this->info['filesize'] != 0) && (ftell($fp) == 0)) ||
  175. ($this->info['filesize'] < 0) ||
  176. (ftell($fp) < 0)) {
  177. $real_filesize = false;
  178. if (GETID3_OS_ISWINDOWS) {
  179. $commandline = 'dir /-C "'.str_replace('/', DIRECTORY_SEPARATOR, $filename).'"';
  180. $dir_output = `$commandline`;
  181. if (eregi('1 File\(s\)[ ]+([0-9]+) bytes', $dir_output, $matches)) {
  182. $real_filesize = (float) $matches[1];
  183. }
  184. } else {
  185. $commandline = 'ls -o -g -G --time-style=long-iso '.escapeshellarg($filename);
  186. $dir_output = `$commandline`;
  187. if (eregi('([0-9]+) ([0-9]{4}-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}) '.preg_quote($filename).'$', $dir_output, $matches)) {
  188. $real_filesize = (float) $matches[1];
  189. }
  190. }
  191. if ($real_filesize === false) {
  192. unset($this->info['filesize']);
  193. fclose($fp);
  194. return $this->error('File is most likely larger than 2GB and is not supported by PHP');
  195. } elseif ($real_filesize < pow(2, 31)) {
  196. unset($this->info['filesize']);
  197. fclose($fp);
  198. return $this->error('PHP seems to think the file is larger than 2GB, but filesystem reports it as '.number_format($real_filesize, 3).'GB, please report to info@getid3.org');
  199. }
  200. $this->info['filesize'] = $real_filesize;
  201. $this->error('File is larger than 2GB (filesystem reports it as '.number_format($real_filesize, 3).'GB) and is not properly supported by PHP.');
  202. }
  203. }
  204. // set more parameters
  205. $this->info['avdataoffset'] = 0;
  206. $this->info['avdataend'] = $this->info['filesize'];
  207. $this->info['fileformat'] = ''; // filled in later
  208. $this->info['audio']['dataformat'] = ''; // filled in later, unset if not used
  209. $this->info['video']['dataformat'] = ''; // filled in later, unset if not used
  210. $this->info['tags'] = array(); // filled in later, unset if not used
  211. $this->info['error'] = array(); // filled in later, unset if not used
  212. $this->info['warning'] = array(); // filled in later, unset if not used
  213. $this->info['comments'] = array(); // filled in later, unset if not used
  214. $this->info['encoding'] = $this->encoding; // required by id3v2 and iso modules - can be unset at the end if desired
  215. // set redundant parameters - might be needed in some include file
  216. $this->info['filename'] = basename($filename);
  217. $this->info['filepath'] = str_replace('\\', '/', realpath(dirname($filename)));
  218. $this->info['filenamepath'] = $this->info['filepath'].'/'.$this->info['filename'];
  219. // handle ID3v2 tag - done first - already at beginning of file
  220. // ID3v2 detection (even if not parsing) is always done otherwise fileformat is much harder to detect
  221. if ($this->option_tag_id3v2) {
  222. $GETID3_ERRORARRAY = &$this->info['warning'];
  223. if (getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v2.php', __FILE__, false)) {
  224. $tag = new getid3_id3v2($fp, $this->info);
  225. unset($tag);
  226. }
  227. } else {
  228. fseek($fp, 0, SEEK_SET);
  229. $header = fread($fp, 10);
  230. if (substr($header, 0, 3) == 'ID3' && strlen($header) == 10) {
  231. $this->info['id3v2']['header'] = true;
  232. $this->info['id3v2']['majorversion'] = ord($header{3});
  233. $this->info['id3v2']['minorversion'] = ord($header{4});
  234. $this->info['id3v2']['headerlength'] = getid3_lib::BigEndian2Int(substr($header, 6, 4), 1) + 10; // length of ID3v2 tag in 10-byte header doesn't include 10-byte header length
  235. $this->info['id3v2']['tag_offset_start'] = 0;
  236. $this->info['id3v2']['tag_offset_end'] = $this->info['id3v2']['tag_offset_start'] + $this->info['id3v2']['headerlength'];
  237. $this->info['avdataoffset'] = $this->info['id3v2']['tag_offset_end'];
  238. }
  239. }
  240. // handle ID3v1 tag
  241. if ($this->option_tag_id3v1) {
  242. if (!@include_once(GETID3_INCLUDEPATH.'module.tag.id3v1.php')) {
  243. return $this->error('module.tag.id3v1.php is missing - you may disable option_tag_id3v1.');
  244. }
  245. $tag = new getid3_id3v1($fp, $this->info);
  246. unset($tag);
  247. }
  248. // handle APE tag
  249. if ($this->option_tag_apetag) {
  250. if (!@include_once(GETID3_INCLUDEPATH.'module.tag.apetag.php')) {
  251. return $this->error('module.tag.apetag.php is missing - you may disable option_tag_apetag.');
  252. }
  253. $tag = new getid3_apetag($fp, $this->info);
  254. unset($tag);
  255. }
  256. // handle lyrics3 tag
  257. if ($this->option_tag_lyrics3) {
  258. if (!@include_once(GETID3_INCLUDEPATH.'module.tag.lyrics3.php')) {
  259. return $this->error('module.tag.lyrics3.php is missing - you may disable option_tag_lyrics3.');
  260. }
  261. $tag = new getid3_lyrics3($fp, $this->info);
  262. unset($tag);
  263. }
  264. // read 32 kb file data
  265. fseek($fp, $this->info['avdataoffset'], SEEK_SET);
  266. $formattest = fread($fp, 32774);
  267. // determine format
  268. $determined_format = $this->GetFileFormat($formattest, $filename);
  269. // unable to determine file format
  270. if (!$determined_format) {
  271. fclose($fp);
  272. return $this->error('unable to determine file format');
  273. }
  274. // check for illegal ID3 tags
  275. if (isset($determined_format['fail_id3']) && (in_array('id3v1', $this->info['tags']) || in_array('id3v2', $this->info['tags']))) {
  276. if ($determined_format['fail_id3'] === 'ERROR') {
  277. fclose($fp);
  278. return $this->error('ID3 tags not allowed on this file type.');
  279. } elseif ($determined_format['fail_id3'] === 'WARNING') {
  280. $this->info['warning'][] = 'ID3 tags not allowed on this file type.';
  281. }
  282. }
  283. // check for illegal APE tags
  284. if (isset($determined_format['fail_ape']) && in_array('ape', $this->info['tags'])) {
  285. if ($determined_format['fail_ape'] === 'ERROR') {
  286. fclose($fp);
  287. return $this->error('APE tags not allowed on this file type.');
  288. } elseif ($determined_format['fail_ape'] === 'WARNING') {
  289. $this->info['warning'][] = 'APE tags not allowed on this file type.';
  290. }
  291. }
  292. // set mime type
  293. $this->info['mime_type'] = $determined_format['mime_type'];
  294. // supported format signature pattern detected, but module deleted
  295. if (!file_exists(GETID3_INCLUDEPATH.$determined_format['include'])) {
  296. fclose($fp);
  297. return $this->error('Format not supported, module "'.$determined_format['include'].'" was removed.');
  298. }
  299. // module requires iconv support
  300. if (!function_exists('iconv') && @$determined_format['iconv_req']) {
  301. return $this->error('iconv support is required for this module ('.$determined_format['include'].').');
  302. }
  303. // include module
  304. include_once(GETID3_INCLUDEPATH.$determined_format['include']);
  305. // instantiate module class
  306. $class_name = 'getid3_'.$determined_format['module'];
  307. if (!class_exists($class_name)) {
  308. return $this->error('Format not supported, module "'.$determined_format['include'].'" is corrupt.');
  309. }
  310. if (isset($determined_format['option'])) {
  311. $class = new $class_name($fp, $this->info, $determined_format['option']);
  312. } else {
  313. $class = new $class_name($fp, $this->info);
  314. }
  315. unset($class);
  316. // close file
  317. fclose($fp);
  318. // process all tags - copy to 'tags' and convert charsets
  319. if ($this->option_tags_process) {
  320. $this->HandleAllTags();
  321. }
  322. // perform more calculations
  323. if ($this->option_extra_info) {
  324. $this->ChannelsBitratePlaytimeCalculations();
  325. $this->CalculateCompressionRatioVideo();
  326. $this->CalculateCompressionRatioAudio();
  327. $this->CalculateReplayGain();
  328. $this->ProcessAudioStreams();
  329. }
  330. // get the MD5 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
  331. if ($this->option_md5_data) {
  332. // do not cald md5_data if md5_data_source is present - set by flac only - future MPC/SV8 too
  333. if (!$this->option_md5_data_source || empty($this->info['md5_data_source'])) {
  334. $this->getHashdata('md5');
  335. }
  336. }
  337. // get the SHA1 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
  338. if ($this->option_sha1_data) {
  339. $this->getHashdata('sha1');
  340. }
  341. // remove undesired keys
  342. $this->CleanUp();
  343. // restore magic_quotes_runtime setting
  344. set_magic_quotes_runtime($old_magic_quotes_runtime);
  345. // return info array
  346. return $this->info;
  347. }
  348. // private: error handling
  349. function error($message) {
  350. $this->CleanUp();
  351. $this->info['error'][] = $message;
  352. return $this->info;
  353. }
  354. // private: warning handling
  355. function warning($message) {
  356. $this->info['warning'][] = $message;
  357. return true;
  358. }
  359. // private: CleanUp
  360. function CleanUp() {
  361. // remove possible empty keys
  362. $AVpossibleEmptyKeys = array('dataformat', 'bits_per_sample', 'encoder_options', 'streams', 'bitrate');
  363. foreach ($AVpossibleEmptyKeys as $dummy => $key) {
  364. if (empty($this->info['audio'][$key]) && isset($this->info['audio'][$key])) {
  365. unset($this->info['audio'][$key]);
  366. }
  367. if (empty($this->info['video'][$key]) && isset($this->info['video'][$key])) {
  368. unset($this->info['video'][$key]);
  369. }
  370. }
  371. // remove empty root keys
  372. if (!empty($this->info)) {
  373. foreach ($this->info as $key => $value) {
  374. if (empty($this->info[$key]) && ($this->info[$key] !== 0) && ($this->info[$key] !== '0')) {
  375. unset($this->info[$key]);
  376. }
  377. }
  378. }
  379. // remove meaningless entries from unknown-format files
  380. if (empty($this->info['fileformat'])) {
  381. if (isset($this->info['avdataoffset'])) {
  382. unset($this->info['avdataoffset']);
  383. }
  384. if (isset($this->info['avdataend'])) {
  385. unset($this->info['avdataend']);
  386. }
  387. }
  388. }
  389. // return array containing information about all supported formats
  390. function GetFileFormatArray() {
  391. static $format_info = array();
  392. if (empty($format_info)) {
  393. $format_info = array(
  394. // Audio formats
  395. // AC-3 - audio - Dolby AC-3 / Dolby Digital
  396. 'ac3' => array(
  397. 'pattern' => '^\x0B\x77',
  398. 'group' => 'audio',
  399. 'module' => 'ac3',
  400. 'mime_type' => 'audio/ac3',
  401. ),
  402. // AAC - audio - Advanced Audio Coding (AAC) - ADIF format
  403. 'adif' => array(
  404. 'pattern' => '^ADIF',
  405. 'group' => 'audio',
  406. 'module' => 'aac',
  407. 'option' => 'adif',
  408. 'mime_type' => 'application/octet-stream',
  409. 'fail_ape' => 'WARNING',
  410. ),
  411. // AAC - audio - Advanced Audio Coding (AAC) - ADTS format (very similar to MP3)
  412. 'adts' => array(
  413. 'pattern' => '^\xFF[\xF0-\xF1\xF8-\xF9]',
  414. 'group' => 'audio',
  415. 'module' => 'aac',
  416. 'option' => 'adts',
  417. 'mime_type' => 'application/octet-stream',
  418. 'fail_ape' => 'WARNING',
  419. ),
  420. // AU - audio - NeXT/Sun AUdio (AU)
  421. 'au' => array(
  422. 'pattern' => '^\.snd',
  423. 'group' => 'audio',
  424. 'module' => 'au',
  425. 'mime_type' => 'audio/basic',
  426. ),
  427. // AVR - audio - Audio Visual Research
  428. 'avr' => array(
  429. 'pattern' => '^2BIT',
  430. 'group' => 'audio',
  431. 'module' => 'avr',
  432. 'mime_type' => 'application/octet-stream',
  433. ),
  434. // BONK - audio - Bonk v0.9+
  435. 'bonk' => array(
  436. 'pattern' => '^\x00(BONK|INFO|META| ID3)',
  437. 'group' => 'audio',
  438. 'module' => 'bonk',
  439. 'mime_type' => 'audio/xmms-bonk',
  440. ),
  441. // DSS - audio - Digital Speech Standard
  442. 'dss' => array(
  443. 'pattern' => '^[\x02]dss',
  444. 'group' => 'audio',
  445. 'module' => 'dss',
  446. 'mime_type' => 'application/octet-stream',
  447. ),
  448. // DTS - audio - Dolby Theatre System
  449. 'dts' => array(
  450. 'pattern' => '^\x7F\xFE\x80\x01',
  451. 'group' => 'audio',
  452. 'module' => 'dts',
  453. 'mime_type' => 'audio/dts',
  454. ),
  455. // FLAC - audio - Free Lossless Audio Codec
  456. 'flac' => array(
  457. 'pattern' => '^fLaC',
  458. 'group' => 'audio',
  459. 'module' => 'flac',
  460. 'mime_type' => 'audio/x-flac',
  461. ),
  462. // LA - audio - Lossless Audio (LA)
  463. 'la' => array(
  464. 'pattern' => '^LA0[2-4]',
  465. 'group' => 'audio',
  466. 'module' => 'la',
  467. 'mime_type' => 'application/octet-stream',
  468. ),
  469. // LPAC - audio - Lossless Predictive Audio Compression (LPAC)
  470. 'lpac' => array(
  471. 'pattern' => '^LPAC',
  472. 'group' => 'audio',
  473. 'module' => 'lpac',
  474. 'mime_type' => 'application/octet-stream',
  475. ),
  476. // MIDI - audio - MIDI (Musical Instrument Digital Interface)
  477. 'midi' => array(
  478. 'pattern' => '^MThd',
  479. 'group' => 'audio',
  480. 'module' => 'midi',
  481. 'mime_type' => 'audio/midi',
  482. ),
  483. // MAC - audio - Monkey's Audio Compressor
  484. 'mac' => array(
  485. 'pattern' => '^MAC ',
  486. 'group' => 'audio',
  487. 'module' => 'monkey',
  488. 'mime_type' => 'application/octet-stream',
  489. ),
  490. // has been known to produce false matches in random files (e.g. JPEGs), leave out until more precise matching available
  491. // // MOD - audio - MODule (assorted sub-formats)
  492. // 'mod' => array(
  493. // 'pattern' => '^.{1080}(M\\.K\\.|M!K!|FLT4|FLT8|[5-9]CHN|[1-3][0-9]CH)',
  494. // 'group' => 'audio',
  495. // 'module' => 'mod',
  496. // 'option' => 'mod',
  497. // 'mime_type' => 'audio/mod',
  498. // ),
  499. // MOD - audio - MODule (Impulse Tracker)
  500. 'it' => array(
  501. 'pattern' => '^IMPM',
  502. 'group' => 'audio',
  503. 'module' => 'mod',
  504. 'option' => 'it',
  505. 'mime_type' => 'audio/it',
  506. ),
  507. // MOD - audio - MODule (eXtended Module, various sub-formats)
  508. 'xm' => array(
  509. 'pattern' => '^Extended Module',
  510. 'group' => 'audio',
  511. 'module' => 'mod',
  512. 'option' => 'xm',
  513. 'mime_type' => 'audio/xm',
  514. ),
  515. // MOD - audio - MODule (ScreamTracker)
  516. 's3m' => array(
  517. 'pattern' => '^.{44}SCRM',
  518. 'group' => 'audio',
  519. 'module' => 'mod',
  520. 'option' => 's3m',
  521. 'mime_type' => 'audio/s3m',
  522. ),
  523. // MPC - audio - Musepack / MPEGplus
  524. 'mpc' => array(
  525. 'pattern' => '^(MPCK|MP\+|[\x00\x01\x10\x11\x40\x41\x50\x51\x80\x81\x90\x91\xC0\xC1\xD0\xD1][\x20-37][\x00\x20\x40\x60\x80\xA0\xC0\xE0])',
  526. 'group' => 'audio',
  527. 'module' => 'mpc',
  528. 'mime_type' => 'audio/x-musepack',
  529. ),
  530. // MP3 - audio - MPEG-audio Layer 3 (very similar to AAC-ADTS)
  531. 'mp3' => array(
  532. 'pattern' => '^\xFF[\xE2-\xE7\xF2-\xF7\xFA-\xFF][\x00-\xEB]',
  533. 'group' => 'audio',
  534. 'module' => 'mp3',
  535. 'mime_type' => 'audio/mpeg',
  536. ),
  537. // OFR - audio - OptimFROG
  538. 'ofr' => array(
  539. 'pattern' => '^(\*RIFF|OFR)',
  540. 'group' => 'audio',
  541. 'module' => 'optimfrog',
  542. 'mime_type' => 'application/octet-stream',
  543. ),
  544. // RKAU - audio - RKive AUdio compressor
  545. 'rkau' => array(
  546. 'pattern' => '^RKA',
  547. 'group' => 'audio',
  548. 'module' => 'rkau',
  549. 'mime_type' => 'application/octet-stream',
  550. ),
  551. // SHN - audio - Shorten
  552. 'shn' => array(
  553. 'pattern' => '^ajkg',
  554. 'group' => 'audio',
  555. 'module' => 'shorten',
  556. 'mime_type' => 'audio/xmms-shn',
  557. 'fail_id3' => 'ERROR',
  558. 'fail_ape' => 'ERROR',
  559. ),
  560. // TTA - audio - TTA Lossless Audio Compressor (http://tta.corecodec.org)
  561. 'tta' => array(
  562. 'pattern' => '^TTA', // could also be '^TTA(\x01|\x02|\x03|2|1)'
  563. 'group' => 'audio',
  564. 'module' => 'tta',
  565. 'mime_type' => 'application/octet-stream',
  566. ),
  567. // VOC - audio - Creative Voice (VOC)
  568. 'voc' => array(
  569. 'pattern' => '^Creative Voice File',
  570. 'group' => 'audio',
  571. 'module' => 'voc',
  572. 'mime_type' => 'audio/voc',
  573. ),
  574. // VQF - audio - transform-domain weighted interleave Vector Quantization Format (VQF)
  575. 'vqf' => array(
  576. 'pattern' => '^TWIN',
  577. 'group' => 'audio',
  578. 'module' => 'vqf',
  579. 'mime_type' => 'application/octet-stream',
  580. ),
  581. // WV - audio - WavPack (v4.0+)
  582. 'wv' => array(
  583. 'pattern' => '^wvpk',
  584. 'group' => 'audio',
  585. 'module' => 'wavpack',
  586. 'mime_type' => 'application/octet-stream',
  587. ),
  588. // Audio-Video formats
  589. // ASF - audio/video - Advanced Streaming Format, Windows Media Video, Windows Media Audio
  590. 'asf' => array(
  591. 'pattern' => '^\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C',
  592. 'group' => 'audio-video',
  593. 'module' => 'asf',
  594. 'mime_type' => 'video/x-ms-asf',
  595. 'iconv_req' => false,
  596. ),
  597. // BINK - audio/video - Bink / Smacker
  598. 'bink' => array(
  599. 'pattern' => '^(BIK|SMK)',
  600. 'group' => 'audio-video',
  601. 'module' => 'bink',
  602. 'mime_type' => 'application/octet-stream',
  603. ),
  604. // FLV - audio/video - FLash Video
  605. 'flv' => array(
  606. 'pattern' => '^FLV\x01',
  607. 'group' => 'audio-video',
  608. 'module' => 'flv',
  609. 'mime_type' => 'video/x-flv',
  610. ),
  611. // MKAV - audio/video - Mastroka
  612. 'matroska' => array(
  613. 'pattern' => '^\x1A\x45\xDF\xA3',
  614. 'group' => 'audio-video',
  615. 'module' => 'matroska',
  616. 'mime_type' => 'video/x-matroska', // may also be audio/x-matroska
  617. ),
  618. // MPEG - audio/video - MPEG (Moving Pictures Experts Group)
  619. 'mpeg' => array(
  620. 'pattern' => '^\x00\x00\x01(\xBA|\xB3)',
  621. 'group' => 'audio-video',
  622. 'module' => 'mpeg',
  623. 'mime_type' => 'video/mpeg',
  624. ),
  625. // NSV - audio/video - Nullsoft Streaming Video (NSV)
  626. 'nsv' => array(
  627. 'pattern' => '^NSV[sf]',
  628. 'group' => 'audio-video',
  629. 'module' => 'nsv',
  630. 'mime_type' => 'application/octet-stream',
  631. ),
  632. // Ogg - audio/video - Ogg (Ogg-Vorbis, Ogg-FLAC, Speex, Ogg-Theora(*), Ogg-Tarkin(*))
  633. 'ogg' => array(
  634. 'pattern' => '^OggS',
  635. 'group' => 'audio',
  636. 'module' => 'ogg',
  637. 'mime_type' => 'application/ogg',
  638. 'fail_id3' => 'WARNING',
  639. 'fail_ape' => 'WARNING',
  640. ),
  641. // QT - audio/video - Quicktime
  642. 'quicktime' => array(
  643. 'pattern' => '^.{4}(cmov|free|ftyp|mdat|moov|pnot|skip|wide)',
  644. 'group' => 'audio-video',
  645. 'module' => 'quicktime',
  646. 'mime_type' => 'video/quicktime',
  647. ),
  648. // RIFF - audio/video - Resource Interchange File Format (RIFF) / WAV / AVI / CD-audio / SDSS = renamed variant used by SmartSound QuickTracks (www.smartsound.com) / FORM = Audio Interchange File Format (AIFF)
  649. 'riff' => array(
  650. 'pattern' => '^(RIFF|SDSS|FORM)',
  651. 'group' => 'audio-video',
  652. 'module' => 'riff',
  653. 'mime_type' => 'audio/x-wave',
  654. 'fail_ape' => 'WARNING',
  655. ),
  656. // Real - audio/video - RealAudio, RealVideo
  657. 'real' => array(
  658. 'pattern' => '^(\\.RMF|\\.ra)',
  659. 'group' => 'audio-video',
  660. 'module' => 'real',
  661. 'mime_type' => 'audio/x-realaudio',
  662. ),
  663. // SWF - audio/video - ShockWave Flash
  664. 'swf' => array(
  665. 'pattern' => '^(F|C)WS',
  666. 'group' => 'audio-video',
  667. 'module' => 'swf',
  668. 'mime_type' => 'application/x-shockwave-flash',
  669. ),
  670. // Still-Image formats
  671. // BMP - still image - Bitmap (Windows, OS/2; uncompressed, RLE8, RLE4)
  672. 'bmp' => array(
  673. 'pattern' => '^BM',
  674. 'group' => 'graphic',
  675. 'module' => 'bmp',
  676. 'mime_type' => 'image/bmp',
  677. 'fail_id3' => 'ERROR',
  678. 'fail_ape' => 'ERROR',
  679. ),
  680. // GIF - still image - Graphics Interchange Format
  681. 'gif' => array(
  682. 'pattern' => '^GIF',
  683. 'group' => 'graphic',
  684. 'module' => 'gif',
  685. 'mime_type' => 'image/gif',
  686. 'fail_id3' => 'ERROR',
  687. 'fail_ape' => 'ERROR',
  688. ),
  689. // JPEG - still image - Joint Photographic Experts Group (JPEG)
  690. 'jpg' => array(
  691. 'pattern' => '^\xFF\xD8\xFF',
  692. 'group' => 'graphic',
  693. 'module' => 'jpg',
  694. 'mime_type' => 'image/jpeg',
  695. 'fail_id3' => 'ERROR',
  696. 'fail_ape' => 'ERROR',
  697. ),
  698. // PCD - still image - Kodak Photo CD
  699. 'pcd' => array(
  700. 'pattern' => '^.{2048}PCD_IPI\x00',
  701. 'group' => 'graphic',
  702. 'module' => 'pcd',
  703. 'mime_type' => 'image/x-photo-cd',
  704. 'fail_id3' => 'ERROR',
  705. 'fail_ape' => 'ERROR',
  706. ),
  707. // PNG - still image - Portable Network Graphics (PNG)
  708. 'png' => array(
  709. 'pattern' => '^\x89\x50\x4E\x47\x0D\x0A\x1A\x0A',
  710. 'group' => 'graphic',
  711. 'module' => 'png',
  712. 'mime_type' => 'image/png',
  713. 'fail_id3' => 'ERROR',
  714. 'fail_ape' => 'ERROR',
  715. ),
  716. // SVG - still image - Scalable Vector Graphics (SVG)
  717. 'svg' => array(
  718. 'pattern' => '<!DOCTYPE svg PUBLIC ',
  719. 'group' => 'graphic',
  720. 'module' => 'svg',
  721. 'mime_type' => 'image/svg+xml',
  722. 'fail_id3' => 'ERROR',
  723. 'fail_ape' => 'ERROR',
  724. ),
  725. // TIFF - still image - Tagged Information File Format (TIFF)
  726. 'tiff' => array(
  727. 'pattern' => '^(II\x2A\x00|MM\x00\x2A)',
  728. 'group' => 'graphic',
  729. 'module' => 'tiff',
  730. 'mime_type' => 'image/tiff',
  731. 'fail_id3' => 'ERROR',
  732. 'fail_ape' => 'ERROR',
  733. ),
  734. // Data formats
  735. // ISO - data - International Standards Organization (ISO) CD-ROM Image
  736. 'iso' => array(
  737. 'pattern' => '^.{32769}CD001',
  738. 'group' => 'misc',
  739. 'module' => 'iso',
  740. 'mime_type' => 'application/octet-stream',
  741. 'fail_id3' => 'ERROR',
  742. 'fail_ape' => 'ERROR',
  743. 'iconv_req' => false,
  744. ),
  745. // RAR - data - RAR compressed data
  746. 'rar' => array(
  747. 'pattern' => '^Rar\!',
  748. 'group' => 'archive',
  749. 'module' => 'rar',
  750. 'mime_type' => 'application/octet-stream',
  751. 'fail_id3' => 'ERROR',
  752. 'fail_ape' => 'ERROR',
  753. ),
  754. // SZIP - audio/data - SZIP compressed data
  755. 'szip' => array(
  756. 'pattern' => '^SZ\x0A\x04',
  757. 'group' => 'archive',
  758. 'module' => 'szip',
  759. 'mime_type' => 'application/octet-stream',
  760. 'fail_id3' => 'ERROR',
  761. 'fail_ape' => 'ERROR',
  762. ),
  763. // TAR - data - TAR compressed data
  764. 'tar' => array(
  765. 'pattern' => '^.{100}[0-9\x20]{7}\x00[0-9\x20]{7}\x00[0-9\x20]{7}\x00[0-9\x20\x00]{12}[0-9\x20\x00]{12}',
  766. 'group' => 'archive',
  767. 'module' => 'tar',
  768. 'mime_type' => 'application/x-tar',
  769. 'fail_id3' => 'ERROR',
  770. 'fail_ape' => 'ERROR',
  771. ),
  772. // GZIP - data - GZIP compressed data
  773. 'gz' => array(
  774. 'pattern' => '^\x1F\x8B\x08',
  775. 'group' => 'archive',
  776. 'module' => 'gzip',
  777. 'mime_type' => 'application/x-gzip',
  778. 'fail_id3' => 'ERROR',
  779. 'fail_ape' => 'ERROR',
  780. ),
  781. // ZIP - data - ZIP compressed data
  782. 'zip' => array(
  783. 'pattern' => '^PK\x03\x04',
  784. 'group' => 'archive',
  785. 'module' => 'zip',
  786. 'mime_type' => 'application/zip',
  787. 'fail_id3' => 'ERROR',
  788. 'fail_ape' => 'ERROR',
  789. ),
  790. // Misc other formats
  791. // PAR2 - data - Parity Volume Set Specification 2.0
  792. 'par2' => array (
  793. 'pattern' => '^PAR2\x00PKT',
  794. 'group' => 'misc',
  795. 'module' => 'par2',
  796. 'mime_type' => 'application/octet-stream',
  797. 'fail_id3' => 'ERROR',
  798. 'fail_ape' => 'ERROR',
  799. ),
  800. // PDF - data - Portable Document Format
  801. 'pdf' => array(
  802. 'pattern' => '^\x25PDF',
  803. 'group' => 'misc',
  804. 'module' => 'pdf',
  805. 'mime_type' => 'application/pdf',
  806. 'fail_id3' => 'ERROR',
  807. 'fail_ape' => 'ERROR',
  808. ),
  809. // MSOFFICE - data - ZIP compressed data
  810. 'msoffice' => array(
  811. 'pattern' => '^\xD0\xCF\x11\xE0', // D0CF11E == DOCFILE == Microsoft Office Document
  812. 'group' => 'misc',
  813. 'module' => 'msoffice',
  814. 'mime_type' => 'application/octet-stream',
  815. 'fail_id3' => 'ERROR',
  816. 'fail_ape' => 'ERROR',
  817. ),
  818. );
  819. }
  820. return $format_info;
  821. }
  822. function GetFileFormat(&$filedata, $filename='') {
  823. // this function will determine the format of a file based on usually
  824. // the first 2-4 bytes of the file (8 bytes for PNG, 16 bytes for JPG,
  825. // and in the case of ISO CD image, 6 bytes offset 32kb from the start
  826. // of the file).
  827. // Identify file format - loop through $format_info and detect with reg expr
  828. foreach ($this->GetFileFormatArray() as $format_name => $info) {
  829. // Using preg_match() instead of ereg() - much faster
  830. // The /s switch on preg_match() forces preg_match() NOT to treat
  831. // newline (0x0A) characters as special chars but do a binary match
  832. if (preg_match('/'.$info['pattern'].'/s', $filedata)) {
  833. $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
  834. return $info;
  835. }
  836. }
  837. if (preg_match('/\.mp[123a]$/i', $filename)) {
  838. // Too many mp3 encoders on the market put gabage in front of mpeg files
  839. // use assume format on these if format detection failed
  840. $GetFileFormatArray = $this->GetFileFormatArray();
  841. $info = $GetFileFormatArray['mp3'];
  842. $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
  843. return $info;
  844. }
  845. return false;
  846. }
  847. // converts array to $encoding charset from $this->encoding
  848. function CharConvert(&$array, $encoding) {
  849. // identical encoding - end here
  850. if ($encoding == $this->encoding) {
  851. return;
  852. }
  853. // loop thru array
  854. foreach ($array as $key => $value) {
  855. // go recursive
  856. if (is_array($value)) {
  857. $this->CharConvert($array[$key], $encoding);
  858. }
  859. // convert string
  860. elseif (is_string($value)) {
  861. $array[$key] = trim(getid3_lib::iconv_fallback($encoding, $this->encoding, $value));
  862. }
  863. }
  864. }
  865. function HandleAllTags() {
  866. // key name => array (tag name, character encoding)
  867. static $tags;
  868. if (empty($tags)) {
  869. $tags = array(
  870. 'asf' => array('asf' , 'UTF-16LE'),
  871. 'midi' => array('midi' , 'ISO-8859-1'),
  872. 'nsv' => array('nsv' , 'ISO-8859-1'),
  873. 'ogg' => array('vorbiscomment' , 'UTF-8'),
  874. 'png' => array('png' , 'UTF-8'),
  875. 'tiff' => array('tiff' , 'ISO-8859-1'),
  876. 'quicktime' => array('quicktime' , 'ISO-8859-1'),
  877. 'real' => array('real' , 'ISO-8859-1'),
  878. 'vqf' => array('vqf' , 'ISO-8859-1'),
  879. 'zip' => array('zip' , 'ISO-8859-1'),
  880. 'riff' => array('riff' , 'ISO-8859-1'),
  881. 'lyrics3' => array('lyrics3' , 'ISO-8859-1'),
  882. 'id3v1' => array('id3v1' , $this->encoding_id3v1),
  883. 'id3v2' => array('id3v2' , 'UTF-8'), // not according to the specs (every frame can have a different encoding), but getID3() force-converts all encodings to UTF-8
  884. 'ape' => array('ape' , 'UTF-8')
  885. );
  886. }
  887. // loop thru comments array
  888. foreach ($tags as $comment_name => $tagname_encoding_array) {
  889. list($tag_name, $encoding) = $tagname_encoding_array;
  890. // fill in default encoding type if not already present
  891. if (isset($this->info[$comment_name]) && !isset($this->info[$comment_name]['encoding'])) {
  892. $this->info[$comment_name]['encoding'] = $encoding;
  893. }
  894. // copy comments if key name set
  895. if (!empty($this->info[$comment_name]['comments'])) {
  896. foreach ($this->info[$comment_name]['comments'] as $tag_key => $valuearray) {
  897. foreach ($valuearray as $key => $value) {
  898. if (strlen(trim($value)) > 0) {
  899. $this->info['tags'][trim($tag_name)][trim($tag_key)][] = $value; // do not trim!! Unicode characters will get mangled if trailing nulls are removed!
  900. }
  901. }
  902. }
  903. if (!isset($this->info['tags'][$tag_name])) {
  904. // comments are set but contain nothing but empty strings, so skip
  905. continue;
  906. }
  907. if ($this->option_tags_html) {
  908. foreach ($this->info['tags'][$tag_name] as $tag_key => $valuearray) {
  909. foreach ($valuearray as $key => $value) {
  910. if (is_string($value)) {
  911. //$this->info['tags_html'][$tag_name][$tag_key][$key] = getid3_lib::MultiByteCharString2HTML($value, $encoding);
  912. $this->info['tags_html'][$tag_name][$tag_key][$key] = str_replace('&#0;', '', getid3_lib::MultiByteCharString2HTML($value, $encoding));
  913. } else {
  914. $this->info['tags_html'][$tag_name][$tag_key][$key] = $value;
  915. }
  916. }
  917. }
  918. }
  919. $this->CharConvert($this->info['tags'][$tag_name], $encoding); // only copy gets converted!
  920. }
  921. }
  922. return true;
  923. }
  924. function getHashdata($algorithm) {
  925. switch ($algorithm) {
  926. case 'md5':
  927. case 'sha1':
  928. break;
  929. default:
  930. return $this->error('bad algorithm "'.$algorithm.'" in getHashdata()');
  931. break;
  932. }
  933. if ((@$this->info['fileformat'] == 'ogg') && (@$this->info['audio']['dataformat'] == 'vorbis')) {
  934. // We cannot get an identical md5_data value for Ogg files where the comments
  935. // span more than 1 Ogg page (compared to the same audio data with smaller
  936. // comments) using the normal getID3() method of MD5'ing the data between the
  937. // end of the comments and the end of the file (minus any trailing tags),
  938. // because the page sequence numbers of the pages that the audio data is on
  939. // do not match. Under normal circumstances, where comments are smaller than
  940. // the nominal 4-8kB page size, then this is not a problem, but if there are
  941. // very large comments, the only way around it is to strip off the comment
  942. // tags with vorbiscomment and MD5 that file.
  943. // This procedure must be applied to ALL Ogg files, not just the ones with
  944. // comments larger than 1 page, because the below method simply MD5's the
  945. // whole file with the comments stripped, not just the portion after the
  946. // comments block (which is the standard getID3() method.
  947. // The above-mentioned problem of comments spanning multiple pages and changing
  948. // page sequence numbers likely happens for OggSpeex and OggFLAC as well, but
  949. // currently vorbiscomment only works on OggVorbis files.
  950. if ((bool) ini_get('safe_mode')) {
  951. $this->info['warning'][] = 'Failed making system call to vorbiscomment.exe - '.$algorithm.'_data is incorrect - error returned: PHP running in Safe Mode (backtick operator not available)';
  952. $this->info[$algorithm.'_data'] = false;
  953. } else {
  954. // Prevent user from aborting script
  955. $old_abort = ignore_user_abort(true);
  956. // Create empty file
  957. $empty = tempnam('*', 'getID3');
  958. touch($empty);
  959. // Use vorbiscomment to make temp file without comments
  960. $temp = tempnam('*', 'getID3');
  961. $file = $this->info['filenamepath'];
  962. if (GETID3_OS_ISWINDOWS) {
  963. if (file_exists(GETID3_HELPERAPPSDIR.'vorbiscomment.exe')) {
  964. $commandline = '"'.GETID3_HELPERAPPSDIR.'vorbiscomment.exe" -w -c "'.$empty.'" "'.$file.'" "'.$temp.'"';
  965. $VorbisCommentError = `$commandline`;
  966. } else {
  967. $VorbisCommentError = 'vorbiscomment.exe not found in '.GETID3_HELPERAPPSDIR;
  968. }
  969. } else {
  970. $commandline = 'vorbiscomment -w -c "'.$empty.'" "'.$file.'" "'.$temp.'" 2>&1';
  971. $commandline = 'vorbiscomment -w -c '.escapeshellarg($empty).' '.escapeshellarg($file).' '.escapeshellarg($temp).' 2>&1';
  972. $VorbisCommentError = `$commandline`;
  973. }
  974. if (!empty($VorbisCommentError)) {
  975. $this->info['warning'][] = 'Failed making system call to vorbiscomment(.exe) - '.$algorithm.'_data will be incorrect. If vorbiscomment is unavailable, please download from http://www.vorbis.com/download.psp and put in the getID3() directory. Error returned: '.$VorbisCommentError;
  976. $this->info[$algorithm.'_data'] = false;
  977. } else {
  978. // Get hash of newly created file
  979. switch ($algorithm) {
  980. case 'md5':
  981. $this->info[$algorithm.'_data'] = getid3_lib::md5_file($temp);
  982. break;
  983. case 'sha1':
  984. $this->info[$algorithm.'_data'] = getid3_lib::sha1_file($temp);
  985. break;
  986. }
  987. }
  988. // Clean up
  989. unlink($empty);
  990. unlink($temp);
  991. // Reset abort setting
  992. ignore_user_abort($old_abort);
  993. }
  994. } else {
  995. if (!empty($this->info['avdataoffset']) || (isset($this->info['avdataend']) && ($this->info['avdataend'] < $this->info['filesize']))) {
  996. // get hash from part of file
  997. $this->info[$algorithm.'_data'] = getid3_lib::hash_data($this->info['filenamepath'], $this->info['avdataoffset'], $this->info['avdataend'], $algorithm);
  998. } else {
  999. // get hash from whole file
  1000. switch ($algorithm) {
  1001. case 'md5':
  1002. $this->info[$algorithm.'_data'] = getid3_lib::md5_file($this->info['filenamepath']);
  1003. break;
  1004. case 'sha1':
  1005. $this->info[$algorithm.'_data'] = getid3_lib::sha1_file($this->info['filenamepath']);
  1006. break;
  1007. }
  1008. }
  1009. }
  1010. return true;
  1011. }
  1012. function ChannelsBitratePlaytimeCalculations() {
  1013. // set channelmode on audio
  1014. if (@$this->info['audio']['channels'] == '1') {
  1015. $this->info['audio']['channelmode'] = 'mono';
  1016. } elseif (@$this->info['audio']['channels'] == '2') {
  1017. $this->info['audio']['channelmode'] = 'stereo';
  1018. }
  1019. // Calculate combined bitrate - audio + video
  1020. $CombinedBitrate = 0;
  1021. $CombinedBitrate += (isset($this->info['audio']['bitrate']) ? $this->info['audio']['bitrate'] : 0);
  1022. $CombinedBitrate += (isset($this->info['video']['bitrate']) ? $this->info['video']['bitrate'] : 0);
  1023. if (($CombinedBitrate > 0) && empty($this->info['bitrate'])) {
  1024. $this->info['bitrate'] = $CombinedBitrate;
  1025. }
  1026. //if ((isset($this->info['video']) && !isset($this->info['video']['bitrate'])) || (isset($this->info['audio']) && !isset($this->info['audio']['bitrate']))) {
  1027. // // for example, VBR MPEG video files cannot determine video bitrate:
  1028. // // should not set overall bitrate and playtime from audio bitrate only
  1029. // unset($this->info['bitrate']);
  1030. //}
  1031. // video bitrate undetermined, but calculable
  1032. if (isset($this->info['video']['dataformat']) && $this->info['video']['dataformat'] && (!isset($this->info['video']['bitrate']) || ($this->info['video']['bitrate'] == 0))) {
  1033. // if video bitrate not set
  1034. if (isset($this->info['audio']['bitrate']) && ($this->info['audio']['bitrate'] > 0) && ($this->info['audio']['bitrate'] == $this->info['bitrate'])) {
  1035. // AND if audio bitrate is set to same as overall bitrate
  1036. if (isset($this->info['playtime_seconds']) && ($this->info['playtime_seconds'] > 0)) {
  1037. // AND if playtime is set
  1038. if (isset($this->info['avdataend']) && isset($this->info['avdataoffset'])) {
  1039. // AND if AV data offset start/end is known
  1040. // THEN we can calculate the video bitrate
  1041. $this->info['bitrate'] = round((($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['playtime_seconds']);
  1042. $this->info['video']['bitrate'] = $this->info['bitrate'] - $this->info['audio']['bitrate'];
  1043. }
  1044. }
  1045. }
  1046. }
  1047. if ((!isset($this->info['playtime_seconds']) || ($this->info['playtime_seconds'] <= 0)) && !empty($this->info['bitrate'])) {
  1048. $this->info['playtime_seconds'] = (($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['bitrate'];
  1049. }
  1050. if (!isset($this->info['bitrate']) && !empty($this->info['playtime_seconds'])) {
  1051. $this->info['bitrate'] = (($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['playtime_seconds'];
  1052. }
  1053. //echo '<pre>';
  1054. //var_dump($this->info['bitrate']);
  1055. //var_dump($this->info['audio']['bitrate']);
  1056. //var_dump($this->info['video']['bitrate']);
  1057. //echo '</pre>';
  1058. if (isset($this->info['bitrate']) && empty($this->info['audio']['bitrate']) && empty($this->info['video']['bitrate'])) {
  1059. if (isset($this->info['audio']['dataformat']) && empty($this->info['video']['resolution_x'])) {
  1060. // audio only
  1061. $this->info['audio']['bitrate'] = $this->info['bitrate'];
  1062. } elseif (isset($this->info['video']['resolution_x']) && empty($this->info['audio']['dataformat'])) {
  1063. // video only
  1064. $this->info['video']['bitrate'] = $this->info['bitrate'];
  1065. }
  1066. }
  1067. // Set playtime string
  1068. if (!empty($this->info['playtime_seconds']) && empty($this->info['playtime_string'])) {
  1069. $this->info['playtime_string'] = getid3_lib::PlaytimeString($this->info['playtime_seconds']);
  1070. }
  1071. }
  1072. function CalculateCompressionRatioVideo() {
  1073. if (empty($this->info['video'])) {
  1074. return false;
  1075. }
  1076. if (empty($this->info['video']['resolution_x']) || empty($this->info['video']['resolution_y'])) {
  1077. return false;
  1078. }
  1079. if (empty($this->info['video']['bits_per_sample'])) {
  1080. return false;
  1081. }
  1082. switch ($this->info['video']['dataformat']) {
  1083. case 'bmp':
  1084. case 'gif':
  1085. case 'jpeg':
  1086. case 'jpg':
  1087. case 'png':
  1088. case 'tiff':
  1089. $FrameRate = 1;
  1090. $PlaytimeSeconds = 1;
  1091. $BitrateCompressed = $this->info['filesize'] * 8;
  1092. break;
  1093. default:
  1094. if (!empty($this->info['video']['frame_rate'])) {
  1095. $FrameRate = $this->info['video']['frame_rate'];
  1096. } else {
  1097. return false;
  1098. }
  1099. if (!empty($this->info['playtime_seconds'])) {
  1100. $PlaytimeSeconds = $this->info['playtime_seconds'];
  1101. } else {
  1102. return false;
  1103. }
  1104. if (!empty($this->info['video']['bitrate'])) {
  1105. $BitrateCompressed = $this->info['video']['bitrate'];
  1106. } else {
  1107. return false;
  1108. }
  1109. break;
  1110. }
  1111. $BitrateUncompressed = $this->info['video']['resolution_x'] * $this->info['video']['resolution_y'] * $this->info['video']['bits_per_sample'] * $FrameRate;
  1112. $this->info['video']['compression_ratio'] = $BitrateCompressed / $BitrateUncompressed;
  1113. return true;
  1114. }
  1115. function CalculateCompressionRatioAudio() {
  1116. if (empty($this->info['audio']['bitrate']) || empty($this->info['audio']['channels']) || empty($this->info['audio']['sample_rate'])) {
  1117. return false;
  1118. }
  1119. $this->info['audio']['compression_ratio'] = $this->info['audio']['bitrate'] / ($this->info['audio']['channels'] * $this->info['audio']['sample_rate'] * (!empty($this->info['audio']['bits_per_sample']) ? $this->info['audio']['bits_per_sample'] : 16));
  1120. if (!empty($this->info['audio']['streams'])) {
  1121. foreach ($this->info['audio']['streams'] as $streamnumber => $streamdata) {
  1122. if (!empty($streamdata['bitrate']) && !empty($streamdata['channels']) && !empty($streamdata['sample_rate'])) {
  1123. $this->info['audio']['streams'][$streamnumber]['compression_ratio'] = $streamdata['bitrate'] / ($streamdata['channels'] * $streamdata['sample_rate'] * (!empty($streamdata['bits_per_sample']) ? $streamdata['bits_per_sample'] : 16));
  1124. }
  1125. }
  1126. }
  1127. return true;
  1128. }
  1129. function CalculateReplayGain() {
  1130. if (isset($this->info['replay_gain'])) {
  1131. $this->info['replay_gain']['reference_volume'] = 89;
  1132. if (isset($this->info['replay_gain']['track']['adjustment'])) {
  1133. $this->info['replay_gain']['track']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['track']['adjustment'];
  1134. }
  1135. if (isset($this->info['replay_gain']['album']['adjustment'])) {
  1136. $this->info['replay_gain']['album']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['album']['adjustment'];
  1137. }
  1138. if (isset($this->info['replay_gain']['track']['peak'])) {
  1139. $this->info['replay_gain']['track']['max_noclip_gain'] = 0 - getid3_lib::RGADamplitude2dB($this->info['replay_gain']['track']['peak']);
  1140. }
  1141. if (isset($this->info['replay_gain']['album']['peak'])) {
  1142. $this->info['replay_gain']['album']['max_noclip_gain'] = 0 - getid3_lib::RGADamplitude2dB($this->info['replay_gain']['album']['peak']);
  1143. }
  1144. }
  1145. return true;
  1146. }
  1147. function ProcessAudioStreams() {
  1148. if (!empty($this->info['audio']['bitrate']) || !empty($this->info['audio']['channels']) || !empty($this->info['audio']['sample_rate'])) {
  1149. if (!isset($this->info['audio']['streams'])) {
  1150. foreach ($this->info['audio'] as $key => $value) {
  1151. if ($key != 'streams') {
  1152. $this->info['audio']['streams'][0][$key] = $value;
  1153. }
  1154. }
  1155. }
  1156. }
  1157. return true;
  1158. }
  1159. function getid3_tempnam() {
  1160. return tempnam($this->tempdir, 'gI3');
  1161. }
  1162. }
  1163. ?>