php flv视频时间获取函数

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

<?php
  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;
  }
  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;
  }
  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;
  }
  echo getTime("27729.flv");//显示数字时间如236722
  echo fn(236722); //显示时间格式0:03:56
  ?>

相关文章

PHP5.4中json_encode中文转码的变化小结

在php5.4以前做json_encode的时候中文会被unicode编码,中文都会被编码,变成不可读的,类似“\u***”的格式,还会在一定程度上增加传输的数据量。 例如: 复制代码...

PHP中防止直接访问或查看或下载config.php文件的方法

或是,PHP的设计本身就避免直接查看文件内容的情况? 从安全角度考虑,这个系统级的文件应该做什么保护措施? 网友完善的答案 经调研,得出以下常用方法: 1 在程序中定义一个标识变量 复制...

php中3des加密代码(完全与.net中的兼容)

复制代码 代码如下: <?php class Crypt3Des { private $key = ""; private $iv = ""; /** * 构造,传递二个已经进行b...

php实现zip文件解压操作

PHP解压zip文件函数,源码简短,需要使用 ZZIPlib library 扩展,使用前请确认该扩展已经开启。 <? /*********************...

PHP字符串word末字符实现大小写互换的方法

本文实例讲述了PHP字符串word末字符实现大小写互换的方法。分享给大家供大家参考。具体实现方法如下: 一、要求: 给出一个字符串如 “A journey of, a thousand...