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
  ?>

相关文章

php字符串按照单词进行反转的方法

本文实例讲述了php字符串按照单词进行反转的方法。分享给大家供大家参考。具体分析如下: 下面的php代码可以将字符串按照单词进行反转输出,实际上市现将字符串按照空格分隔到数组,然后对数组...

浅析PHP开发规范

基本约定 源文件 代码使用<?php开头,忽略闭合标签?> 文件格式必须是无BOM UTF-8格式 一个文件只声明一种类型,如class和interfa...

Laravel中log无法写入问题的解决

前言 账号登录报500错误,也没有返回错误信息,没办法只能使用原始方法,到现在一行一行打印。到 Log::info() 后面就无法正常显示了,那么问题就找到了。 导致无法写入日志的问题,...

php 注册时输入信息验证器的实现详解

1、对输入信息进行验证的类(主要用于验证用户名,密码,重复密码,邮箱,可添加其它功能)复制代码 代码如下:<?php/** * Validator for Registe...

PHP 将dataurl转成图片image方法总结

PHP 将dataurl转成图片image方法 使用canvas 生成的图片,是使用dataurl的,php无法直接通过file_put_contents方法保存到本地电脑,需要做一下转...