用PHP读取flv文件的播放时间长度

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

<?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}) * 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;
}
?>

相关文章

PHP $_SERVER详解

复制代码 代码如下:$_SERVER['HTTP_ACCEPT_LANGUAGE']//浏览器语言 $_SERVER['REMOTE_ADDR'] //当前用户 IP 。 $_SERVE...

PHP工厂模式的日常使用

负责生成其他对象的类或方法,这就是工厂模式,下面是一个经常见到的用法 <?php class test{ public $x=1; public $settin...

PHP 单引号与双引号的区别

1、定义字符串   在PHP中,字符串的定义可以使用单引号,也可以使用双引号。但是必须使用同一种单或双引号来定义字符串,如:‘Hello"和“Hello'为非法的字符串定义。    定义...

PHP数组交集的优化代码分析

不过由于手机的参数多,且不同的手机其参数差异大,所以参数表结构通常是纵表(一个参数是一行),而不是横表(一个参数是一列),此时使用若干参数来取结果,通常就是把每个单独参数来取结果,再一起...

PHP 函数执行效率的小比较

就是把原来的数组中的数都“拆”成“单”位的。 下面是自己写的一个函数: 复制代码 代码如下: function splitStrToArray_mine($array) { $new_a...