PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/zina/extras/getid3/getid3.php

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