module.audio.shorten.php 7.5 KB

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