PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Molinos/Metadata/getid3/getid3.php

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