module.audio.shorten.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // also https://github.com/JamesHeinrich/getID3 //
  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. public function Analyze() {
  19. $info = &$this->getid3->info;
  20. $this->fseek($info['avdataoffset']);
  21. $ShortenHeader = $this->fread(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. $this->fseek($info['avdataend'] - 12);
  33. $SeekTableSignatureTest = $this->fread(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. $this->fseek($info['shn']['seektable']['offset']);
  39. $SeekTableMagic = $this->fread(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 = $this->fread($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::parseWAVEFORMATex(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. }