-
Notifications
You must be signed in to change notification settings - Fork 2
/
FLV.php
954 lines (788 loc) · 29.3 KB
/
FLV.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
<?php
/*
Copyright 2006 Iván Montes, Morten Hundevad
This file is part of FLV tools for PHP (FLV4PHP from now on).
FLV4PHP is free software; you can redistribute it and/or modify it under the
terms of the GNU General var License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
FLV4PHP is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General var License for more details.
You should have received a copy of the GNU General var License along with
FLV4PHP; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
define('FLV_INCLUDE_PATH', dirname(__FILE__) . '/');
define('FLV_SECRET_KEY', 'flv_key');
define('FLV_VERSION', 'V0.21');
require_once(FLV_INCLUDE_PATH . 'Tag.php');
require_once(FLV_INCLUDE_PATH . 'Util/AMFSerialize.php');
require_once(FLV_INCLUDE_PATH . 'Util/AMFUnserialize.php');
require_once(FLV_INCLUDE_PATH . 'getid3/getid3.php');
define('FLV_HEADER_SIGNATURE', 'FLV');
define('FLV_HEADER_SIZE', 9);
define('FLV_ERROR_BLOCKED', 'ERROR CODE: 1');
define('FLV_ERROR_INVALID', 'ERROR CODE: 2');
/**
* Parse a .flv file to extract all the 'tag' information
*/
class FLV {
/** The FLV header signature */
var $FLV_HEADER_SIGNATURE = FLV_HEADER_SIGNATURE;
/** The FLV main header size */
var $FLV_HEADER_SIZE = FLV_HEADER_SIZE;
/** The FLV tag header size */
var $TAG_HEADER_SIZE = FLV_TAG_HEADER_SIZE;
/**
Maximun number of bytes to process as tag body. This is a safety meassure against
corrupted FLV files.
*/
var $MAX_TAG_BODY_SIZE = FLV_TAG_MAX_BODY_SIZE;
var $filename;
var $_tempfilename;
var $metadata;
var $metadataend;
var $config;
var $_db_link_id;
var $lock_key;
// var $lock_id;
var $_fh;
var $_lastTagSize = 0;
var $_getID3 = NULL;
var $FileInfo = NULL;
var $error = NULL;
var $_nocashe = false;
var $version;
var $hasVideo;
var $hasAudio;
var $bodyOfs;
var $eof;
/**
* @param bool $fname
* @param int $timeout
*
* @return bool|true
*/function FLV( $fname = false, $timeout = 30 )
{
if($fname) return $this->open($fname, $timeout);
return false;
}
/**
*
* @param bool|string $fname Locatsion of file
* @param int $timeout Script Time out default 30, when using external ling for downloading..
*
* @return true/false if success/failed.
*/
function open( $fname = false, $timeout = 30 )
{
$this->error = array();
if($fname) {
$this->filename = $fname;
$this->setKey(basename($fname, ".flv"));
$this->_nocashe = false;
$url = parse_url($fname);
if(!empty($url['scheme'])) {
if( !is_readable(FLV_INCLUDE_PATH) || !is_writable(FLV_INCLUDE_PATH) ) {
$this->_die('To Use External files you need to have: '. FLV_INCLUDE_PATH . ' chmod:777 ');
} elseif( $this->_tempfilename = tempnam(FLV_INCLUDE_PATH, "FLV") ) {
$this->_fh = @fopen($this->_tempfilename, "r+");
if (!$this->_fh) $this->_die('Unable to Open temporary file');
$tempcontent = file_get_contents($fname);
fwrite($this->_fh, $tempcontent);
$fname = $this->_tempfilename;
rewind($this->_fh);
} else $this->_die('Unable to Make temporary file');
} else {
$this->_fh = @fopen( $fname, 'r' );
if (!$this->_fh) $this->_die('Unable to open the file');
}
$this->_getID3 = new getID3();
$this->_getID3->setOption(array('encoding' => 'UTF-8'));
$AutoGetHashes = (bool) (filesize($fname) < 52428800); // auto-get md5_data, md5_file, sha1_data, sha1_file if filesize < 50MB
$this->_getID3->setOption(array('option_md5_data' => $AutoGetHashes,'option_sha1_data' => $AutoGetHashes));
$this->FileInfo = $this->_getID3->analyze($fname);
$hdr = fread( $this->_fh, $this->FLV_HEADER_SIZE );
//check file header signature
if ( substr($hdr, 0, 3) !== $this->FLV_HEADER_SIGNATURE ) $this->_die('The header signature does not match');
$this->version = ord($hdr[3]);
$this->hasVideo = (bool)(ord($hdr[4]) & 0x01);
$this->hasAudio = (bool)(ord($hdr[4]) & 0x04);
$this->bodyOfs = (ord($hdr[5]) << 24) +
(ord($hdr[6]) << 16) +
(ord($hdr[7]) << 8) +
(ord($hdr[8]));
$this->eof = false;
$this->getMetaData();
return true;
}
return false;
}
/**
* Move to beginning of the File ( after first header )
*/
function start()
{
fseek( $this->_fh, $this->bodyOfs );
$this->eof = false;
}
/**
* Close a previously open FLV file
*/
function close()
{
fclose( $this->_fh );
if(!empty($this->_tempfilename)) unlink($this->_tempfilename);
}
/**
* Close a previously open FLV file
*/
function _die($msg)
{
$this->close();
$this->error[] = $msg;
die($msg);
}
/**
* Returns the MetaData Tag
*
* @return object return metadata object
*/
function getMetaData()
{
$this->start();
$hdr = fread( $this->_fh, $this->TAG_HEADER_SIZE );
if (strlen($hdr) < $this->TAG_HEADER_SIZE) {
$this->eof = true;
return NULL;
}
// Get the tag object by skiping the first 4 bytes which tell the previous tag size
$tag = FLV_Tag::getTag( substr( $hdr, 4 ) );
// Read at most MAX_TAG_BODY_SIZE bytes of the body
$bytesToRead = min( $this->MAX_TAG_BODY_SIZE, $tag->size );
$tag->setBody( fread( $this->_fh, $bytesToRead ) );
// Check if the tag body has to be processed
$tag->analyze();
// If the tag was skipped or the body size was larger than MAX_TAG_BODY_SIZE
if ($tag->size > $bytesToRead) fseek( $this->_fh, $tag->size-$bytesToRead, SEEK_CUR );
$this->_lastTagSize = $tag->size + $this->TAG_HEADER_SIZE - 4;
$this->metadata = $tag->data;
$this->metadataend = ftell($this->_fh);
return $tag;
}
/**
* Returns the MetaData string
*
* @param array|bool $newMetaData Array with new metadata
* @param bool|string $merge Merge original array with new one
*
* @return string New Metadata + next tag's previous size
*/
function createMetaData($newMetaData = false,$merge = true)
{
//if the metadata is pressent in the file merge it with the generated one
$amf = new FLV_Util_AMFSerialize();
if (is_array($newMetaData)) {
if($merge && is_array($this->metadata)) $newMetaData = array_merge( $this->metadata, $newMetaData );
$metadata = $newMetaData;
} else $metadata = $this->metadata;
$metadata['metadatacreator'] = 'FLV Editor for PHP '.FLV_VERSION.' (Project: Flv4php)';
$serMeta = $amf->serialize('onMetaData') . $amf->serialize($metadata);
$out = pack('N', 0); // PreviousTagSize
$out.= pack('C', FLV_TAG_TYPE_DATA); // Type
$out.= pack('Cn', "\x00", strlen($serMeta)); // BodyLength assumes it's shorter than 64Kb
$out.= pack('N', 0); // Time stamp (not used)
$out.= pack('Cn', 0, 0); // Stream ID (not used) <---- WHERE IS THIS comming from
$out.= $serMeta; // Metadata Body
$out.= pack('N', strlen($serMeta) + 1 + 3 + 4 + 3); // PreviousTagSize
return $out;
}
/**
* Play the flv and close file after
*
* @param int $limitSpeed Limit speed of downloading, calculated off videorate + audiorate + $limitSpeed
* @param int $seekat Start playback at..
* @param array|bool $newMetaData Array with new metadata
* @param bool $merge Merge original array with new one
*/
function playFlv($limitSpeed = 0,$seekat = 0,$newMetaData = false,$merge = true)
{
session_write_close();
if ($seekat != 0) {
fseek($this->_fh, $seekat);
} else {
if (!is_array($newMetaData)) $newMetaData = $this->defaultMetaData();
$metadata = $this->createMetaData($newMetaData,$merge);
rewind($this->_fh); // Rewind the movie
fseek($this->_fh, $this->metadataend+4); // Skip the Original metadata
}
if ($limitSpeed) {
if ($this->FileInfo['bitrate']) $limitSpeed = ceil(($this->FileInfo['bitrate']/100)/8)+$limitSpeed-1;
else $limitSpeed = false;
}
if (isset($metadata)) $size = strlen($metadata) + ( filesize($this->filename) - ftell($this->_fh) ) + 9;
else $size = filesize($this->filename) - ftell($this->_fh) + 9;
$this->setHeader($size);
header("Content-Disposition: filename=".basename($this->filename));
print("FLV");
print(pack('C', 1 ));
print(pack('C', 5 ));
print(pack('N', 9 ));
if ($seekat) {
print(pack('N', 9 ));
} else {
if (isset($metadata)) {
print($metadata);
}
}
set_time_limit(0);
// print(fread($this->_fh, 50000));
print(fread($this->_fh, 5000));
while(!feof($this->_fh)) {
if ($limitSpeed) {
print(fread($this->_fh, round($limitSpeed*(1024/32))));
flush();
usleep(31250);
} else {
print(fread($this->_fh, 1024));
}
}
$this->close();
}
/**
* Play the flv with lock and close file after
*
* @param int $limitSpeed Limit speed off downloading, calculated off videorate + audiorate + $limitSpeed
* @param int $seekat Start playback at..
* @param array|bool $newMetaData Array off new metadata
* @param bool $merge Merge original array with new one
* @param bool $usedb use database for key veryfying
*/
function playFlvLock($limitSpeed = 0,$seekat = 0,$newMetaData = false,$merge = true,$usedb = false)
{
$this->_nocashe = true;
if ( $this->validLock($usedb) ) {
$this->playFlv($limitSpeed,$seekat,$newMetaData,$merge);
} else {
$this->error[] = FLV_ERROR_INVALID;
$this->close();
}
}
/**
* Get Flv Thumb output's a thumb clip from offset point, locate a key frame and from there output's duration
* if no key frame is found it use the first key frame.
*
* @param int $offset Offset in ms
* @param bool $usemetadata Use metadata to attempt to find first keyframe
*
* @return string Single Flv keyframe
*/
function getFlvThumb($offset=2000,$usemetadata=true) {
session_write_close();
$this->start();
$skipTagTypes = array();
$skipTagTypes[FLV_TAG_TYPE_AUDIO] = FLV_TAG_TYPE_AUDIO;
if ($usemetadata && $offset) {
if (isset($this->metadata['keyframes']['times'])){
foreach ( $this->metadata['keyframes']['times'] as $key => $value){
if ( $value >= ($offset/1000) ) {
$offset = $value*1000;
fseek($this->_fh,$this->metadata['keyframes']['filepositions'][$key]-4);
break;
}
}
}
}
$dataOut= null;
while ($tag = $this->getTag($skipTagTypes)) {
if ( $tag->type == FLV_TAG_TYPE_VIDEO ) {
if ($tag->timestamp >= $offset && $tag->frametype == 1 ) {
rewind($this->_fh);
fseek($this->_fh, $tag->start );
$dataOut = fread($this->_fh, ( ( $tag->end + 4 ) - $tag->start ) );
$this->replaceTimestamp($dataOut,0);
break;
}
}
//Does it actually help with memory allocation?
unset($tag);
}
if(!$dataOut) {
$offset = 0;
$this->start();
while ($tag = $this->getTag($skipTagTypes)) {
if ( $tag->type == FLV_TAG_TYPE_VIDEO ) {
if ($tag->timestamp >= $offset && $tag->frametype == 1 ) {
rewind($this->_fh);
fseek($this->_fh, $tag->start);
$dataOut = fread($this->_fh, ( ( $tag->end + 4 ) - $tag->start ) );
$this->replaceTimestamp($dataOut,0);
break;
}
}
//Does it actually help with memory allocation?
unset($tag);
}
}
$newMetaData = $this->defaultMetaData(false);
unset($newMetaData['keyframes']['times']);
$newMetaData['keyframes']['times'][] = 0;
//
unset($newMetaData['keyframes']['filepositions']);
$newMetaData['keyframes']['filepositions'][] = 0;
//
$newMetaData['duration'] = $newMetaData['datasize'] = $newMetaData['audiosize'] = $newMetaData['videosize'] = $newMetaData['filesize'] = $newMetaData['audiodatarate'] = $newMetaData['audiocodecid'] = $newMetaData['lastkeyframetimestamp'] = $newMetaData['lasttimestamp'] = $newMetaData['framerate'] = 0;
$metadata = $this->createMetaData($newMetaData,true);
$sizeMetaData = strlen($metadata);
$newMetaData = array();
$newMetaData = $this->defaultMetaData(false);
unset($newMetaData['keyframes']['times']);
$newMetaData['keyframes']['times'][] = 0;
//
unset($newMetaData['keyframes']['filepositions']);
$newMetaData['keyframes']['filepositions'][] = 0;
//
$newMetaData['lastkeyframetimestamp'] = $newMetaData['lasttimestamp'] = $newMetaData['framerate'] = $newMetaData['duration'] = $newMetaData['audiosize'] = $newMetaData['audiodatarate'] = $newMetaData['audiocodecid'] = 0;
$newMetaData['datasize'] = $sizeMetaData;
$newMetaData['videosize'] = strlen($dataOut);
$newMetaData['filesize'] = $sizeMetaData + strlen($dataOut) + FLV_HEADER_SIZE;
$metadata = $this->createMetaData($newMetaData,true);
$this->setHeader(strlen($metadata) + strlen($dataOut) + FLV_HEADER_SIZE);
header("Content-Disposition: filename=".basename($this->filename));
$this->close();
return "FLV".pack('C', 1 ).pack('C', 1 ).pack('N', 9 ).$metadata.$dataOut;
}
/**
* Get Flv Preview output's a Preview clip from offset point, locate a key frame and from there output's duration
* if no key frame is found it use the first key frame.
*
* @param int $offset Offset in ms
* @param int $duration Duration in ms
* @param bool $usemetadata Use metadata to attempt to find first keyframe
* @param int $speedModifyer Playback Speed modifier
*
* @return string Flv File preview
*/
function getFlvPreview($offset=2000,$duration=2000,$usemetadata=true,$speedModifyer=100) {
session_write_close();
$speedModifier = (int) $speedModifyer;
if ($speedModifier < 1) $speedModifier = 1;
$dataArray = array();
$this->start();
$skipTagTypes = array();
$skipTagTypes[FLV_TAG_TYPE_AUDIO] = FLV_TAG_TYPE_AUDIO;
if($usemetadata && $offset) {
if (isset($this->metadata['keyframes']['times'])){
foreach($this->metadata['keyframes']['times'] as $key => $value){
if($value >= ($offset/1000)) {
$offset = $value*1000;
fseek($this->_fh,$this->metadata['keyframes']['filepositions'][$key]-4);
break;
}
}
}
}
$endFouned = false;
$index = $lastKeyframe = $startTimestamp = $endTimestamp = 0;
while($tag = $this->getTag($skipTagTypes)) {
if($tag->type == FLV_TAG_TYPE_VIDEO) {
if(empty($dataArray) && $tag->timestamp >= $offset && $tag->frametype == 1 ) {
$startTimestamp = $tag->timestamp;
rewind($this->_fh);
fseek($this->_fh, $tag->start);
$dataArray[$index++] = fread($this->_fh, ($tag->end - $tag->start));
$this->replaceTimestamp($dataArray[$index-1],0);
rewind($this->_fh);
fseek($this->_fh, $tag->end);
} elseif($dataArray) {
$endTimestamp = $this->modifyTimestamp(($tag->timestamp-$startTimestamp),$speedModifyer);
if($tag->frametype == 1) $lastKeyframe = $endTimestamp;
rewind($this->_fh);
fseek($this->_fh, $tag->start);
$dataArray[$index++] = fread($this->_fh, ($tag->end - $tag->start));
$this->replaceTimestamp($dataArray[$index-1],$endTimestamp);
rewind($this->_fh);
fseek($this->_fh, $tag->end);
if($tag->timestamp >= ($duration+$startTimestamp)) {
$endFouned = true;
break;
}
}
if(!$dataArray && $tag->timestamp >= ($offset+$duration)) break;
}
//Does it actually help with memory allocation?
unset($tag);
}
if(!$endFouned) {
$dataArray = array();
$index = $offset = $lastKeyframe = $startTimestamp = $endTimestamp = 0;
$this->start();
while($tag = $this->getTag($skipTagTypes)) {
if($tag->type == FLV_TAG_TYPE_VIDEO) {
if(empty($dataArray) && $tag->timestamp >= $offset && $tag->frametype == 1 ) {
$startTimestamp = $tag->timestamp;
rewind($this->_fh);
fseek($this->_fh, $tag->start);
$dataArray[$index++] = fread($this->_fh, ($tag->end - $tag->start));
$this->replaceTimestamp($dataArray[$index-1],0);
rewind($this->_fh);
fseek($this->_fh, $tag->end);
} elseif($dataArray) {
$endTimestamp = $this->modifyTimestamp(($tag->timestamp-$startTimestamp),$speedModifyer);
if($tag->frametype == 1) $lastKeyframe = $endTimestamp;
rewind($this->_fh);
fseek($this->_fh,$tag->start);
$dataArray[$index++] = fread($this->_fh, ($tag->end - $tag->start));
$this->replaceTimestamp($dataArray[$index-1],$endTimestamp);
rewind($this->_fh);
fseek($this->_fh, $tag->end);
if ($tag->timestamp >= ($duration+$startTimestamp)) break;
}
}
//Does it actually help with memory allocation?
unset($tag);
}
}
$dataOut = '';
$lastSize = 0;
foreach($dataArray as $key => $value) {
if($key) $dataOut = $dataOut.pack('N', $lastSize ).$value;
else $dataOut = $value;
$lastSize = strlen($value);
}
$newMetaData = array();
$newMetaData = $this->defaultMetaData(true);
unset($newMetaData['keyframes']['times']);
$newMetaData['keyframes']['times'][] = 0;
//
unset($newMetaData['keyframes']['filepositions']);
$newMetaData['keyframes']['filepositions'][] = 0;
$newMetaData['duration'] = ( $endTimestamp - $startTimestamp ) / 1000;
$newMetaData['datasize'] = $newMetaData['audiosize'] = $newMetaData['videosize'] = $newMetaData['filesize'] = $newMetaData['audiodatarate'] = $newMetaData['audiocodecid'] = 0;
$newMetaData['hasmetadata'] = true;
$newMetaData['haskeyframes'] = true;
$newMetaData['lastkeyframetimestamp'] = $lastKeyframe;
$newMetaData['lasttimestamp'] = ( $endTimestamp - $startTimestamp ) / 1000;
$metadata = $this->createMetaData($newMetaData,true);
$sizeMetaData = strlen($metadata);
$newMetaData = array();
$newMetaData = $this->defaultMetaData(true);
unset($newMetaData['keyframes']['times']);
$newMetaData['keyframes']['times'][] = 0;
//
unset($newMetaData['keyframes']['filepositions']);
$newMetaData['keyframes']['filepositions'][] = $sizeMetaData + FLV_HEADER_SIZE;
//
$newMetaData['duration'] = ( $endTimestamp - $startTimestamp ) / 1000;
$newMetaData['datasize'] = $sizeMetaData;
$newMetaData['audiosize'] = $newMetaData['audiodatarate'] = $newMetaData['audiocodecid'] = 0;
$newMetaData['videosize'] = strlen($dataOut);
$newMetaData['filesize'] = $sizeMetaData + strlen($dataOut) + FLV_HEADER_SIZE;
$newMetaData['hasmetadata'] = true;
$newMetaData['haskeyframes'] = true;
$newMetaData['lastkeyframetimestamp'] = $lastKeyframe;
$newMetaData['lasttimestamp'] = ( $endTimestamp - $startTimestamp ) / 1000;
$metadata = $this->createMetaData($newMetaData,true);
$this->setHeader(strlen($metadata) + strlen($dataOut) + FLV_HEADER_SIZE);
header("Content-Disposition: filename=".basename($this->filename));
$this->close();
return "FLV".pack('C', 1).pack('C', 1).pack('N', 9).$metadata.$dataOut;
}
/**
* Download Flv
*
* @param int $limitSpeed Limit speed off downloading, calculated off videorate + audiorate + $limitSpeed
* @param array|bool $newMetaData Array off new metadata
* @param bool $merge Merge original array with new one
*/
function downloadFlv($limitSpeed = 0,$newMetaData = false,$merge = true)
{
session_write_close();
$this->setHeader(filesize($this->filename));
header("Content-Disposition: attachment; filename=".basename($this->filename));
print("FLV");
print(pack('C', 1 ));
print(pack('C', 5 ));
print(pack('N', 9 ));
if(!is_array($newMetaData)) $newMetaData = $this->defaultMetaData();
print($this->createMetaData($newMetaData,$merge));
rewind($this->_fh); // Rewind the movie
fseek($this->_fh, $this->metadataend+4); // Skip the Original metadata
if($limitSpeed) {
if ($this->FileInfo['bitrate']) $limitSpeed = ceil(($this->FileInfo['bitrate']/100)/8)+$limitSpeed-1;
// if ($this->metadata["videodatarate"] || $this->metadata["audiodatarate"]) $limitSpeed = ceil(($this->metadata["videodatarate"]+$this->metadata["audiodatarate"])/8)+$limitSpeed-1;
else $limitSpeed = false;
}
set_time_limit(0);
while(!feof($this->_fh)) {
if($limitSpeed) {
print(fread($this->_fh, round($limitSpeed*(1024/32))));
flush();
usleep(31250);
} else {
print(fread($this->_fh, 1024));
}
}
$this->close();
}
/**
* Flv php header
*
* @param int $size Size of file.
*/
function setHeader($size=0)
{
header("Content-Type: video/x-flv");
// header("Content-Type: text/plain");
header("Content-Length: " .(string)$size);
if($this->_nocashe) {
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
} else {
// calc an offset of 24 hours
$offset = 3600 * 24;
// calc the string in GMT not localtime and add the offset
$expire = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
//output the HTTP header
header($expire);
}
}
/**
* Open a lock used when playing with playFlvLock.
*
* @param bool|string $key Key Name
* @param int $validtime Defines how long time a key is valid
* @param bool $usedb Use Database key storage?
*
* @return true/false if sucess/failed.
*/
function openLock($key=false,$validtime=30,$usedb=false)
{
if(!$key) return false;
if(!session_id()) session_start();
if($usedb) {
$this->loadConfig();
$sid = session_id();
$expire = time()+$validtime;
$ip = $_SERVER['REMOTE_ADDR'];
$this->dbConnect();
$result = mysql_list_tables ( $this->config['db_name'] ,$this->_db_link_id);
$tableKey = $this->config['db_table_prefix']."key";
$tableBlock = $this->config['db_table_prefix']."block";
$maketableKey = true;
$maketableBlock = true;
while($row = mysql_fetch_row($result)) {
if($row[0] == $tableKey) $maketableKey = false;
elseif($row[0] == $tableBlock) $maketableBlock = false;
}
if($maketableKey) {
$query="CREATE TABLE `".$tableKey."` ( `uid` SMALLINT( 5 ) AUTO_INCREMENT , `sid` VARCHAR( 35 ) NOT NULL , `key` VARCHAR( 50 ) NOT NULL , `timeout` INT( 20 ) NOT NULL , `ip` VARCHAR( 15 ) NOT NULL , PRIMARY KEY ( `uid` ) ) TYPE = MYISAM";
if (!mysql_query($query)) $this->_die(mysql_error());
}
if($maketableBlock) {
$query="CREATE TABLE `".$tableBlock."` (`uid` SMALLINT( 5 ) AUTO_INCREMENT ,`ip` VARCHAR( 15 ) NOT NULL ,`count` SMALLINT( 5 ) DEFAULT '0',PRIMARY KEY ( `uid` ) ) TYPE = MYISAM";
if (!mysql_query($query)) $this->_die(mysql_error());
}
$query = "SELECT uid FROM ".$tableKey." WHERE `sid` LIKE '$sid' AND `key` LIKE '$key' LIMIT 1";
$result = mysql_query($query);
$rowKey = mysql_fetch_assoc($result);
if($rowKey['uid']) $query = "UPDATE ".$tableKey." SET timeout = $expire , ip = '$ip' WHERE uid = ".$rowKey['uid'];
else $query = "INSERT INTO ".$tableKey." VALUES ('','$sid','$key','$expire','$ip')";
@mysql_query($query);
mysql_close($this->_db_link_id);
} else {
$_SESSION[FLV_SECRET_KEY][$key] = time()+$validtime;
}
return true;
}
/**
* Close a Lock used when playing with playFlvLock.
*
* @param int $lock_id uid in Database
*/
function closeLock($lock_id = 0)
{
if($lock_id) {
$this->loadConfig();
$this->dbConnect();
$sid = session_id();
$timeout = time();
$tableKey = $this->config['db_table_prefix']."key";
$tableBlock = $this->config['db_table_prefix']."block";
$query = "DELETE FROM `".$tableKey."` WHERE `uid` = '$lock_id' AND `sid` LIKE '$sid' AND `key` LIKE '$this->lock_key'";
if (!mysql_query($query)) $this->_die(mysql_error());
mysql_close($this->_db_link_id);
}
unset($_SESSION[FLV_SECRET_KEY][$this->lock_key]);
}
/**
* Play the flv with lock
*
* @param bool $usedb Check in database
*
* @return true/false if sucess/failed.
*/
function validLock($usedb=false)
{
if(!session_id()) session_start();
$return = false;
if($usedb) {
$this->loadConfig();
$this->dbConnect();
$ip = $_SERVER['REMOTE_ADDR'];
$tableKey = $this->config['db_table_prefix']."key";
$tableBlock = $this->config['db_table_prefix']."block";
$query = "SELECT count FROM ".$tableBlock." WHERE `ip` LIKE '$ip' LIMIT 1";
$result = mysql_query($query);
$rowBlock = mysql_fetch_assoc($result);
$sid = session_id();
$timeout = time();
$query = "SELECT uid FROM ".$tableKey." WHERE `sid` LIKE '$sid' AND `key` LIKE '$this->lock_key' AND `timeout` >= $timeout LIMIT 1";
$result = mysql_query($query);
$rowKey = mysql_fetch_assoc($result);
if($rowKey['uid'] && ($this->config['block_counter'] > $rowBlock['count'] || !$rowBlock['count'])) $return = true;
elseif($this->config['block_counter']) {
$query = "SELECT uid FROM ".$tableBlock." WHERE `ip` LIKE '$ip' LIMIT 1";
$result = mysql_query($query);
$rowBlock = mysql_fetch_assoc($result);
if($rowBlock['uid']) $query = "UPDATE ".$tableBlock." SET count = count+1 WHERE uid = ".$rowBlock['uid'];
else $query = "INSERT INTO ".$tableBlock." VALUES ('','$ip','1')";
@mysql_query($query);
$this->error[] = FLV_ERROR_BLOCKED;
}
mysql_close($this->_db_link_id);
} elseif($_SESSION[FLV_SECRET_KEY][$this->lock_key] >= time()) {
$return = true;
}
if (isset($rowKey['uid'])) {
$this->closeLock($rowKey['uid']);
}
return $return;
}
/**
* Play the flv with lock
*
* @param string $key Set Key
*/
function setKey($key)
{
$this->lock_key = $key;
}
/**
* Returns the next tag from the open file
*
* @param array|bool $skipTagTypes The tag types contained in this array won't be examined
*
* @return object FLV_Tag_Generic or one of its descendants
*/
function getTag( $skipTagTypes = false )
{
if ($this->eof) return NULL;
$hdr = fread( $this->_fh, $this->TAG_HEADER_SIZE );
if (strlen($hdr) < $this->TAG_HEADER_SIZE) {
$this->eof = true;
return NULL;
}
// check against corrupted files
$prevTagSize = unpack( 'Nprev', $hdr );
// if ($prevTagSize['prev'] != $this->_lastTagSize) die("<br>Previous tag size check failed. Actual size is ".$this->_lastTagSize." but defined size is ".$prevTagSize['prev']);
// Get the tag object by skiping the first 4 bytes which tell the previous tag size
$tag = FLV_Tag::getTag( substr( $hdr, 4 ) );
// Read at most MAX_TAG_BODY_SIZE bytes of the body
$bytesToRead = min( $this->MAX_TAG_BODY_SIZE, $tag->size );
$tag->setBody( fread( $this->_fh, $bytesToRead ) );
// Check if the tag body has to be processed
if ((is_array($skipTagTypes) && !in_array($tag->type, $skipTagTypes)) || !$skipTagTypes) $tag->analyze();
// If the tag was skipped or the body size was larger than MAX_TAG_BODY_SIZE
if ($tag->size > $bytesToRead) fseek($this->_fh, $tag->size-$bytesToRead, SEEK_CUR);
$this->_lastTagSize = $tag->size + $this->TAG_HEADER_SIZE - 4;
$tag->start = $this->getTagOffset();
$tag->end = ftell($this->_fh);
return $tag;
}
/**
* Returns the offset from the start of the file of the last processed tag
*
* @return int get offset
*/
function getTagOffset()
{
return ftell($this->_fh) - $this->_lastTagSize;
}
/**
* Connect to Database
*/
function dbConnect()
{
$this->loadConfig();
// connect to the mysql database server.
$this->_db_link_id = mysql_connect ($this->config['db_host'], $this->config['db_username'], $this->config['db_password']);
// sellect db
if (!mysql_select_db($this->config['db_name'])) $this->_die(mysql_error());
}
/**
* Load Config
*/
function loadConfig()
{
if( empty($this->config) ) {
$config = array();
require(FLV_INCLUDE_PATH.'config.php');
$this->config = $config;
}
}
/**
* defaultMetaData
*
* @param bool $supressGetId3
*
* @return array default Metadata
*/
function defaultMetaData($supressGetId3=false)
{
$buffMetaData = array();
if(!$supressGetId3) {
// $buffMetaData = $this->FileInfo;
$buffMetaData = $this->FileInfo['flv']['meta']['onMetaData'];
$buffMetaData['flv'] = $this->FileInfo['flv'];
$buffMetaData['video'] = $this->FileInfo['video'];
$buffMetaData['audio'] = $this->FileInfo['audio'];
$buffMetaData['bitrate'] = $this->FileInfo['bitrate'];
}
$buffMetaData['width'] = $this->FileInfo['video']['resolution_x'];
$buffMetaData['height'] = $this->FileInfo['video']['resolution_y'];
$buffMetaData['metadatacreator'] = 'FLV Editor for PHP '.FLV_VERSION.' (Project: Flv4php)';
$buffMetaData['creator'] = 'FLV Editor for PHP '.FLV_VERSION." (Project: Flv4php)";
$buffMetaData['metadatadate'] = gmdate('Y-m-d\TH:i:s') . '.000Z';
return (array) $buffMetaData;
}
/**
* defaultMetaData replace timestamp in frame with new one
*/
function replaceTimestamp(&$frame,$newTimestamp)
{
$frame[4] = chr($newTimestamp >> 16);
$frame[5] = chr($newTimestamp >> 8);
$frame[6] = chr($newTimestamp);
$frame[7] = chr($newTimestamp >> 24);
}
/**
* defaultMetaData replace timestamp in frame with new one
*
* @param $timestamp
* @param int $modifyer
*
* @return int Modifyed timestamp
*/
function modifyTimestamp($timestamp,$modifyer=100)
{
if ($modifyer < 1) $modifyer = 1;
return (int) ceil((100*$timestamp)/$modifyer);
}
}