PHP   发布时间:2019-11-10  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP调用ffmpeg对视频截图并拼接脚本大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

php脚本调用ffmpeg对视频截图并拼接,供大家参,具体内容如下

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">@H_262_19@ <pre class="brush:php;">@H_262_19@ <?php@H_262_19@ define('DS',DIRECTORY_SEPARATOR);@H_262_19@ date_default_timezone_set("Asia/Shanghai");@H_262_19@ class FileLoader@H_262_19@ {@H_262_19@ //路径变量@H_262_19@ private $rootdir = '';@H_262_19@ private $tmp = "tmp"; //tmp 目录@H_262_19@ private $source = "mpg"; //source 目录@H_262_19@ private $desTination = "screenshoot"; //目标截图路径@H_262_19@ private $emptyImagename = "empty.jpg"; //合成的背景图@H_262_19@ //文件数组@H_262_19@ private $maxShoots = 12; //最大的截图数@H_262_19@ private $videoInfo = NULL;@H_262_19@ private $files = array(); //文件数@H_262_19@ private $fileArray = array();@H_262_19@ private $extensionArray = array("mpg","mkv","mp4","avi","3gp","mov"); //支持的格式@H_262_19@ 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");@H_262_19@ //统计变量@H_262_19@ private $timestart = 0;@H_262_19@ private $timeEnd = 0;@H_262_19@ private $fileCount = 0;@H_262_19@ private $successCount = 0;@H_262_19@ private $failedCount = 0;

/**

  • 初始化信息@H_262_19@ */@H_262_19@ function construct()@H_262_19@ {@H_262_19@ file_put_contents("log.txt","");@H_262_19@ $this->rootdir = dirname(FILE__);@H_262_19@ $count = count($this->timeArray);
for($i=1;$i<=$count;$i++) 
{ 
  $ii = $i-1; 
  $this->fileArraY[$ii] = $this->tmp.Ds.$i.".jpg"; 
} 

}

/**

  • 当前时间,精确到小数点@H_262_19@ */@H_262_19@ private static function microtime_float()@H_262_19@ {@H_262_19@ list($usec,$seC)= explode(" ",microtime());@H_262_19@ return ((float)$usec + (float)$sec);@H_262_19@ }

/**

  • 00:00:00 时间转秒@H_262_19@ /@H_262_19@ private static function timeToSec($timE)@H_262_19@ {@H_262_19@ $p = explode(':',$timE);@H_262_19@ $c = count($p);@H_262_19@ if ($c>1)@H_262_19@ {@H_262_19@ $hour = intval($p[0]);@H_262_19@ $minute = intval($p[1]);@H_262_19@ $sec = intval($p[2]);@H_262_19@ }@H_262_19@ else@H_262_19@ {@H_262_19@ throw new Exception('error time format');@H_262_19@ }@H_262_19@ $secs = $hour 3600 + $minute * 60 + $sec;@H_262_19@ return $secs;@H_262_19@ }

/**

  • 00:00:00 时间转秒@H_262_19@ */@H_262_19@ private static function secToTime($timE)@H_262_19@ {
$hour = floor($time/3600); 
$min = floor(($time - $hour * 3600)/60); 
$sec = $time % 60; 
$timestr = sprintf("%02d:%02d:%02d",$hour,$min,$sec); 
return $timestr;    

}

/**

  • 获取全部文件@H_262_19@ */@H_262_19@ private function getFiles($dir)@H_262_19@ {@H_262_19@ $files = array();@H_262_19@ $dir = rtrim($dir,"/\") . DS;@H_262_19@ $dh = opendir($dir);@H_262_19@ 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; 

}

/**

  • 搜索路径@H_262_19@ */@H_262_19@ public function searchDir($sourcePath = NULL)@H_262_19@ {
$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("[@R_176_10586@L]: Cost Time:%8s | @R_176_10586@l File:[%d] | successed:[%d] | Failed:[%d] | Speed:%.2fs per file\n",$this->secToTime($timE),$this->fileCount,$this->successCount,$this->failedCount,$time/$this->fileCount); 
  file_put_contents("log.txt",$str,FILE_APPEND);  
} 
else 
{ 
  $str = sprintf("[@R_176_10586@L]: Cost Time:%8s | @R_176_10586@l File:[%d] | successed:[%d] | Failed:[%d] | Speed:%.2fs per file\n",0); 
  file_put_contents("log.txt",FILE_APPEND); 
}   

}

