This article mainly introduces the PHP call FFmpeg to the video and stitching the script
PHP script calls FFmpeg video and stitching, for everyone to refer to, the specific content as follows
Currently supports video in common formats such as MKV,MPG,MP4, other formats to be tested
12P an average generation time 1.64s 100 videos, about 2 minutes and a half
9P an average generation time 1.13s 100 video, about 2 minutes
6P an average generation time 0.86s 100 videos, about 1 minutes and a half
3P an average generation time 0.54s 100 video, about 1 minutes
<?php define (' DS ', directory_separator); Date_default_timezone_set ("Asia/shanghai"); Class Fileloader {//PATH variable private $rootdir = '; Private $tmp = "tmp"; TMP directory Private $source = "mpg"; SOURCE directory Private $destination = "Screenshoot"; Target path Private $emptyImageName = "empty.jpg"; Synthetic background map//file array private $maxShoots = 12; The largest number of private $videoInfo = NULL; Private $files = Array (); Number of files Private $fileArray = Array (); Private $extensionArray = Array ("MPG", "mkv", "mp4", "Avi", "3gp", "mov"); Supported formats for 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 "); Statistic variable Private $timeStart = 0; Private $timeEnd = 0; Private $fileCount = 0; Private $successCount = 0; Private $failedCount = 0; /** * Initialization Information */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 "; }}/** * Current time, accurate to the decimal point */private static function Microtime_float () {list ($usec, $sec) = Explode ("", Microtime ()); return (float) $usec + (float) $sec); }/** * 00:00:00 time to seconds */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 * + $sec; return $secs; }/** * 00:00:00 time to seconds */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; }/** * Get all Files */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; }/** * Search path */Public Function searchdir ($sourcePath = NULL) {$this->timestart = $this->mic Rotime_float (); if ($sourcePath) {$this->rootdir = $sourcePath; } if (File_exists ($this->rootdir) && is_dir ($this->rootdir)) {$this->files = $this-& Gt;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); }}/** * Get video information */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" and $bitrate, "Secends" and $this->ti Metosec ($duration)); }}/** * Set time */Private Function Setshootsecends ($secends, $useDefault = NO) {if ($US EDefault) {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; }}}/** * Mosaic image */Private Function Getfixedphoto ($fileName) {$target = $this-≫rootdir. DS. $this->emptyimagename;//background image $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 the spacing between =5;//pictures 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 '); }/** * Set timescale label */Private Function Settimelabel ($image, $image _x, $image _y, $image _w, $image _h, $img _text) { Imagealphablending ($image, true); Set color $color =imagecolorallocate ($image, 255,255,255); $ttf _im=imagettfbbox (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; }/** * Set video information label */Private Function Setvideoinfolabel ($image, $txt _x, $txt _y, $videoInfo) {Imagealpha Blending ($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: * Capture pictures, * need to configure ffmpeg-php, more trouble, * but this class is very useful. */Public Function GetScreenShoot2 ($fileName) {if (extension_loaded (' ffmpeg ')) {//Determines if ffmpeg is loaded $mov = new FF Mpeg_movie ($this->rootdir. DS. $this->source. DS. $fileName);//The path of the video $count = $mov->getframecount (); $ff _frame = $mov->getframe (Floor ($count/2)); if ($ff _frame) {$gd _image = $ff _frame->togdimage (); $img = $this->rootdir. DS. " Test.jpg ";//to generate an absolute path to the picture imagejpeg ($gd _image, $img);//create JPG image imaGedestroy ($GD _image);//Destroy an image}}else{echo "FFmpeg not Loaded"; }}} $fileLoader = new Fileloader (); $fileLoader->searchdir ();?>