用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 清空varnish 缓存的详解(包括指定站点下的)

没法清空文件夹内容 只能清空指定链接缓存复制代码 代码如下:<?phpfunction clearVarnish($ip,$url,$host=null){  ...

PHP在引号前面添加反斜杠(PHP去除反斜杠)

一般空间商提供的服务器空间默认PHP 指令 magic_quotes_gpc是on的,也就是打开的。这时候就可以用stripslashes() 函数删除自动添加的反斜杠。用法就是:比如包...

php中文字符截取防乱码

先看段代码 复制代码 代码如下:<?php        $len = 15;&...

PHP实现适用于文件内容操作的分页类

本文实例为大家分享了PHP实现文件内容操作的分页类,强调一下只针对文件的操作,供大家参考,具体内容如下 <?php class StrPage { private...

php中http_build_query 的一个问题

当我们使用CURL来post数据的时候,需要设置post的数据 curl_setopt($c, CURLOPT_POSTFIELDS, $post_data); 假如这里的$data是...