/**

  • 获取视频信息@H_262_19@ */@H_262_19@ private function getVideoInfo($filE){@H_262_19@ $re = array();@H_262_19@ exec(".".Ds."ffmpeg -i {$filE} 2>&1",$rE);@H_262_19@ $info = implode("\n",$rE);

    if(preg_match("/No such file or directory/i",$info))@H_262_19@ {@H_262_19@ return false;@H_262_19@ }

    if(preg_match("/Invalid data/i",$info)){@H_262_19@ return false;@H_262_19@ }

    $match = array();@H_262_19@ preg_match("/\d{2,}x\d+/",$info,$match);@H_262_19@ list($width,$height) = explode("x",$match[0]);

    $match = array();@H_262_19@ preg_match("/Duration:(.*?),/",$match);@H_262_19@ if($match)@H_262_19@ {@H_262_19@ $duration = date("H:i:s",strtotime($match[1]));@H_262_19@ }else@H_262_19@ {@H_262_19@ $duration = NULL;@H_262_19@ }

    $match = array();@H_262_19@ preg_match("/bitrate:(.*kb\/s)/",$match);@H_262_19@ $bitrate = $match[1];

    if(!$width && !$height && !$duration && !$bitratE){@H_262_19@ return false;@H_262_19@ }else{@H_262_19@ return array(@H_262_19@ "file" => $file,"width" => $width,"height" => $height,"duration" => $duration,"bitrate" => $bitrate,"secends" => $this->timeToSec($duration)@H_262_19@ );@H_262_19@ }@H_262_19@ }

/**

  • 设置截图时间@H_262_19@ */@H_262_19@ private function setShootSecends($secends,$useDefault = NO)@H_262_19@ {
if($useDefault) 
{ 
  if($secends<18) 
  { 
    $time = 1; 
  }else 
  { 
    $time = 5; 
  }   

  $range = floor(($secends - $timE)/ ($this->maxShoots)); 
  if ($range < 1)  
  { 
    $range = 1; 
  } 

  $this->timeArray = array(); 
  for($i=0;$i<$this->maxShoots;$i++) 
  { 
    $this->timeArraY[$i] = $this->secToTime($timE); 
    $time = $time + $range; 
    if ($time > $secends) break; 
  } 
} 

}

/**

  • 拼接图片@H_262_19@ */@H_262_19@ private function getFixedPhoto($fileName)@H_262_19@ {
$target = $this->rootdir.Ds.$this->emptyImagename;//背景图片 
$target_img = Imagecreatefromjpeg($target); 
$source= array(); 

foreach ($this->fileArray as $k=>$v) 
{ 
  $source[$k]['source'] = Imagecreatefromjpeg($v); 
  $source[$k]['size'] = getimagesize($v); 
} 

$tmpx=5; 
$tmpy=5;//图片之间的间距 
for ($i=0; $i< count($this->timeArray); $i++) 
{   
  imagecopy($target_img,$source[$i]['source'],$tmpx,$tmpy,$source[$i]['size'][0],$source[$i]['size'][1]); 
  $target_img = $this->setTimeLabel($target_img,$source[$i]['size'][1],$this->timeArraY[$i]);   
  $tmpx = $tmpx+ $source[$i]['size'][0]; 
  $tmpx = $tmpx+5; 
  if(($i+1) %3 == 0){ 
    $tmpy = $tmpy+$source[$i]['size'][1]; 
    $tmpy = $tmpy+5; 
    $tmpx=5; 
  } 
} 

$target_img = $this->setVideoInfoLabel($target_img,$this->videoInfo); 
Imagejpeg($target_img,$this->rootdir.Ds.$this->desTination.Ds.$filename.'.jpg');  

}

/**

  • 设置时间刻度标签@H_262_19@ */@H_262_19@ private function setTimeLabel($image,$image_x,$image_y,$image_w,$image_h,$img_text)@H_262_19@ {@H_262_19@ imagealphablending($image,truE);@H_262_19@ //设定颜色@H_262_19@ $color=imagecolorallocate($image,255,255);@H_262_19@ $ttf_im=imagettfbbox(30,"Arial.ttf",$this->img_text);@H_262_19@ $w = $ttf_im[2] - $ttf_im[6];@H_262_19@ $h = $ttf_im[3] - $ttf_im[7];@H_262_19@ unset($ttf_im);

    $txt_y =$image_y+$image_h+$h-5;@H_262_19@ $txt_x =$image_x+$w+5;

    imagettftext($image,30,$txt_x,$txt_y,$color,$img_text);@H_262_19@ return $image;@H_262_19@ }

