module.audio.shorten.php 7.6 KB

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