当我尝试获取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

任何想法将不胜感激。

谢谢

最佳答案

当视频的元信息被部分或全部纠正时,会发生这种类型的问题。为了解决此问题,请使用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

10-05 20:54
查看更多