Which brother can help annotate this program? The function is used to obtain flv videos. it is found online, but it is not clear in many places... PHPcodefunctionBigEndian2Int ($ byte_word, $ signedfalse) {$ int_value0; $ byte_wordlenstrlen ($ byte_wor which brother can help annotate this program?
The function is used to obtain flv videos. it is found online, but it is not clear in many places...
PHP code
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}) * 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 ;} // Obtain the video's digital time 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 ($ scheme> $ 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 ;} // convert to the time format function fn ($ time) {$ num = $ time; $ sec = intval ($ num/1000 ); $ h = intval ($ sec/3600); $ m = intval ($ sec % 3600)/60); $ s = intval ($ sec % 60 )); $ tm = $ h. ':'. $ m. ':'. $ s; return $ tm;} $ t = getTime ("/tmp/907701336.flv"); // display the numeric time, for example, 236722 echo fn ($ t); // display the time format 0:03:56
What do BigEndian2Int functions and getTimes numbers mean?
------ Solution --------------------
According to the FLV file format
FLV consists of File Header and File Body. The File Body consists of a series of Tag and Tag Size pairs. Tag includes the Tag Header and Tag Data.
Tag Header
The value of the 5-7 bytes is of the UI24 type, indicating the Tag timestamp (unit: ms)
The second byte is an extended timestamp. when the 24-bit value is not enough, this byte is used as the highest bit to extend the timestamp to a 32-bit value.
You only need to add the timestamp to get the whole playback time.
The UI24 type occupies three bytes, with the top priority
So the program has
$ Int_value + = ord ($ byte_word {$ I}) x pow (256, ($ byte_wordlen-1-$ I ));
This method is very bad, and pow is also used.
You can also modify the basic formula.
Input string $ s with (ord ($ s {0}) * 256 + ord ($ s {1}) * 256 + ord ($ s {2 })