麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 編程 > PHP > 正文

PHP調(diào)用ffmpeg對(duì)視頻截圖并拼接腳本

2020-03-22 20:13:52
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了PHP調(diào)用ffmpeg對(duì)視頻截圖并拼接腳本

PHP腳本調(diào)用ffmpeg對(duì)視頻截圖并拼接,供大家參考,具體內(nèi)容如下

目前支持MKV,MPG,MP4等常見(jiàn)格式的視頻,其他格式有待測(cè)試

12P 一張截圖平均生成時(shí)間 1.64s 100個(gè)視頻,大概需要2分半左右

9P 一張截圖平均生成時(shí)間 1.13s 100個(gè)視頻,大概需要2分鐘左右

6P 一張截圖平均生成時(shí)間 0.86s 100個(gè)視頻,大概需要1分半左右

3P 一張截圖平均生成時(shí)間 0.54s 100個(gè)視頻,大概需要1分鐘左右


 ?php define( DS , DIRECTORY_SEPARATOR); date_default_timezone_set( Asia/Shanghai html' target='_blank'>class FileLoader  //路徑變量  private $rootdir =  private $tmp = tmp //tmp 目錄  private $source = mpg //source 目錄  private $destination = screenshoot //目標(biāo)截圖路徑  private $emptyImageName = empty.jpg //合成的背景圖  //文件數(shù)組  private $maxShoots = 12; //最大的截圖數(shù)  private $videoInfo = NULL;  private $files = array(); //文件數(shù)  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  //統(tǒng)計(jì)變量  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  /**  * 當(dāng)前時(shí)間,精確到小數(shù)點(diǎn)  private static function microtime_float()  list($usec, $sec)= explode( , microtime());  return ((float)$usec + (float)$sec);  /**  * 00:00:00 時(shí)間轉(zhuǎn)秒  private static function timeToSec($time)  $p = explode( : ,$time);  $c = count($p);  if ($c 1)  $hour = intval($p[0]);  $minute = intval($p[1]);  $sec = intval($p[2]);  else  throw new Exception( error time format  $secs = $hour * 3600 + $minute * 60 + $sec;  return $secs;  /**  * 00:00:00 時(shí)間轉(zhuǎn)秒  private static function secToTime($time)  $hour = floor($time/3600);  $min = floor(($time - $hour * 3600)/60);  $sec = $time % 60;  $timeStr = sprintf( %02d:%02d:%02d ,$hour,$min,$sec);  return $timeStr;  /**  * 獲取全部文件  private function getFiles($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;  /**  * 搜索路徑  public function searchDir($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- 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] | 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( [TOTAL]: Cost Time:%8s | Total File:[%d] | Successed:[%d] | Failed:[%d] | Speed:%.2fs per file/n ,$this- secToTime($time),$this- fileCount,$this- successCount,$this- failedCount,0);  file_put_contents( log.txt ,$str,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:(.*?),/ , $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){  return false;  }else{  return array(  file = $file,  width = $width,  height = $height,  duration = $duration,  bitrate = $bitrate,  secends = $this- timeToSec($duration) 
$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,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 /** * 設(shè)置時(shí)間刻度標(biāo)簽 private function setTimeLabel($image,$image_x,$image_y,$image_w,$image_h,$img_text) imagealphablending($image,true); //設(shè)定顏色 $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; /** * 設(shè)置視頻信息標(biāo)簽 private function setVideoInfoLabel($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; /** * 屏幕截圖 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 ,$str,FILE_APPEND); $this- successCount += 1; }else $str = sprintf( [%s]:FAILED.................................[%s][%2dP]%-30s/n ,date( y-m-d h:i:s ,time()),$this- videoInfo[ duration ],count($this- timeArray),$fileName); file_put_contents( log.txt ,$str,FILE_APPEND); $this- failedCount += 1; /** * TODO: * 截取圖片, * 需要配置ffmpeg-php,比較麻煩, * 但是這個(gè)類(lèi)確實(shí)挺好用的。 public function getScreenShoot2($fileName) if(extension_loaded( ffmpeg )){//判斷ffmpeg是否載入 $mov = new ffmpeg_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 //要生成圖片的絕對(duì)路徑 imagejpeg($gd_image, $img);//創(chuàng)建jpg圖像 imagedestroy($gd_image);//銷(xiāo)毀一圖像 }else{ echo ffmpeg沒(méi)有載入 $fileLoader = new FileLoader(); $fileLoader- searchDir(); ?

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持php 。

您可能感興趣的文章:

Yii2中的場(chǎng)景(scenario)和驗(yàn)證規(guī)則(rule)的詳解

MixPHP、Yii和CodeIgniter的并發(fā)壓力測(cè)試的小結(jié)

PHP基于非遞歸算法實(shí)現(xiàn)先序、中序及后序遍歷二叉樹(shù)操作的示例

以上就是PHP調(diào)用ffmpeg對(duì)視頻截圖并拼接腳本的詳細(xì)內(nèi)容,PHP教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄色免费影片 | 亚洲3atv精品一区二区三区 | 国产一级毛片a | 成人羞羞视频在线观看 | 色中色激情影院 | 一色桃子av大全在线播放 | 亚洲第一视频 | 久久国产精品二区 | 成人精品久久久 | 又黄又爽又色无遮挡免费 | 国产一级毛片a | 黄色电影免费提供 | 日本在线视频二区 | 亚洲精品成人在线视频 | 日韩精品 | 成人在线网站 | 免费观看国产精品视频 | 成年人免费视频播放 | 看免费一级毛片 | 国产一区二区三区四区五区加勒比 | 欧美国产精品久久 | 亚洲啊v在线观看 | 毛片av网址 | 中文字幕在线免费 | hdhdhd69ⅹxxx黑人| 91真视频 | 欧美大逼网| 91精品国产乱码久久久久久久久 | 蜜桃传媒视频麻豆第一区免费观看 | 久久线视频 | 在线看成人av | 亚洲成人免费电影 | 国产精品亚洲综合 | 看黄在线| 凹凸成人精品亚洲精品密奴 | 国产精品久久久久久模特 | 中文字幕在线资源 | 污污的视频在线观看 | 99激情视频 | 国产精品欧美久久久久一区二区 | 国产免费久久久久 |