ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
module.audio-video.flv.php
Go to the documentation of this file.
1 <?php
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // //
7 // FLV module by Seth Kaufman <seth@whirl-i-gig.com> //
8 // //
9 // * version 0.1 (26 June 2005) //
10 // //
11 // minor modifications by James Heinrich <info@getid3.org> //
12 // * version 0.1.1 (15 July 2005) //
13 // //
14 // Support for On2 VP6 codec and meta information by //
15 // Steve Webster <steve.webster@featurecreep.com> //
16 // * version 0.2 (22 February 2006) //
17 // //
18 // Modified to not read entire file into memory //
19 // by James Heinrich <info@getid3.org> //
20 // * version 0.3 (15 June 2006) //
21 // //
23 // //
24 // module.audio-video.flv.php //
25 // module for analyzing Shockwave Flash Video files //
26 // dependencies: NONE //
27 // ///
29 
30 define('GETID3_FLV_TAG_AUDIO', 8);
31 define('GETID3_FLV_TAG_VIDEO', 9);
32 define('GETID3_FLV_TAG_META', 18);
33 
34 define('GETID3_FLV_VIDEO_H263', 2);
35 define('GETID3_FLV_VIDEO_SCREEN', 3);
36 define('GETID3_FLV_VIDEO_VP6', 4);
37 
39 {
40 
41  function getid3_flv(&$fd, &$ThisFileInfo, $ReturnAllTagData=false) {
42  fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
43 
44  $FLVdataLength = $ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset'];
45  $FLVheader = fread($fd, 5);
46 
47  $ThisFileInfo['fileformat'] = 'flv';
48  $ThisFileInfo['flv']['header']['signature'] = substr($FLVheader, 0, 3);
49  $ThisFileInfo['flv']['header']['version'] = getid3_lib::BigEndian2Int(substr($FLVheader, 3, 1));
50  $TypeFlags = getid3_lib::BigEndian2Int(substr($FLVheader, 4, 1));
51 
52  if ($ThisFileInfo['flv']['header']['signature'] != 'FLV') {
53  $ThisFileInfo['error'][] = 'Expecting "FLV" at offset '.$ThisFileInfo['avdataoffset'].', found "'.$ThisFileInfo['flv']['header']['signature'].'"';
54  unset($ThisFileInfo['flv']);
55  unset($ThisFileInfo['fileformat']);
56  return false;
57  }
58 
59  $ThisFileInfo['flv']['header']['hasAudio'] = (bool) ($TypeFlags & 0x04);
60  $ThisFileInfo['flv']['header']['hasVideo'] = (bool) ($TypeFlags & 0x01);
61 
62  $FrameSizeDataLength = getid3_lib::BigEndian2Int(fread($fd, 4));
63  $FLVheaderFrameLength = 9;
64  if ($FrameSizeDataLength > $FLVheaderFrameLength) {
65  fseek($fd, $FrameSizeDataLength - $FLVheaderFrameLength, SEEK_CUR);
66  }
67 
68  $Duration = 0;
69  while ((ftell($fd) + 1) < $ThisFileInfo['avdataend']) {
70  //if (!$ThisFileInfo['flv']['header']['hasAudio'] || isset($ThisFileInfo['flv']['audio']['audioFormat'])) {
71  // if (!$ThisFileInfo['flv']['header']['hasVideo'] || isset($ThisFileInfo['flv']['video']['videoCodec'])) {
72  // break;
73  // }
74  //}
75 
76  $ThisTagHeader = fread($fd, 16);
77 
78  $PreviousTagLength = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 0, 4));
79  $TagType = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 4, 1));
80  $DataLength = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 5, 3));
81  $Timestamp = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 8, 3));
82  $LastHeaderByte = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 15, 1));
83  $NextOffset = ftell($fd) - 1 + $DataLength;
84 
85  switch ($TagType) {
87  if (!isset($ThisFileInfo['flv']['audio']['audioFormat'])) {
88  $ThisFileInfo['flv']['audio']['audioFormat'] = $LastHeaderByte & 0x07;
89  $ThisFileInfo['flv']['audio']['audioRate'] = ($LastHeaderByte & 0x30) / 0x10;
90  $ThisFileInfo['flv']['audio']['audioSampleSize'] = ($LastHeaderByte & 0x40) / 0x40;
91  $ThisFileInfo['flv']['audio']['audioType'] = ($LastHeaderByte & 0x80) / 0x80;
92  }
93  break;
94 
96  if (!isset($ThisFileInfo['flv']['video']['videoCodec'])) {
97  $ThisFileInfo['flv']['video']['videoCodec'] = $LastHeaderByte & 0x07;
98 
99  $FLVvideoHeader = fread($fd, 11);
100 
101  if ($ThisFileInfo['flv']['video']['videoCodec'] != GETID3_FLV_VIDEO_VP6) {
102 
103  $PictureSizeType = (getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 3, 2))) >> 7;
104  $PictureSizeType = $PictureSizeType & 0x0007;
105  $ThisFileInfo['flv']['header']['videoSizeType'] = $PictureSizeType;
106  switch ($PictureSizeType) {
107  case 0:
108  $PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 2));
109  $PictureSizeEnc <<= 1;
110  $ThisFileInfo['video']['resolution_x'] = ($PictureSizeEnc & 0xFF00) >> 8;
111  $PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 2));
112  $PictureSizeEnc <<= 1;
113  $ThisFileInfo['video']['resolution_y'] = ($PictureSizeEnc & 0xFF00) >> 8;
114  break;
115 
116  case 1:
117  $PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 4));
118  $PictureSizeEnc <<= 1;
119  $ThisFileInfo['video']['resolution_x'] = ($PictureSizeEnc & 0xFFFF0000) >> 16;
120 
121  $PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 7, 4));
122  $PictureSizeEnc <<= 1;
123  $ThisFileInfo['video']['resolution_y'] = ($PictureSizeEnc & 0xFFFF0000) >> 16;
124  break;
125 
126  case 2:
127  $ThisFileInfo['video']['resolution_x'] = 352;
128  $ThisFileInfo['video']['resolution_y'] = 288;
129  break;
130 
131  case 3:
132  $ThisFileInfo['video']['resolution_x'] = 176;
133  $ThisFileInfo['video']['resolution_y'] = 144;
134  break;
135 
136  case 4:
137  $ThisFileInfo['video']['resolution_x'] = 128;
138  $ThisFileInfo['video']['resolution_y'] = 96;
139  break;
140 
141  case 5:
142  $ThisFileInfo['video']['resolution_x'] = 320;
143  $ThisFileInfo['video']['resolution_y'] = 240;
144  break;
145 
146  case 6:
147  $ThisFileInfo['video']['resolution_x'] = 160;
148  $ThisFileInfo['video']['resolution_y'] = 120;
149  break;
150 
151  default:
152  $ThisFileInfo['video']['resolution_x'] = 0;
153  $ThisFileInfo['video']['resolution_y'] = 0;
154  break;
155 
156  }
157  }
158  }
159  break;
160 
161  // Meta tag
162  case GETID3_FLV_TAG_META:
163 
164  fseek($fd, -1, SEEK_CUR);
165  $reader = new AMFReader(new AMFStream(fread($fd, $DataLength)));
166  $eventName = $reader->readData();
167  $ThisFileInfo['meta'][$eventName] = $reader->readData();
168  unset($reader);
169 
170  $ThisFileInfo['video']['frame_rate'] = $ThisFileInfo['meta']['onMetaData']['framerate'];
171  $ThisFileInfo['video']['resolution_x'] = $ThisFileInfo['meta']['onMetaData']['width'];
172  $ThisFileInfo['video']['resolution_y'] = $ThisFileInfo['meta']['onMetaData']['height'];
173  break;
174 
175  default:
176  // noop
177  break;
178  }
179 
180  if ($Timestamp > $Duration) {
181  $Duration = $Timestamp;
182  }
183 
184  fseek($fd, $NextOffset, SEEK_SET);
185  }
186 
187  $ThisFileInfo['playtime_seconds'] = $Duration / 1000;
188  $ThisFileInfo['bitrate'] = ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) / $ThisFileInfo['playtime_seconds'];
189 
190  if ($ThisFileInfo['flv']['header']['hasAudio']) {
191  $ThisFileInfo['audio']['codec'] = $this->FLVaudioFormat($ThisFileInfo['flv']['audio']['audioFormat']);
192  $ThisFileInfo['audio']['sample_rate'] = $this->FLVaudioRate($ThisFileInfo['flv']['audio']['audioRate']);
193  $ThisFileInfo['audio']['bits_per_sample'] = $this->FLVaudioBitDepth($ThisFileInfo['flv']['audio']['audioSampleSize']);
194 
195  $ThisFileInfo['audio']['channels'] = $ThisFileInfo['flv']['audio']['audioType'] + 1; // 0=mono,1=stereo
196  $ThisFileInfo['audio']['lossless'] = ($ThisFileInfo['flv']['audio']['audioFormat'] ? false : true); // 0=uncompressed
197  $ThisFileInfo['audio']['dataformat'] = 'flv';
198  }
199  if (@$ThisFileInfo['flv']['header']['hasVideo']) {
200  $ThisFileInfo['video']['codec'] = $this->FLVvideoCodec($ThisFileInfo['flv']['video']['videoCodec']);
201  $ThisFileInfo['video']['dataformat'] = 'flv';
202  $ThisFileInfo['video']['lossless'] = false;
203  }
204 
205  return true;
206  }
207 
208 
209  function FLVaudioFormat($id) {
210  $FLVaudioFormat = array(
211  0 => 'uncompressed',
212  1 => 'ADPCM',
213  2 => 'mp3',
214  5 => 'Nellymoser 8kHz mono',
215  6 => 'Nellymoser',
216  );
217  return (@$FLVaudioFormat[$id] ? @$FLVaudioFormat[$id] : false);
218  }
219 
220  function FLVaudioRate($id) {
221  $FLVaudioRate = array(
222  0 => 5500,
223  1 => 11025,
224  2 => 22050,
225  3 => 44100,
226  );
227  return (@$FLVaudioRate[$id] ? @$FLVaudioRate[$id] : false);
228  }
229 
230  function FLVaudioBitDepth($id) {
231  $FLVaudioBitDepth = array(
232  0 => 8,
233  1 => 16,
234  );
235  return (@$FLVaudioBitDepth[$id] ? @$FLVaudioBitDepth[$id] : false);
236  }
237 
238  function FLVvideoCodec($id) {
239  $FLVvideoCodec = array(
240  GETID3_FLV_VIDEO_H263 => 'Sorenson H.263',
241  GETID3_FLV_VIDEO_SCREEN => 'Screen video',
242  GETID3_FLV_VIDEO_VP6 => 'On2 VP6',
243  );
244  return (@$FLVvideoCodec[$id] ? @$FLVvideoCodec[$id] : false);
245  }
246 }
247 
248 class AMFStream {
249  var $bytes;
250  var $pos;
251 
252  function AMFStream(&$bytes) {
253  $this->bytes =& $bytes;
254  $this->pos = 0;
255  }
256 
257  function readByte() {
258  return getid3_lib::BigEndian2Int(substr($this->bytes, $this->pos++, 1));
259  }
260 
261  function readInt() {
262  return ($this->readByte() << 8) + $this->readByte();
263  }
264 
265  function readLong() {
266  return ($this->readByte() << 24) + ($this->readByte() << 16) + ($this->readByte() << 8) + $this->readByte();
267  }
268 
269  function readDouble() {
270  return getid3_lib::BigEndian2Float($this->read(8));
271  }
272 
273  function readUTF() {
274  $length = $this->readInt();
275  return $this->read($length);
276  }
277 
278  function readLongUTF() {
279  $length = $this->readLong();
280  return $this->read($length);
281  }
282 
283  function read($length) {
284  $val = substr($this->bytes, $this->pos, $length);
285  $this->pos += $length;
286  return $val;
287  }
288 
289  function peekByte() {
290  $pos = $this->pos;
291  $val = $this->readByte();
292  $this->pos = $pos;
293  return $val;
294  }
295 
296  function peekInt() {
297  $pos = $this->pos;
298  $val = $this->readInt();
299  $this->pos = $pos;
300  return $val;
301  }
302 
303  function peekLong() {
304  $pos = $this->pos;
305  $val = $this->readLong();
306  $this->pos = $pos;
307  return $val;
308  }
309 
310  function peekDouble() {
311  $pos = $this->pos;
312  $val = $this->readDouble();
313  $this->pos = $pos;
314  return $val;
315  }
316 
317  function peekUTF() {
318  $pos = $this->pos;
319  $val = $this->readUTF();
320  $this->pos = $pos;
321  return $val;
322  }
323 
324  function peekLongUTF() {
325  $pos = $this->pos;
326  $val = $this->readLongUTF();
327  $this->pos = $pos;
328  return $val;
329  }
330 }
331 
332 class AMFReader {
333  var $stream;
334 
335  function AMFReader(&$stream) {
336  $this->stream =& $stream;
337  }
338 
339  function readData() {
340  $value = null;
341 
342  $type = $this->stream->readByte();
343 
344  switch($type) {
345  // Double
346  case 0:
347  $value = $this->readDouble();
348  break;
349 
350  // Boolean
351  case 1:
352  $value = $this->readBoolean();
353  break;
354 
355  // String
356  case 2:
357  $value = $this->readString();
358  break;
359 
360  // Object
361  case 3:
362  $value = $this->readObject();
363  break;
364 
365  // null
366  case 6:
367  return null;
368  break;
369 
370  // Mixed array
371  case 8:
372  $value = $this->readMixedArray();
373  break;
374 
375  // Array
376  case 10:
377  $value = $this->readArray();
378  break;
379 
380  // Date
381  case 11:
382  $value = $this->readDate();
383  break;
384 
385  // Long string
386  case 13:
387  $value = $this->readLongString();
388  break;
389 
390  // XML (handled as string)
391  case 15:
392  $value = $this->readXML();
393  break;
394 
395  // Typed object (handled as object)
396  case 16:
397  $value = $this->readTypedObject();
398  break;
399 
400  // Long string
401  default:
402  $value = '(unknown or unsupported data type)';
403  break;
404  }
405 
406  return $value;
407  }
408 
409  function readDouble() {
410  return $this->stream->readDouble();
411  }
412 
413  function readBoolean() {
414  return $this->stream->readByte() == 1;
415  }
416 
417  function readString() {
418  return $this->stream->readUTF();
419  }
420 
421  function readObject() {
422  // Get highest numerical index - ignored
423  $highestIndex = $this->stream->readLong();
424 
425  $data = array();
426 
427  while ($key = $this->stream->readUTF()) {
428  // Mixed array record ends with empty string (0x00 0x00) and 0x09
429  if (($key == '') && ($this->stream->peekByte() == 0x09)) {
430  // Consume byte
431  $this->stream->readByte();
432  break;
433  }
434 
435  $data[$key] = $this->readData();
436  }
437 
438  return $data;
439  }
440 
441  function readMixedArray() {
442  // Get highest numerical index - ignored
443  $highestIndex = $this->stream->readLong();
444 
445  $data = array();
446 
447  while ($key = $this->stream->readUTF()) {
448  // Mixed array record ends with empty string (0x00 0x00) and 0x09
449  if (($key == '') && ($this->stream->peekByte() == 0x09)) {
450  // Consume byte
451  $this->stream->readByte();
452  break;
453  }
454 
455  if (is_numeric($key)) {
456  $key = (float) $key;
457  }
458 
459  $data[$key] = $this->readData();
460  }
461 
462  return $data;
463  }
464 
465  function readArray() {
466  $length = $this->stream->readLong();
467 
468  $data = array();
469 
470  for ($i = 0; $i < count($length); $i++) {
471  $data[] = $this->readData();
472  }
473 
474  return $data;
475  }
476 
477  function readDate() {
478  $timestamp = $this->stream->readDouble();
479  $timezone = $this->stream->readInt();
480  return $timestamp;
481  }
482 
483  function readLongString() {
484  return $this->stream->readLongUTF();
485  }
486 
487  function readXML() {
488  return $this->stream->readLongUTF();
489  }
490 
491  function readTypedObject() {
492  $className = $this->stream->readUTF();
493  return $this->readObject();
494  }
495 }
496 
497 ?>