得到flv视频长度

Suj*_*eet 5 php video 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))
?>
Run Code Online (Sandbox Code Playgroud)

我在下面的链接中附加了工作和不工作的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

任何想法将不胜感激.

谢谢

wew*_*web 7

当视频的元信息被部分或完全取消时,会发生此类问题.要解决此问题,请使用FFMPEG commnad行工具在上载时修复此类损坏的文件.以下是使用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);
?>
Run Code Online (Sandbox Code Playgroud)

下载FFMPEG请访问http://www.ffmpeg.org