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

/libraries/getid3/getid3/module.audio.shorten.php

https://bitbucket.org/pbwebdev/podcast-suite
PHP | 184 lines | 102 code | 16 blank | 66 comment | 11 complexity | 93c7519cae29324ea7024de458a94fd5 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. defined( '_JEXEC' ) or die;
  3. /////////////////////////////////////////////////////////////////
  4. /// getID3() by James Heinrich <info@getid3.org> //
  5. // available at http://getid3.sourceforge.net //
  6. // or http://www.getid3.org //
  7. /////////////////////////////////////////////////////////////////
  8. // See readme.txt for more details //
  9. /////////////////////////////////////////////////////////////////
  10. // //
  11. // module.audio.shorten.php //
  12. // module for analyzing Shorten Audio files //
  13. // dependencies: NONE //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. class getid3_shorten extends getid3_handler
  17. {
  18. function Analyze() {
  19. $info = &$this->getid3->info;
  20. fseek($this->getid3->fp, $info['avdataoffset'], SEEK_SET);
  21. $ShortenHeader = fread($this->getid3->fp, 8);
  22. $magic = 'ajkg';
  23. if (substr($ShortenHeader, 0, 4) != $magic) {
  24. $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($ShortenHeader, 0, 4)).'"';
  25. return false;
  26. }
  27. $info['fileformat'] = 'shn';
  28. $info['audio']['dataformat'] = 'shn';
  29. $info['audio']['lossless'] = true;
  30. $info['audio']['bitrate_mode'] = 'vbr';
  31. $info['shn']['version'] = getid3_lib::LittleEndian2Int(substr($ShortenHeader, 4, 1));
  32. fseek($this->getid3->fp, $info['avdataend'] - 12, SEEK_SET);
  33. $SeekTableSignatureTest = fread($this->getid3->fp, 12);
  34. $info['shn']['seektable']['present'] = (bool) (substr($SeekTableSignatureTest, 4, 8) == 'SHNAMPSK');
  35. if ($info['shn']['seektable']['present']) {
  36. $info['shn']['seektable']['length'] = getid3_lib::LittleEndian2Int(substr($SeekTableSignatureTest, 0, 4));
  37. $info['shn']['seektable']['offset'] = $info['avdataend'] - $info['shn']['seektable']['length'];
  38. fseek($this->getid3->fp, $info['shn']['seektable']['offset'], SEEK_SET);
  39. $SeekTableMagic = fread($this->getid3->fp, 4);
  40. $magic = 'SEEK';
  41. if ($SeekTableMagic != $magic) {
  42. $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['shn']['seektable']['offset'].', found "'.getid3_lib::PrintHexBytes($SeekTableMagic).'"';
  43. return false;
  44. } else {
  45. // typedef struct tag_TSeekEntry
  46. // {
  47. // unsigned long SampleNumber;
  48. // unsigned long SHNFileByteOffset;
  49. // unsigned long SHNLastBufferReadPosition;
  50. // unsigned short SHNByteGet;
  51. // unsigned short SHNBufferOffset;
  52. // unsigned short SHNFileBitOffset;
  53. // unsigned long SHNGBuffer;
  54. // unsigned short SHNBitShift;
  55. // long CBuf0[3];
  56. // long CBuf1[3];
  57. // long Offset0[4];
  58. // long Offset1[4];
  59. // }TSeekEntry;
  60. $SeekTableData = fread($this->getid3->fp, $info['shn']['seektable']['length'] - 16);
  61. $info['shn']['seektable']['entry_count'] = floor(strlen($SeekTableData) / 80);
  62. //$info['shn']['seektable']['entries'] = array();
  63. //$SeekTableOffset = 0;
  64. //for ($i = 0; $i < $info['shn']['seektable']['entry_count']; $i++) {
  65. // $SeekTableEntry['sample_number'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
  66. // $SeekTableOffset += 4;
  67. // $SeekTableEntry['shn_file_byte_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
  68. // $SeekTableOffset += 4;
  69. // $SeekTableEntry['shn_last_buffer_read_position'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
  70. // $SeekTableOffset += 4;
  71. // $SeekTableEntry['shn_byte_get'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
  72. // $SeekTableOffset += 2;
  73. // $SeekTableEntry['shn_buffer_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
  74. // $SeekTableOffset += 2;
  75. // $SeekTableEntry['shn_file_bit_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
  76. // $SeekTableOffset += 2;
  77. // $SeekTableEntry['shn_gbuffer'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
  78. // $SeekTableOffset += 4;
  79. // $SeekTableEntry['shn_bit_shift'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
  80. // $SeekTableOffset += 2;
  81. // for ($j = 0; $j < 3; $j++) {
  82. // $SeekTableEntry['cbuf0'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
  83. // $SeekTableOffset += 4;
  84. // }
  85. // for ($j = 0; $j < 3; $j++) {
  86. // $SeekTableEntry['cbuf1'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
  87. // $SeekTableOffset += 4;
  88. // }
  89. // for ($j = 0; $j < 4; $j++) {
  90. // $SeekTableEntry['offset0'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
  91. // $SeekTableOffset += 4;
  92. // }
  93. // for ($j = 0; $j < 4; $j++) {
  94. // $SeekTableEntry['offset1'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
  95. // $SeekTableOffset += 4;
  96. // }
  97. //
  98. // $info['shn']['seektable']['entries'][] = $SeekTableEntry;
  99. //}
  100. }
  101. }
  102. if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
  103. $info['error'][] = 'PHP running in Safe Mode - backtick operator not available, cannot run shntool to analyze Shorten files';
  104. return false;
  105. }
  106. if (GETID3_OS_ISWINDOWS) {
  107. $RequiredFiles = array('shorten.exe', 'cygwin1.dll', 'head.exe');
  108. foreach ($RequiredFiles as $required_file) {
  109. if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
  110. $info['error'][] = GETID3_HELPERAPPSDIR.$required_file.' does not exist';
  111. return false;
  112. }
  113. }
  114. $commandline = GETID3_HELPERAPPSDIR.'shorten.exe -x "'.$info['filenamepath'].'" - | '.GETID3_HELPERAPPSDIR.'head.exe -c 64';
  115. $commandline = str_replace('/', '\\', $commandline);
  116. } else {
  117. static $shorten_present;
  118. if (!isset($shorten_present)) {
  119. $shorten_present = file_exists('/usr/local/bin/shorten') || `which shorten`;
  120. }
  121. if (!$shorten_present) {
  122. $info['error'][] = 'shorten binary was not found in path or /usr/local/bin';
  123. return false;
  124. }
  125. $commandline = (file_exists('/usr/local/bin/shorten') ? '/usr/local/bin/' : '' ) . 'shorten -x '.escapeshellarg($info['filenamepath']).' - | head -c 64';
  126. }
  127. $output = `$commandline`;
  128. if (!empty($output) && (substr($output, 12, 4) == 'fmt ')) {
  129. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
  130. $fmt_size = getid3_lib::LittleEndian2Int(substr($output, 16, 4));
  131. $DecodedWAVFORMATEX = getid3_riff::RIFFparseWAVEFORMATex(substr($output, 20, $fmt_size));
  132. $info['audio']['channels'] = $DecodedWAVFORMATEX['channels'];
  133. $info['audio']['bits_per_sample'] = $DecodedWAVFORMATEX['bits_per_sample'];
  134. $info['audio']['sample_rate'] = $DecodedWAVFORMATEX['sample_rate'];
  135. if (substr($output, 20 + $fmt_size, 4) == 'data') {
  136. $info['playtime_seconds'] = getid3_lib::LittleEndian2Int(substr($output, 20 + 4 + $fmt_size, 4)) / $DecodedWAVFORMATEX['raw']['nAvgBytesPerSec'];
  137. } else {
  138. $info['error'][] = 'shorten failed to decode DATA chunk to expected location, cannot determine playtime';
  139. return false;
  140. }
  141. $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) / $info['playtime_seconds']) * 8;
  142. } else {
  143. $info['error'][] = 'shorten failed to decode file to WAV for parsing';
  144. return false;
  145. }
  146. return true;
  147. }
  148. }
  149. ?>