大佬教程收集整理的这篇文章主要介绍了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;
/**
for($i=1;$i<=$count;$i++)
{
$ii = $i-1;
$this->fileArraY[$ii] = $this->tmp.Ds.$i.".jpg";
}
}
/**
/**
/**
$hour = floor($time/3600);
$min = floor(($time - $hour * 3600)/60);
$sec = $time % 60;
$timestr = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
return $timestr;
}
/**
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;
}
/**
$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@ }
/**
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;
}
}
}
/**
$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@ }
/**
$fileLoader = new FileLoader();@H_262_19@ $fileLoader->searchDir();@H_262_19@ ?>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持菜鸟教程。
以上是大佬教程为你收集整理的PHP调用ffmpeg对视频截图并拼接脚本全部内容,希望文章能够帮你解决PHP调用ffmpeg对视频截图并拼接脚本所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。