加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP调用ffmpeg对视频截图并拼接脚本

发布时间:2021-04-01 12:14:34 所属栏目:PHP教程 来源:网络整理
导读:副标题#e# PHP脚本调用ffmpeg对视频截图并拼接,供大家参考,具体内容如下 目前支持MKV,MPG,MP4等常见格式的视频,其他格式有待测试 12P 一张截图平均生成时间 1.64s 100个视频,大概需要2分半左右 9P 一张截图平均生成时间 1.13s 100个视频,大概需要2分钟

$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)
);
}
}

/**

  • 设置截图时间
    */
    private function setShootSecends($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; 
  } 
} 

}

/**

  • 拼接图片
    */
    private function getFixedPhoto($fileName)
    {
$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');  

}

/**

  • 设置时间刻度标签
    */
    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]%-30sn",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]%-30sn",FILE_APPEND);
    $this->failedCount += 1;
    }
    }

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读