获取flv视频长度
当我尝试获取 flv 视频文件的长度时,我得到 0 秒,因为它只发生在某些视频上,否则我的功能工作正常。
下面是我的代码。
<?php
function mbmGetFLVDuration($file){
// read file
if (file_exists($file)){
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
fclose($handle);
//
if (strlen($contents) > 3){
if (substr($contents,0,3) == "FLV"){
$taglen = hexdec(bin2hex(substr($contents,strlen($contents)-3)));
if (strlen($contents) > $taglen){
$duration = hexdec(bin2hex(substr($contents,strlen($contents)-$taglen,3))) ;
return $duration;
}
}
}
}
}
// not working video file
$result = ceil(mbmGetFLVDuration('not_working_copy.flv')/1000);
// working video file
//$result = ceil(mbmGetFLVDuration('working_copy.flv')/1000);
echo date('H:i:s',mktime(0,0,$result))
?>
我在下面的链接中附上了工作和不工作的 flv 视频:
工作视频: http://blog.developeronhire.com/wp-content/uploads/downloads/ 2011/06/working_copy.flv
不工作视频: http://blog.developeronhire.com/wp-content/uploads/downloads/ 2011/06/not_working_copy.flv
任何想法将不胜感激。
谢谢
While i try to get length of a flv video file i get 0 second where as it only happens with some videos, else my function works fine.
below is my code.
<?php
function mbmGetFLVDuration($file){
// read file
if (file_exists($file)){
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
fclose($handle);
//
if (strlen($contents) > 3){
if (substr($contents,0,3) == "FLV"){
$taglen = hexdec(bin2hex(substr($contents,strlen($contents)-3)));
if (strlen($contents) > $taglen){
$duration = hexdec(bin2hex(substr($contents,strlen($contents)-$taglen,3))) ;
return $duration;
}
}
}
}
}
// not working video file
$result = ceil(mbmGetFLVDuration('not_working_copy.flv')/1000);
// working video file
//$result = ceil(mbmGetFLVDuration('working_copy.flv')/1000);
echo date('H:i:s',mktime(0,0,$result))
?>
i have attached both working and not working flv video in link below:
working video:
http://blog.developeronhire.com/wp-content/uploads/downloads/2011/06/working_copy.flv
not working video:
http://blog.developeronhire.com/wp-content/uploads/downloads/2011/06/not_working_copy.flv
any idea will be appreciated.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当视频的元信息部分或完全损坏时,就会出现此类问题。为了解决此问题,请使用 FFMPEG 命令行工具,在上传时修复此类损坏的文件。下面是使用 FFMPEG 提取视频持续时间的代码片段。
要下载 FFMPEG,请访问 http://www.ffmpeg.org
This type of problem occurs when the meta information of a video is partially or fully corrputed. In order to resolve this problem, use FFMPEG commnad line tool, to repair such corrupted file while uploading. below is a code snippet that extracts the video duration using FFMPEG.
to download FFMPEG go to http://www.ffmpeg.org
首先,我担心你的函数可能会完全停止工作,如果有足够大的 FLV 视频文件,并达到 PHP 的内存限制,
因为你实际上是将整个文件加载到内存中。
然后,非工作文件对我来说似乎也已损坏。
flvmeta 给出以下输出:
如果您需要有效地从可能已损坏或包含非标准标签的文件中获取持续时间,我建议您使用 MediaInfo,它可以很好地处理甚至是最奇特的视频文件,而无需像 ffmpeg 那样改变它们。
它可以像任何命令行程序一样从 PHP 调用,其输出通过命令行参数控制:
以毫秒为单位显示视频持续时间。
First of all, I'm afraid your function might stop working at all, given a sufficiently big FLV video file, and hitting PHP's memory_limit
because you are actually loading the entire file into memory.
Then, the non-working file also seems corrupted to me.
flvmeta gives the following output:
If you need to efficiently get the duration from a file that might be corrupted, or containing non-standard tags, I recommend you to use MediaInfo, which does a great job at handling even the most exotic video files, without altering them like ffmpeg would.
It can be called from PHP like any command-line program, and its output controlled via command line arguments:
which displays the video duration in milliseconds.
您可以提取 flv 视频的元数据,您会找到所有信息,如长度、大小等。
请参阅链接 http://code.google.com/p/flv4php/
祝苏吉好运
You can extract meta-data of flv video, you'll find all the info like length, size etc.
See link http://code.google.com/p/flv4php/
Good Luck sujeet