module.audio-video.swf.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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-video.swf.php //
  11. // module for analyzing Shockwave Flash files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_swf
  16. {
  17. function getid3_swf(&$fd, &$ThisFileInfo, $ReturnAllTagData=false) {
  18. //$start_time = microtime(true);
  19. $ThisFileInfo['fileformat'] = 'swf';
  20. $ThisFileInfo['video']['dataformat'] = 'swf';
  21. // http://www.openswf.org/spec/SWFfileformat.html
  22. fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
  23. $SWFfileData = fread($fd, $ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']); // 8 + 2 + 2 + max(9) bytes NOT including Frame_Size RECT data
  24. $ThisFileInfo['swf']['header']['signature'] = substr($SWFfileData, 0, 3);
  25. switch ($ThisFileInfo['swf']['header']['signature']) {
  26. case 'FWS':
  27. $ThisFileInfo['swf']['header']['compressed'] = false;
  28. break;
  29. case 'CWS':
  30. $ThisFileInfo['swf']['header']['compressed'] = true;
  31. break;
  32. default:
  33. $ThisFileInfo['error'][] = 'Expecting "FWS" or "CWS" at offset '.$ThisFileInfo['avdataoffset'].', found "'.$ThisFileInfo['swf']['header']['signature'].'"';
  34. unset($ThisFileInfo['swf']);
  35. unset($ThisFileInfo['fileformat']);
  36. return false;
  37. break;
  38. }
  39. $ThisFileInfo['swf']['header']['version'] = getid3_lib::LittleEndian2Int(substr($SWFfileData, 3, 1));
  40. $ThisFileInfo['swf']['header']['length'] = getid3_lib::LittleEndian2Int(substr($SWFfileData, 4, 4));
  41. //echo __LINE__.'='.number_format(microtime(true) - $start_time, 3).'<br>';
  42. if ($ThisFileInfo['swf']['header']['compressed']) {
  43. $SWFHead = substr($SWFfileData, 0, 8);
  44. $SWFfileData = substr($SWFfileData, 8);
  45. if ($decompressed = @gzuncompress($SWFfileData)) {
  46. $SWFfileData = $SWFHead.$decompressed;
  47. } else {
  48. $ThisFileInfo['error'][] = 'Error decompressing compressed SWF data ('.strlen($SWFfileData).' bytes compressed, should be '.($ThisFileInfo['swf']['header']['length'] - 8).' bytes uncompressed)';
  49. return false;
  50. }
  51. }
  52. //echo __LINE__.'='.number_format(microtime(true) - $start_time, 3).'<br>';
  53. $FrameSizeBitsPerValue = (ord(substr($SWFfileData, 8, 1)) & 0xF8) >> 3;
  54. $FrameSizeDataLength = ceil((5 + (4 * $FrameSizeBitsPerValue)) / 8);
  55. $FrameSizeDataString = str_pad(decbin(ord(substr($SWFfileData, 8, 1)) & 0x07), 3, '0', STR_PAD_LEFT);
  56. for ($i = 1; $i < $FrameSizeDataLength; $i++) {
  57. $FrameSizeDataString .= str_pad(decbin(ord(substr($SWFfileData, 8 + $i, 1))), 8, '0', STR_PAD_LEFT);
  58. }
  59. list($X1, $X2, $Y1, $Y2) = explode("\n", wordwrap($FrameSizeDataString, $FrameSizeBitsPerValue, "\n", 1));
  60. $ThisFileInfo['swf']['header']['frame_width'] = getid3_lib::Bin2Dec($X2);
  61. $ThisFileInfo['swf']['header']['frame_height'] = getid3_lib::Bin2Dec($Y2);
  62. // http://www-lehre.informatik.uni-osnabrueck.de/~fbstark/diplom/docs/swf/Flash_Uncovered.htm
  63. // Next in the header is the frame rate, which is kind of weird.
  64. // It is supposed to be stored as a 16bit integer, but the first byte
  65. // (or last depending on how you look at it) is completely ignored.
  66. // Example: 0x000C -> 0x0C -> 12 So the frame rate is 12 fps.
  67. // Byte at (8 + $FrameSizeDataLength) is always zero and ignored
  68. $ThisFileInfo['swf']['header']['frame_rate'] = getid3_lib::LittleEndian2Int(substr($SWFfileData, 9 + $FrameSizeDataLength, 1));
  69. $ThisFileInfo['swf']['header']['frame_count'] = getid3_lib::LittleEndian2Int(substr($SWFfileData, 10 + $FrameSizeDataLength, 2));
  70. $ThisFileInfo['video']['frame_rate'] = $ThisFileInfo['swf']['header']['frame_rate'];
  71. $ThisFileInfo['video']['resolution_x'] = intval(round($ThisFileInfo['swf']['header']['frame_width'] / 20));
  72. $ThisFileInfo['video']['resolution_y'] = intval(round($ThisFileInfo['swf']['header']['frame_height'] / 20));
  73. $ThisFileInfo['video']['pixel_aspect_ratio'] = (float) 1;
  74. if (($ThisFileInfo['swf']['header']['frame_count'] > 0) && ($ThisFileInfo['swf']['header']['frame_rate'] > 0)) {
  75. $ThisFileInfo['playtime_seconds'] = $ThisFileInfo['swf']['header']['frame_count'] / $ThisFileInfo['swf']['header']['frame_rate'];
  76. }
  77. //echo __LINE__.'='.number_format(microtime(true) - $start_time, 3).'<br>';
  78. // SWF tags
  79. $CurrentOffset = 12 + $FrameSizeDataLength;
  80. $SWFdataLength = strlen($SWFfileData);
  81. while ($CurrentOffset < $SWFdataLength) {
  82. //echo __LINE__.'='.number_format(microtime(true) - $start_time, 3).'<br>';
  83. $TagIDTagLength = getid3_lib::LittleEndian2Int(substr($SWFfileData, $CurrentOffset, 2));
  84. $TagID = ($TagIDTagLength & 0xFFFC) >> 6;
  85. $TagLength = ($TagIDTagLength & 0x003F);
  86. $CurrentOffset += 2;
  87. if ($TagLength == 0x3F) {
  88. $TagLength = getid3_lib::LittleEndian2Int(substr($SWFfileData, $CurrentOffset, 4));
  89. $CurrentOffset += 4;
  90. }
  91. unset($TagData);
  92. $TagData['offset'] = $CurrentOffset;
  93. $TagData['size'] = $TagLength;
  94. $TagData['id'] = $TagID;
  95. $TagData['data'] = substr($SWFfileData, $CurrentOffset, $TagLength);
  96. switch ($TagID) {
  97. case 0: // end of movie
  98. break 2;
  99. case 9: // Set background color
  100. //$ThisFileInfo['swf']['tags'][] = $TagData;
  101. $ThisFileInfo['swf']['bgcolor'] = strtoupper(str_pad(dechex(getid3_lib::BigEndian2Int($TagData['data'])), 6, '0', STR_PAD_LEFT));
  102. break;
  103. default:
  104. if ($ReturnAllTagData) {
  105. $ThisFileInfo['swf']['tags'][] = $TagData;
  106. }
  107. break;
  108. }
  109. $CurrentOffset += $TagLength;
  110. }
  111. return true;
  112. }
  113. }
  114. ?>