| 副标题[/!--empirenews.page--] PHP脚本调用ffmpeg对视频截图并拼接,供大家参考,具体内容如下
 
 目前支持MKV,MPG,MP4等常见格式的视频,其他格式有待测试 12P 一张截图平均生成时间 1.64s 100个视频,大概需要2分半左右 9P 一张截图平均生成时间 1.13s 100个视频,大概需要2分钟左右 6P 一张截图平均生成时间 0.86s 100个视频,大概需要1分半左右 3P 一张截图平均生成时间 0.54s 100个视频,大概需要1分钟左右 <div class="jb51code"><pre class="brush:php;">
 <?php
 define('DS',DIRECTORY_SEPARATOR);
 date_default_timezone_set("Asia/Shanghai");
 class FileLoader
 {
 //路径变量
 private $rootdir    = '';
 private $tmp      = "tmp";      //tmp 目录
 private $source     = "mpg";      //source 目录
 private $destination  = "screenshoot";  //目标截图路径
 private $emptyImageName = "empty.jpg";   //合成的背景图
 //文件数组
 private $maxShoots   = 12;        //最大的截图数
 private $videoInfo   = NULL;
 private $files     = array();     //文件数
 private $fileArray   = array();
 private $extensionArray = array("mpg","mkv","mp4","avi","3gp","mov");  //支持的格式
 private $timeArray   = array("00:00:10","00:00:20","00:00:30","00:01:00","00:01:30","00:02:00","00:02:30","00:03:00","00:03:30","00:03:40","00:03:50","00:04:00");
 //统计变量
 private $timeStart   = 0;
 private $timeEnd    = 0;
 private $fileCount   = 0;
 private $successCount  = 0;
 private $failedCount  = 0;
 /**  
初始化信息*/
 function construct()
 {
 file_put_contents("log.txt","");
 $this->rootdir = dirname(FILE__);
 $count = count($this->timeArray);
 for($i=1;$i<=$count;$i++) 
{ 
  $ii = $i-1; 
  $this->fileArray[$ii] = $this->tmp.DS.$i.".jpg"; 
} 
 }  /**  
当前时间,精确到小数点*/
 private static function microtime_float()
 {
 list($usec,$sec)= explode(" ",microtime());
 return ((float)$usec + (float)$sec);
 }
 /**  
00:00:00 时间转秒/
 private static function timeToSec($time)
 {
 $p = explode(':',$time);
 $c = count($p);
 if ($c>1)
 {
 $hour  = intval($p[0]);
 $minute = intval($p[1]);
 $sec   = intval($p[2]);
 }
 else
 {
 throw new Exception('error time format');
 }
 $secs = $hour  3600 + $minute * 60 + $sec;
 return $secs;
 }
 /**  
00:00:00 时间转秒*/
 private static function secToTime($time)
 {
 $hour = floor($time/3600); 
$min = floor(($time - $hour * 3600)/60); 
$sec = $time % 60; 
$timeStr = sprintf("%02d:%02d:%02d",$hour,$min,$sec); 
return $timeStr;    
 }  /**  
获取全部文件*/
 private function getFiles($dir)
 {
 $files = array();
 $dir = rtrim($dir,"/") . DS;
 $dh = opendir($dir);
 if ($dh == false) { return $files; }
 while (($file = readdir($dh)) != false) 
{ 
  if ($file{0} == '.') { continue; } 
  $path = $dir . $file; 
  if (is_dir($path)) 
  { 
    $files = array_merge($files,$this->getFiles($path)); 
  } 
  elseif (is_file($path)) 
  { 
    $files[] = $path; 
  } 
} 
closedir($dh); 
return $files; 
 }  /**  
搜索路径*/
 public function searchDir($sourcePath = NULL)
 {
 $this->timeStart = $this->microtime_float(); 
if ($sourcePath)  
{ 
  $this->rootdir = $sourcePath; 
}  
if (file_exists($this->rootdir) && is_dir($this->rootdir)) 
{ 
  $this->files = $this->getFiles($this->rootdir.DS.$this->source);       
} 
$this->fileCount = count($this->files); 
foreach ($this->files as $path) 
{ 
  $fi = pathinfo($path); 
  $flag = array_search(strtolower($fi['extension']),$this->extensionArray); 
  if (!$flag) continue; 
  $this->getScreenShoot(basename($path)); 
} 
$this->timeEnd = $this->microtime_float(); 
$time = $this->timeEnd - $this->timeStart; 
if($this->fileCount > 0) 
{ 
  $str = sprintf("[TOTAL]: Cost Time:%8s | Total File:[%d] | Successed:[%d] | Failed:[%d] | Speed:%.2fs per filen",$this->secToTime($time),$this->fileCount,$this->successCount,$this->failedCount,$time/$this->fileCount); 
  file_put_contents("log.txt",$str,FILE_APPEND);  
} 
else 
{ 
  $str = sprintf("[TOTAL]: Cost Time:%8s | Total File:[%d] | Successed:[%d] | Failed:[%d] | Speed:%.2fs per filen",0); 
  file_put_contents("log.txt",FILE_APPEND); 
}   
 }  /**  
获取视频信息*/
 private function getVideoInfo($file){
 $re = array();
 exec(".".DS."ffmpeg -i {$file} 2>&1",$re);
 $info = implode("n",$re);
 if(preg_match("/No such file or directory/i",$info)){
 return false;
 }
 if(preg_match("/Invalid data/i",$info)){return false;
 }
 $match = array();preg_match("/d{2,}xd+/",$info,$match);
 list($width,$height) = explode("x",$match[0]);
 (编辑:南平站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |