脚本宝典收集整理的这篇文章主要介绍了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="js-codecode">
<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;
/**
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("[total]: Cost Time:%8s | Total File:[%d] | Successed:[%d] | <a href="https://www.js-code.COM/tag/Failed/" target="_blank" class="keywords">Failed</a>:[%d] | Speed:%.2fs per file\n",$this->secToTime($time),$this->fileCount,$this->successCount,$this-><a href="https://www.js-code.com/tag/Failed/" target="_blank" class="keywords">Failed</a>Count,$time/$this->fileCount);
file_put_contents("log.txt",$str,FILE_APPEND);
}
else
{
$str = sprintf("[TOTAL]: Cost Time:%8s | Total File:[%d] | Successed:[%d] | <a href="https://www.js-code.com/tag/Failed/" target="_blank" class="keywords">Failed</a>:[%d] | Speed:%.2fs per file\n",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,}x\d+/",$info,$match);
list($width,$height) = explode("x",$match[0]);
$match = array();
preg_match("/Duration:(.*?),/",$match);
if($match)
{
$duration = date("H:i:s",strtotime($match[1]));
}else
{
$duration = NULL;
}
$match = array();
preg_match("/bITrate:(.*kb\/s)/",$match);
$bitrate = $match[1];
if(!$width && !$height && !$duration && !$bitrate){
return false;
}else{
return array(
"file" => $file,"width" => $width,"height" => $height,"duration" => $duration,"bitrate" => $bitrate,"secends" => $this->timeToSec($duration)
);
}
}
/**
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;//背景<a href="https://www.js-code.com/tag/tupian/" target="_blank" class="keywords">图片</a>
$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;//<a href="https://www.js-code.com/tag/tupian/" target="_blank" class="keywords">图片</a>之<a href="https://www.js-code.com/tag/jiande/" target="_blank" class="keywords">间的</a>间距
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');
}
/**
设置时间刻度标签
*/
private function setTimeLabel($image,$image_x,$image_y,$image_w,$image_h,$img_text)
{
imagealphablending($image,true);
//设定颜色
$color=imagecolorallocate($image,255,255);
$ttf_im=imagettfbBox(30,"Arial.ttf",$this->img_text);
$w = $ttf_im[2] - $ttf_im[6];
$h = $ttf_im[3] - $ttf_im[7];
unset($ttf_im);
$txt_y =$image_y+$image_h+$h-5;
$txt_x =$image_x+$w+5;
imagettftext($image,30,$txt_x,$txt_y,$color,$img_text);
return $image;
}
/**
设置视频信息标签
*/
private function setVideoInfoLabel($image,$videoInfo)
{
imagealphablending($image,true);
$color=imagecolorallocate($image,0);
imagettftext($image,32,100,2000+30,"FZLTHJW.ttf","FileName:".basename($videoInfo["file"]));
imagettftext($image,1600,"Size:".$videoInfo["width"]."x".$videoInfo["height"]);
imagettftext($image,2000+120,"Duration:".$videoInfo["duration"]);
imagettftext($image,"Bitrate:".$videoInfo["bitrate"]);
return $image;
}
/**
屏幕截图
*/
public function getScreenShoot($fileName)
{
$fi = pathinfo($fileName);
$this->videoInfo = $this->getVideoInfo($this->rootdir.DS.$this->source.DS.$fileName);
if($this->videoInfo)
{
$this->setShootSecends($this->videoInfo["secends"]);
for ($i=0; $i< count($this->timeArray); $i++ )
{
$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];
exec($cmd,$out,$status);
}
$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);
file_put_contents("log.txt",FILE_APPEND);
$this->successCount += 1;
}else
{
$str = sprintf("[%s]:Failed.................................[%s][%2dP]%-30s\n",FILE_APPEND);
$this->FailedCount += 1;
}
}
/**
$fileLoader = new FileLoader();
$fileLoader->searchDir();
?>
以上是脚本宝典为你收集整理的PHP调用ffmpeg对视频截图并拼接脚本全部内容,希望文章能够帮你解决PHP调用ffmpeg对视频截图并拼接脚本所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。