您正在使用 IPV4 [44.211.31.134] 访问本站,您本次已经查看了 1 页
用户名: 密 码: 验证码:     用QQ登录本站
首页 软件 编程 笑话 知识 公告 台风 日历 计算器
悟空收录网       [公益]保护绿色环境,构建和谐社会      

【腾讯云】2核2G4M云服务器新老同享99元/年,续费同价      
[公益] 地球是我家,绿化靠大家      
2024年 七夕节 028
2025年 高 考 329
2025年 元 旦 172
2025年 春 节 200
 
您现在的位置:首页 >> PHP >> 内容
本类新增
本类热门
PHP调用ffmpeg对视频截图并拼接脚本
内容摘要: ?phpdefine('DS',DIRECTORY_SEPARATOR);date_default_timezone_set('Asia/Shanghai');classFileLoader{//路径变量private$rootdir='';private$tmp='tmp';//tmp目录private$source='mpg';//source目录pri......
<?php

define('DS',DIRECTORY_SEPARATOR);

date_default_timezone_set("Asia/Shanghai");

classFileLoader

{

//路径变量

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";

}

}

/**

*当前时间,精确到小数点

*/

privatestaticfunctionmicrotime_float()

{

list($usec,$sec)=explode("",microtime());

return((float)$usec+(float)$sec);

}

/**

*00:00:00时间转秒

*/

privatestaticfunctiontimeToSec($time)

{

$p=explode(':',$time);

$c=count($p);

if($c>1)

{

$hour=intval($p[0]);

$minute=intval($p[1]);

$sec=intval($p[2]);

}

else

{

thrownewException('errortimeformat');

}

$secs=$hour*3600+$minute*60+$sec;

return$secs;

}

/**

*00:00:00时间转秒

*/

privatestaticfunctionsecToTime($time)

{

$hour=floor($time/3600);

$min=floor(($time-$hour*3600)/60);

$sec=$time%60;

$timeStr=sprintf("%02d:%02d:%02d",$hour,$min,$sec);

return$timeStr;

}

/**

*获取全部文件

*/

privatefunctiongetFiles($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;

}

/**

*搜索路径

*/

publicfunctionsearchDir($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->filesas$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]:CostTime:%8s|TotalFile:[%d]|Successed:[%d]|Failed:[%d]|Speed:%.2fsperfile\n",$this->secToTime($time),$this->fileCount,$this->successCount,$this->failedCount,$time/$this->fileCount);

file_put_contents("log.txt",$str,FILE_APPEND);

}

else

{

$str=sprintf("[TOTAL]:CostTime:%8s|TotalFile:[%d]|Successed:[%d]|Failed:[%d]|Speed:%.2fsperfile\n",$this->secToTime($time),$this->fileCount,$this->successCount,$this->failedCount,0);

file_put_contents("log.txt",$str,FILE_APPEND);

}

}

/**

*获取视频信息

*/

privatefunctiongetVideoInfo($file){

$re=array();

exec(".".DS."ffmpeg-i{$file}2>&1",$re);

$info=implode("\n",$re);

if(preg_match("/Nosuchfileordirectory/i",$info))

{

returnfalse;

}

if(preg_match("/Invaliddata/i",$info)){

returnfalse;

}

$match=array();

preg_match("/\d{2,}x\d+/",$info,$match);

list($width,$height)=explode("x",$match[0]);

$match=array();

preg_match("/Duration:(.*?),/",$info,$match);

if($match)

{

$duration=date("H:i:s",strtotime($match[1]));

}else

{

$duration=NULL;

}

$match=array();

preg_match("/bitrate:(.*kb\/s)/",$info,$match);

$bitrate=$match[1];

if(!$width&&!$height&&!$duration&&!$bitrate){

returnfalse;

}else{

returnarray(

"file"=>$file,

"width"=>$width,

"height"=>$height,

"duration"=>$duration,

"bitrate"=>$bitrate,

"secends"=>$this->timeToSec($duration)

);

}

}

/**

*设置截图时间

*/

privatefunctionsetShootSecends($secends,$useDefault=NO)

{

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;

}

}

}

/**

*拼接图片

*/

privatefunctiongetFixedPhoto($fileName)

