用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相当简单的分页类

class Helper_Page{ /** 总信息数 */ var $infoCount; /** 总页数 */ var $pageCount; /** 每页显示条数 */ var $...

/etc/php-fpm.d/www.conf 配置注意事项

1、php-fpm 配置文件里 rlimit_files的值 要与系统的打开连接数一致 1)查看系统文件打开连接数 [root@iZ94eveq0q4Z ~]# ulimit -n 65...

不要轻信 PHP_SELF的安全问题

复制代码 代码如下:<html> <body> <?php if (isset($_REQUEST['submitted']) && $_REQUEST['...

php匹配字符中链接地址的方法

本文实例讲述了php匹配字符中链接地址的方法。分享给大家供大家参考。具体如下: 判断一个字符串是否含有超级链接,代码如下: 复制代码 代码如下:$str="ssdsf<a targ...

PHP 生成的XML以FLASH获取为乱码终极解决

经过探索最终解决。记录之,顺便也记录了通用解决方案。如果你也遇到XML<->FLASH乱码情况,可以速查: 1.确信XML绝对没有问题的情况: 首先,flash读取xml出现...