Copy codeThe Code is as follows:
<? Php
// + ------------------------------------------------------------------------ +
// | PHP version 4 & 5 |
// + ------------------------------------------------------------------------ +
// | Copyright (c) 2007 JackieWangjackieit@hotmail.com |
// + ------------------------------------------------------------------------ +
// | This source file's function is to get the time length of flv |
// | Main function getTime param: $ name The flv file you want to get |
// + ------------------------------------------------------------------------ +
Function BigEndian2Int ($ byte_word, $ signed = false ){
$ Int_value = 0;
$ Byte_wordlen = strlen ($ byte_word );
For ($ I = 0; $ I <$ byte_wordlen; $ I ++ ){
$ Int_value + = ord ($ byte_word {$ I}) x pow (256, ($ byte_wordlen-1-$ I ));
}
If ($ signed ){
$ Sign_mask_bit = 0x80 <(8 * ($ byte_wordlen-1 ));
If ($ int_value & $ sign_mask_bit ){
$ Int_value = 0-($ int_value & ($ sign_mask_bit-1 ));
}
}
Return $ int_value;
}
Function getTime ($ name ){
If (! File_exists ($ name )){
Return;
}
$ Flv_data_length = filesize ($ name );
$ Fp = @ fopen ($ name, 'rb ');
$ Flv_header = fread ($ fp, 5 );
Fseek ($ fp, 5, SEEK_SET );
$ Frame_size_data_length = BigEndian2Int (fread ($ fp, 4 ));
$ Flv_header_frame_length = 9;
If ($ frame_size_data_length> $ flv_header_frame_length ){
Fseek ($ fp, $ frame_size_data_length-$ flv_header_frame_length, SEEK_CUR );
}
$ Duration = 0;
While (ftell ($ fp) + 1) <$ flv_data_length ){
$ This_tag_header = fread ($ fp, 16 );
$ Data_length = BigEndian2Int (substr ($ this_tag_header, 5, 3 ));
$ Timestamp = BigEndian2Int (substr ($ this_tag_header, 8, 3 ));
$ Next_offset = ftell ($ fp)-1 + $ data_length;
If ($ timestamp> $ duration ){
$ Duration = $ timestamp;
}
Fseek ($ fp, $ next_offset, SEEK_SET );
}
Fclose ($ fp );
Return $ duration;
}
?>