{

$target=$this->rootdir.DS.$this->emptyImageName;//背景图片

$target_img=Imagecreatefromjpeg($target);

$source=array();

foreach($this->fileArrayas$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,0,0,$source[$i]['size'][0],$source[$i]['size'][1]);

$target_img=$this->setTimeLabel($target_img,$tmpx,$tmpy,$source[$i]['size'][0],$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,$tmpx,$tmpy,$this->videoInfo);

Imagejpeg($target_img,$this->rootdir.DS.$this->destination.DS.$fileName.'.jpg');

}

/**

*设置时间刻度标签

*/

privatefunctionsetTimeLabel($image,$image_x,$image_y,$image_w,$image_h,$img_text)

{

imagealphablending($image,true);

//设定颜色

$color=imagecolorallocate($image,255,255,255);

$ttf_im=imagettfbbox(30,0,"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,0,$txt_x,$txt_y,$color,"Arial.ttf",$img_text);

return$image;

}

/**

*设置视频信息标签

*/

privatefunctionsetVideoInfoLabel($image,$txt_x,$txt_y,$videoInfo)

{

imagealphablending($image,true);

$color=imagecolorallocate($image,0,0,0);

imagettftext($image,32,0,100,2000+30,$color,"FZLTHJW.ttf","FileName:".basename($videoInfo["file"]));

imagettftext($image,32,0,1600,2000+30,$color,"Arial.ttf","Size:".$videoInfo["width"]."x".$videoInfo["height"]);

imagettftext($image,32,0,100,2000+120,$color,"Arial.ttf","Duration:".$videoInfo["duration"]);

imagettftext($image,32,0,1600,2000+120,$color,"Arial.ttf","Bitrate:".$videoInfo["bitrate"]);

return$image;

}

/**

*屏幕截图

*/

publicfunctiongetScreenShoot($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-fimage2-s720*480-vframes1".$this->rootdir.DS.$this->fileArray[$i];

exec($cmd,$out,$status);

}

$this->getFixedPhoto($fileName);

$str=sprintf("[%s]:OK...........[%s][%2dP]%-30s\n",date("y-m-dh:i:s",time()),$this->videoInfo["duration"],count($this->timeArray),$fileName);

file_put_contents("log.txt",$str,FILE_APPEND);

$this->successCount+=1;

}else

{

$str=sprintf("[%s]:FAILED.................................[%s][%2dP]%-30s\n",date("y-m-dh:i:s",time()),$this->videoInfo["duration"],count($this->timeArray),$fileName);

file_put_contents("log.txt",$str,FILE_APPEND);

$this->failedCount+=1;

}

}

/**

*TODO:

*截取图片,

*需要配置ffmpeg-php,比较麻烦,

*但是这个类确实挺好用的。

*/

publicfunctiongetScreenShoot2($fileName)

{

if(extension_loaded('ffmpeg')){//判断ffmpeg是否载入

$mov=newffmpeg_movie($this->rootdir.DS.$this->source.DS.$fileName);//视频的路径

$count=$mov->getFrameCount();

$ff_frame=$mov->getFrame(floor($count/2));

if($ff_frame)

{

$gd_image=$ff_frame->toGDImage();

$img=$this->rootdir.DS."test.jpg";//要生成图片的绝对路径

imagejpeg($gd_image,$img);//创建jpg图像

imagedestroy($gd_image);//销毁一图像

}

}else{

echo"ffmpeg没有载入";

}

}

}

$fileLoader=newFileLoader();

$fileLoader->searchDir();

?>

版权声明:本内容来源于网络,如有侵犯您的版权,请联系站长,本站收到您的信息后将及时处理。
上一篇:PHP实现格式化文件数据大小显示的方法

 

下一篇:PHP+Ajax实现的检测用户名功能简单示例

发布日期:2024/6/29
手机扫二维码直达本页
发布时间:22:07:35
点  击:8
录  入:壹家怡园
相关文章
Baidu
YiJiaCMS 7.5.1 build240527(MSSQL) 闽ICP备05000814号-1
本空间由腾讯云(轻量应用服务器)提供,速盾网络提供加速防护
运行时间载入中.....