PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/getid3/module.audio-video.swf.php

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