3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
8 // see readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
11 // module.audio-video.swf.php //
12 // module for analyzing Shockwave Flash files //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
18 class getid3_swf
extends getid3_handler
20 public $ReturnAllTagData = false;
25 public function Analyze() {
26 $info = &$this->getid3
->info
;
28 $info['fileformat'] = 'swf';
29 $info['video']['dataformat'] = 'swf';
31 // http://www.openswf.org/spec/SWFfileformat.html
33 $this->fseek($info['avdataoffset']);
35 $SWFfileData = $this->fread($info['avdataend'] - $info['avdataoffset']); // 8 + 2 + 2 + max(9) bytes NOT including Frame_Size RECT data
37 $info['swf']['header']['signature'] = substr($SWFfileData, 0, 3);
38 switch ($info['swf']['header']['signature']) {
40 $info['swf']['header']['compressed'] = false;
44 $info['swf']['header']['compressed'] = true;
48 $this->error('Expecting "FWS" or "CWS" at offset '.$info['avdataoffset'].', found "'.getid3_lib
::PrintHexBytes($info['swf']['header']['signature']).'"');
50 unset($info['fileformat']);
54 $info['swf']['header']['version'] = getid3_lib
::LittleEndian2Int(substr($SWFfileData, 3, 1));
55 $info['swf']['header']['length'] = getid3_lib
::LittleEndian2Int(substr($SWFfileData, 4, 4));
57 if ($info['swf']['header']['compressed']) {
58 $SWFHead = substr($SWFfileData, 0, 8);
59 $SWFfileData = substr($SWFfileData, 8);
60 if ($decompressed = @gzuncompress
($SWFfileData)) {
61 $SWFfileData = $SWFHead.$decompressed;
63 $this->error('Error decompressing compressed SWF data ('.strlen($SWFfileData).' bytes compressed, should be '.($info['swf']['header']['length'] - 8).' bytes uncompressed)');
68 $FrameSizeBitsPerValue = (ord(substr($SWFfileData, 8, 1)) & 0xF8) >> 3;
69 $FrameSizeDataLength = ceil((5 +
(4 * $FrameSizeBitsPerValue)) / 8);
70 $FrameSizeDataString = str_pad(decbin(ord(substr($SWFfileData, 8, 1)) & 0x07), 3, '0', STR_PAD_LEFT
);
71 for ($i = 1; $i < $FrameSizeDataLength; $i++
) {
72 $FrameSizeDataString .= str_pad(decbin(ord(substr($SWFfileData, 8 +
$i, 1))), 8, '0', STR_PAD_LEFT
);
74 list($X1, $X2, $Y1, $Y2) = explode("\n", wordwrap($FrameSizeDataString, $FrameSizeBitsPerValue, "\n", 1));
75 $info['swf']['header']['frame_width'] = getid3_lib
::Bin2Dec($X2);
76 $info['swf']['header']['frame_height'] = getid3_lib
::Bin2Dec($Y2);
78 // http://www-lehre.informatik.uni-osnabrueck.de/~fbstark/diplom/docs/swf/Flash_Uncovered.htm
79 // Next in the header is the frame rate, which is kind of weird.
80 // It is supposed to be stored as a 16bit integer, but the first byte
81 // (or last depending on how you look at it) is completely ignored.
82 // Example: 0x000C -> 0x0C -> 12 So the frame rate is 12 fps.
84 // Byte at (8 + $FrameSizeDataLength) is always zero and ignored
85 $info['swf']['header']['frame_rate'] = getid3_lib
::LittleEndian2Int(substr($SWFfileData, 9 +
$FrameSizeDataLength, 1));
86 $info['swf']['header']['frame_count'] = getid3_lib
::LittleEndian2Int(substr($SWFfileData, 10 +
$FrameSizeDataLength, 2));
88 $info['video']['frame_rate'] = $info['swf']['header']['frame_rate'];
89 $info['video']['resolution_x'] = intval(round($info['swf']['header']['frame_width'] / 20));
90 $info['video']['resolution_y'] = intval(round($info['swf']['header']['frame_height'] / 20));
91 $info['video']['pixel_aspect_ratio'] = (float) 1;
93 if (($info['swf']['header']['frame_count'] > 0) && ($info['swf']['header']['frame_rate'] > 0)) {
94 $info['playtime_seconds'] = $info['swf']['header']['frame_count'] / $info['swf']['header']['frame_rate'];
96 //echo __LINE__.'='.number_format(microtime(true) - $start_time, 3).'<br>';
101 $CurrentOffset = 12 +
$FrameSizeDataLength;
102 $SWFdataLength = strlen($SWFfileData);
104 while ($CurrentOffset < $SWFdataLength) {
105 //echo __LINE__.'='.number_format(microtime(true) - $start_time, 3).'<br>';
107 $TagIDTagLength = getid3_lib
::LittleEndian2Int(substr($SWFfileData, $CurrentOffset, 2));
108 $TagID = ($TagIDTagLength & 0xFFFC) >> 6;
109 $TagLength = ($TagIDTagLength & 0x003F);
111 if ($TagLength == 0x3F) {
112 $TagLength = getid3_lib
::LittleEndian2Int(substr($SWFfileData, $CurrentOffset, 4));
117 $TagData['offset'] = $CurrentOffset;
118 $TagData['size'] = $TagLength;
119 $TagData['id'] = $TagID;
120 $TagData['data'] = substr($SWFfileData, $CurrentOffset, $TagLength);
122 case 0: // end of movie
125 case 9: // Set background color
126 //$info['swf']['tags'][] = $TagData;
127 $info['swf']['bgcolor'] = strtoupper(str_pad(dechex(getid3_lib
::BigEndian2Int($TagData['data'])), 6, '0', STR_PAD_LEFT
));
131 if ($this->ReturnAllTagData
) {
132 $info['swf']['tags'][] = $TagData;
137 $CurrentOffset +
= $TagLength;