用PHP讀取flv文件的播放時間長度
更新時間:2009年09月03日 00:20:52 作者:
用PHP讀取flv文件的播放時間長度的代碼,需要用的朋友可以參考下。
復(fù)制代碼 代碼如下:
<?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;
}
?>
相關(guān)文章
PHP中static關(guān)鍵字原理的學(xué)習(xí)研究分析
PHP中static關(guān)鍵字原理的學(xué)習(xí)研究分析,學(xué)習(xí)php的朋友可以參考下。2011-07-07深入研究PHP中的preg_replace和代碼執(zhí)行
這篇文章主要給大家介紹了關(guān)于PHP中preg_replace和代碼執(zhí)行的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08如何使用SublimeText3配置 PHP IDE環(huán)境
這篇文章主要介紹了如何使用SublimeText3配置 PHP IDE環(huán)境,并使用Xdebug進行調(diào)試,喜歡使用SublimeText的同學(xué),可以參考下2021-04-04PHP 雜談《重構(gòu)-改善既有代碼的設(shè)計》之五 簡化函數(shù)調(diào)用
前幾篇系列文章,我比較關(guān)注的是 PHP 雜談《重構(gòu)-改善既有代碼的設(shè)計》之一 重新組織你的函數(shù) 但是我覺得我還是沒有說清楚,我自己也有很多不理解的地方,而且這篇是我的第一篇這方面的文章,有很多的紕漏,所以我會經(jīng)常性的去做修改,如果大家有好的意見不妨告知一、二2012-05-05