php获取flv视频时间长度代码

  1. function bigendian2int($byte_word, $signed = false) {
  2.   $int_value = 0;
  3.   $byte_wordlen = strlen($byte_word);
  4.   for ($i = 0; $i < $byte_wordlen; $i++)
  5.   {
  6.   $int_value += ord($byte_word{$i}) * pow(256, ($byte_wordlen - 1 - $i));
  7.   }
  8.   if ($signed)
  9.   {
  10.   $sign_mask_bit = 0x80 << (8 * ($byte_wordlen - 1));
  11.   if ($int_value & $sign_mask_bit)
  12.   {
  13.   $int_value = 0 - ($int_value & ($sign_mask_bit - 1));
  14.   }
  15.   }
  16.   return $int_value;
  17.   }
  18.   function gettime($name){
  19.   if(!file_exists($name)){
  20.   return;
  21.   }
  22.   $flv_data_length=filesize($name);
  23.   $fp = @fopen($name, 'rb');
  24.   $flv_header = fread($fp, 5);
  25.   fseek($fp, 5, seek_set);
  26.   $frame_size_data_length =bigendian2int(fread($fp, 4));
  27.   $flv_header_frame_length = 9;
  28.   if ($frame_size_data_length > $flv_header_frame_length) {
  29. //开源代码phpfensi.com
  30.   fseek($fp, $frame_size_data_length - $flv_header_frame_length, seek_cur);
  31.   }
  32.   $duration = 0;
  33.   while ((ftell($fp) + 1) < $flv_data_length) {
  34.   $this_tag_header = fread($fp, 16);
  35.   $data_length = bigendian2int(substr($this_tag_header, 5, 3));
  36.   $timestamp = bigendian2int(substr($this_tag_header, 8, 3));
  37.   $next_offset = ftell($fp) - 1 + $data_length;
  38.   if ($timestamp > $duration) {
  39.   $duration = $timestamp;
  40.   }
  41.   fseek($fp, $next_offset, seek_set);
  42.   }
  43.   fclose($fp);
  44.   return $duration;
  45.   }
  46.   function fn($time){
  47.   $num = $time;
  48.   $sec = intval($num / 1000);
  49.   $h = intval($sec / 3600);
  50.   $m = intval(($sec % 3600) / 60);
  51.   $s = intval(($sec % 60 ));
  52.   $tm = $h . ':' . $m . ':' . $s ;
  53.   return $tm;
  54.   }
  55.   echo gettime("27729.flv");//显示数字时间如236722
  56.   echo fn(236722); //显示时间格式0:03:56