/**

  • 设置视频信息标签@H_262_19@ */@H_262_19@ private function setVideoInfoLabel($image,$videoInfo)@H_262_19@ {@H_262_19@ imagealphablending($image,truE);

    $color=imagecolorallocate($image,0);

    imagettftext($image,32,100,2000+30,"FZLTHJW.ttf","Filename:".basename($videoInfo["file"]));@H_262_19@ imagettftext($image,1600,"Size:".$videoInfo["width"]."x".$videoInfo["height"]);@H_262_19@ imagettftext($image,2000+120,"Duration:".$videoInfo["duration"]);@H_262_19@ imagettftext($image,"Bitrate:".$videoInfo["bitrate"]);@H_262_19@ return $image;@H_262_19@ }

/**

  • 屏幕截图@H_262_19@ */@H_262_19@ public function getScreenShoot($fileName)@H_262_19@ {@H_262_19@ $fi = pathinfo($fileName);@H_262_19@ $this->videoInfo = $this->getVideoInfo($this->rootdir.Ds.$this->source.Ds.$fileName);@H_262_19@ if($this->videoInfo)@H_262_19@ {@H_262_19@ $this->setShootSecends($this->videoInfo["secends"]);

    for ($i=0; $i< count($this->timeArray); $i++ )@H_262_19@ {@H_262_19@ $cmd=".".Ds."ffmpeg -ss ". $this->timeArraY[$i] ." -i ". $this->rootdir.Ds.$this->source.Ds.$filename ." -y -f image2 -s 720*480 -vframes 1 ".$this->rootdir.Ds.$this->fileArraY[$i];@H_262_19@ exec($cmd,$out,$status);@H_262_19@ }@H_262_19@ $this->getFixedPhoto($fileName);

    $str = sprintf("[%s]:OK...........[%s][%2dP]%-30s\n",date("y-m-d h:i:s",time()),$this->videoInfo["duration"],count($this->timeArray),$fileName);@H_262_19@ file_put_contents("log.txt",FILE_APPEND);@H_262_19@ $this->successCount += 1;@H_262_19@ }else@H_262_19@ {@H_262_19@ $str = sprintf("[%s]:FAILED.................................[%s][%2dP]%-30s\n",FILE_APPEND);@H_262_19@ $this->failedCount += 1;@H_262_19@ }@H_262_19@ }

/**

  • TODO:
  • 截取图片,* 需要配置ffmpeg-php,比较麻烦,
  • 但是这个类确实挺好用的。@H_262_19@ */@H_262_19@ public function getScreenShoot2($fileName)@H_262_19@ {@H_262_19@ if(extension_loaded('ffmpeg')){//判断ffmpeg是否载入@H_262_19@ $mov = new ffmpeg_movie($this->rootdir.Ds.$this->source.Ds.$fileName);//视频的路径@H_262_19@ $count = $mov->getFrameCount();@H_262_19@ $ff_frame = $mov->getFrame(floor($count/2));@H_262_19@ if($ff_framE)@H_262_19@ {@H_262_19@ $gd_image = $ff_frame->toGDImage();@H_262_19@ $img=$this->rootdir.Ds."test.jpg";//要生成图片的绝对路径@H_262_19@ imagejpeg($gd_image,$img);//创建jpg图像@H_262_19@ imagedestroy($gd_imagE);//销毁一图像@H_262_19@ }@H_262_19@ }else{@H_262_19@ echo "ffmpeg没有载入";@H_262_19@ }@H_262_19@ }@H_262_19@ }

$fileLoader = new FileLoader();@H_262_19@ $fileLoader->searchDir();@H_262_19@ ?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持菜鸟教程。

大佬总结

以上是大佬教程为你收集整理的PHP调用ffmpeg对视频截图并拼接脚本全部内容,希望文章能够帮你解决PHP调用ffmpeg对视频截图并拼接脚本所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。