本文主要介绍了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分钟左右
<?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; /** * 初始化信息 */ 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"; } } /** * 当前时间,精确到小数点 */ private static function microtime_float() { list($usec, $sec)= explode(" ", microtime()); return ((float)$usec + (float)$sec); } /** * 00:00:00 时间转秒 */ 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 时间转秒 */ 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) ); } } /** * 设置截图时间 */ 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,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'); } /** * 设置时间刻度标签 */ private function setTimeLabel($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; } /** * 设置视频信息标签 */ 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,比较麻烦, * 但是这个类确实挺好用的。 */ 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";//要生成图片的绝对路径 imagejpeg($gd_image, $img);//创建jpg图像 imagedestroy($gd_image);//销毁一图像 } }else{ echo "ffmpeg没有载入"; } } } $fileLoader = new FileLoader(); $fileLoader->searchDir(); ?>
相关推荐:
HTML5/CSS3 诱人的实例 -模仿优酷视频截图功能的详解
js+HTML5实现视频截图的方法_javascript技巧
以上是PHP呼叫ffmpeg對影片截圖並拼接腳本實例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

PHP在現代編程中仍然是一個強大且廣泛使用的工具,尤其在web開發領域。 1)PHP易用且與數據庫集成無縫,是許多開發者的首選。 2)它支持動態內容生成和麵向對象編程,適合快速創建和維護網站。 3)PHP的性能可以通過緩存和優化數據庫查詢來提升,其廣泛的社區和豐富生態系統使其在當今技術棧中仍具重要地位。

在PHP中,弱引用是通過WeakReference類實現的,不會阻止垃圾回收器回收對象。弱引用適用於緩存系統和事件監聽器等場景,需注意其不能保證對象存活,且垃圾回收可能延遲。

\_\_invoke方法允許對象像函數一樣被調用。 1.定義\_\_invoke方法使對象可被調用。 2.使用$obj(...)語法時,PHP會執行\_\_invoke方法。 3.適用於日誌記錄和計算器等場景,提高代碼靈活性和可讀性。

Fibers在PHP8.1中引入,提升了並發處理能力。 1)Fibers是一種輕量級的並發模型,類似於協程。 2)它們允許開發者手動控制任務的執行流,適合處理I/O密集型任務。 3)使用Fibers可以編寫更高效、響應性更強的代碼。

PHP社區提供了豐富的資源和支持,幫助開發者成長。 1)資源包括官方文檔、教程、博客和開源項目如Laravel和Symfony。 2)支持可以通過StackOverflow、Reddit和Slack頻道獲得。 3)開發動態可以通過關注RFC了解。 4)融入社區可以通過積極參與、貢獻代碼和學習分享來實現。

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Dreamweaver Mac版
視覺化網頁開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3漢化版
中文版,非常好用