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

yipeiwu_com6年前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;
}
?>

相关文章

Linux fgetcsv取得的数组元素为空字符串的解决方法

但服务器上,很多使用Linux服务器,源程序使用UTF-8,这样很容易产生字符编码的问题. 如果仅仅将CSV文件转码为UTF-8,这样在Windows服务器上没有问题, 而在RedHat...

php中将地址生成迅雷快车旋风链接的代码[测试通过]

在线演示地址:http://tools.jb51.net/tools/cs.php复制代码 代码如下:<?php function zhuanhuan() { $urlodd=ex...

使ecshop模板中可引用常量的实现方法

比如$smarty.const.'常量',这个就不能用。 其实模板引擎原理上并不复杂,只是把一些模板标签替换为php中的函数,变量,语法结构罢了。 这次要在ecshop模板中加入引用常量...

细谈php中SQL注入攻击与XSS攻击

例如: SQL注入攻击 XSS攻击 复制代码 代码如下: 任意执行代码 文件包含以及CSRF. } 关于SQL攻击有很多文章还有各种防注入脚本,但是都不能解决SQL注入的根本问题 见代...

php将12小时制转换成24小时制的方法

本文实例讲述了php将12小时制转换成24小时制的方法。分享给大家供大家参考。具体如下: php将12小时制转换成24小时制,输入格式为:02:30:00 pm 转换成:14:30:00...