获取flv视频长度

发布于 2024-11-11 10:10:06 字数 1338 浏览 5 评论 0原文

当我尝试获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟─花易冷 2024-11-18 10:10:06

当视频的元信息部分或完全损坏时,就会出现此类问题。为了解决此问题,请使用 FFMPEG 命令行工具,在上传时修复此类损坏的文件。下面是使用 FFMPEG 提取视频持续时间的代码片段。

<?php
     ob_start();
     passthru("ffmpeg -i working_copy.flv  2>&1");
     $duration = ob_get_contents();
     $full = ob_get_contents();
     ob_end_clean();
     $search = "/duration.*?([0-9]{1,})/";
     print_r($duration);
     $duration = preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE, 3);
     print_r('<pre>');
 print_r($matches[1][0]);
 print_r($full);
?>

要下载 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.

<?php
     ob_start();
     passthru("ffmpeg -i working_copy.flv  2>&1");
     $duration = ob_get_contents();
     $full = ob_get_contents();
     ob_end_clean();
     $search = "/duration.*?([0-9]{1,})/";
     print_r($duration);
     $duration = preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE, 3);
     print_r('<pre>');
 print_r($matches[1][0]);
 print_r($full);
?>

to download FFMPEG go to http://www.ffmpeg.org

筱武穆 2024-11-18 10:10:06

首先,我担心你的函数可能会完全停止工作,如果有足够大的 FLV 视频文件,并达到 PHP 的内存限制,

$contents = fread($handle, filesize($file));

因为你实际上是将整个文件加载到内存中。

然后,非工作文件对我来说似乎也已损坏。
flvmeta 给出以下输出:

$ flvmeta --check not_working_copy.flv
0x00488473: error E30013: unknown tag type 250
0x00488477: error E40023: timestamps are decreasing from 130543 to 0
2 error(s), 0 warning(s)

如果您需要有效地从可能已损坏或包含非标准标签的文件中获取持续时间,我建议您使用 MediaInfo,它可以很好地处理甚至是最奇特的视频文件,而无需像 ffmpeg 那样改变它们。

它可以像任何命令行程序一样从 PHP 调用,其输出通过命令行参数控制:

$ MediaInfo --Inform="Video;%Duration%" not_working_copy.flv
130000

以毫秒为单位显示视频持续时间。

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

$contents = fread($handle, filesize($file));

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:

$ flvmeta --check not_working_copy.flv
0x00488473: error E30013: unknown tag type 250
0x00488477: error E40023: timestamps are decreasing from 130543 to 0
2 error(s), 0 warning(s)

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:

$ MediaInfo --Inform="Video;%Duration%" not_working_copy.flv
130000

which displays the video duration in milliseconds.

昨迟人 2024-11-18 10:10:06

您可以提取 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文