最近有一个项目需要使用ffmpeg处理视频,这里我写了一个demo,方便我们来实现视频操作
<?phpnamespace common\helpers;u common\models\config;u common\models\videoapilog;u yii;u yii\helpers\arrayhelper;u common\helpers\universal;u yii\helpers\filehelper;u yii\httpclient\client;u yii\web\rvererrorhttpexception;/*** ffmpeg视频处理** @author wangjian* @since 0.1*/class ffmpegvideo{public $ffmpeg = 'ffmpeg';public function __construct($ffmpeg = null){if ($ffmpeg) {$this->ffmpeg = $ffmpeg;}}/*** 添加视频文字滚动* @param $source string 视频* @param $savefile string 保存文件* @param $text string 水印文字* @param array $options 水印样式* @param int $step 每秒步长* @param int $star 出现时间*/public function titlemod($source, $savefile, $text, $options = [], $step = 20, $star = 0){$command = $this->ffmpeg .' -y -i '. $source .' -async 1 -metadata:s:v:0 start_time=0 -vf ';$fonts = yii::getalias('@webroot') . "/fonts/simsun.ttc";$fonts = str_replace('\\', '/', $fonts);$fonts = str_replace(':', '\\:', $fonts);$command .= '"drawtext=fontfile=\''. $fonts .'\': text=\''. $text .'\'';foreach ($options as $key => $value) {$command .= ':' . $key . '=' . $value;}$command .= ':x=\'if(gte(t,'. $star .'),((t-'. $star .') * '. $step .'),nan)\'';$command .= '" ';$command .= $savefile;exec($command, $output, $result_code);if ($result_code == 0) {return true;}return fal;}/*** 图片水印* @param $source string 视频* @param $savefile string 保存文件* @param $waterimage string 水印图片* @param $left integer 水印水平位置* @param $top integer 水印垂直位置* @param null $star 水印开始时间* @param null $duration 水印时长*/public function imagewater($source, $savefile, $waterimage, $left, $top, $star = null, $duration = null){$waterimage = str_replace('\\', '/', $waterimage);$waterimage = str_repla杨根思烈士陵园ce(':', '\\:', $waterimage);$command = $this->ffmpeg . ' -y -i '. $source .' -vf "movie=\''. $waterimage .'\'[watermark];';$command .= '[in][watermark] overlay='. $left .':'. $top;if ($star) {$end = ($duration) ? $star + $duration : $star;$command .= ':enable=\'between(t,'. $star .','. $end .')\'';}$command .= '[out] " ' . $savefile;exec($command, $output, $result_code);if ($result_code == 0) {return true;}return fal;}/*** 给视频添加文字水印* @param $source string 视频* @param $savefile string 保存文件* @param $text string 水印文字* @param array $options 水印样式* @param null $star 水印开始时间* @param null $duration 水印时长*/public function titlewater($source, $savefile, $text, $options = [], $star = null, $duration = null){$command = $this->ffmpeg .' -y -i '. $source .' -vf ';$fonts = yii::getalias('@webroot') . "/fonts/stzhongs.ttf";$fonts = str_replace('\\', '/', $fonts);$fonts = str_replace(':', '\\:', $fonts);$command .= '"drawtext=fontfile=\''. $fonts .'\': text=\''. $text .'\'';foreach ($options as $key => $value) {$command .= ':' . $key . '=' . $value;}if ($star) {$end = ($duration) ? $star + $duration : $star;$command .= ':enable=\'between(t,'. $star .','. $end .')\'';}$command .= '" ';$command .= $savefile;exec($command, $output, $result_code);if ($result_code == 0) {return true;}return fal;}/*** 将音频合并到视频中* @param $videofile string 视频文件* @param $audiofile string 音频文件* @param $savefile string 保存文件* @param $delay integer 声音插入延时秒数*/public function mergevideoaudio($videofile, $audiofile, $savefile, $delay = null){$delaytime = 0;if ($delay) {$delaytime = $delay * 1000;}$command = $this->ffmpeg . ' -y -i '. $audiofile .' -i '. $videofile .' -c:v copy -c:a aac -strict experimental -filter_complex "[0]adelay='. $delaytime .'|'. $delaytime .'[del1],[1][del1]amix" ' . $savefile;exec($command, $output, $result_code);if ($result_code == 0) {return true;}return fal;}/*** 静音*/public function audiomute($source, $savefile){$command = $this->ffmpeg . ' -y -i '. $source .' -filter:a "volume=0" ' . $savefile;exec($command, $output, $result_code);if ($result_code == 0) {return true;}return fal;}/*** 提取视频的音频* @param $source string 需要提取声音的视频* @param $savefile string 提取声音后保存的音频* @return bool*/public function collectaudio($source, $savefile){$command = $this->ffmpeg . ' -y -i '. $source .' -vn -acodec copy ' . $savefile;exec($command, $output, $result_code);if ($result_code == 0) {return true;}return fal;}/*** 去除视频声音* @param $source string 需要去除声音的视频* @param $savefile string 去除声音后保存的视频*/public function removeaudio($source, $savefile){$command = $this->ffmpeg . ' -y -i '. $source .' -an ' . $savefile;exec($command, $output, $result_code);if ($result_code == 0) {return true;}return fal;}/*** 视频拼接* @param $sources array 需要拼接的视频/音频* @param $savefile string 拼接后的视频/音频*/public function splicevideo($sources, $savefile){$commands = [];$temporaryfile = [];$bapath = sys_get_temp_dir();$index = 0;foreach ($sources as $i => $source) {$file = $bapath . '/' . $i . '.ts';$commands[$index] = $this->ffmpeg . ' -y -i '. $source .' -vcodec copy -acodec copy -vbsf h264_mp4toannexb ' . $file;$temporaryfile[] = $file;$index++;}$commands[$index] = $this->ffmpeg . ' -y -i "concat:'. implode('|', $temporaryfile) .'" -acodec copy -vcodec copy -absf aac_adtstoasc ' . $savefile;foreach ($commands as $command) {exec($command, $output, $result_code);}foreach ($temporaryfile as $file) {@unlink($file);}return true;}/*** 视频剪切* @param $source string 需要剪切视频/音频* @param $savefile string 剪切后保存视频/音频* @param $star string 剪切开始时间* @param null $duration string 剪切时长*/public function clipvideo($source, $savefile, $star, $duration = null){$command = $this->ffmpeg . ' -y -ss '. $star;if ($duration) {$command .= ' -t '. $duration;}$command .= ' -i '. $source .' -acodec copy ' . $savefile;exec($command, $output, $result_code);if ($result_code == 0) {return true;}return fal;}const rotate_90 = 'transpo=1';const rotate_180 = 'hflip,vflip';const rotate_270 = 'transpo=2';/*** 视频旋转* @param $source string 需要旋转的视频* @param $savefile string 旋转后视频* @param $rotate string 旋转角度*/public function transpovideo($source, $savefile, $rotate){$command = $this->ffmpeg . ' -y -i ' . $source . ' -vf ""transpo=1"" ' . $savefile;exec($command, $output, $result_code);if ($result_code == 0) {return true;}return fal;}/*** 视频转码* @param $source string 需要转码的视频/音频* @param $savefile string 转码后的视频/音频*/public function acodecvideo($source, $savefile){$command = $this->ffmpeg . ' -y -i '. $source .' -acodec copy -vcodec copy -f mp4 ' . $savefile;exec($command, $output, $result_code);if ($result_code == 0) {return true;}return fal;}/*** 视频拼接* @param $sources array 需要拼接的视频/音频* @param $savefile string 拼接后的视频/音频*/public function concatvideo($sources, $savefile){$file = $this->createtemporaryfile();$filestream = @fopen($file, 'w');if($filestream === fal) {throw new rvererrorhttpexception('cannot open the temporary file.');}$count_videos = 0;if(is_array($sources) && (count($sources) > 0)) {foreach ($sources as $videopath) {$line = "";if($count_videos != 0)$line .= "\n";$line .= "file '". str_replace('\\','/',$videopath) ."'";fwrite($filestream, $line);$count_videos++;}}el {throw new rvererrorhttpexception('the list of videos is not a valid array.');}$command = $this->ffmpeg .' -y -f concat -safe 0 -i '. $file . ' -c copy ' . $savefile;exec($command, $output, $result_code);fclo($filestream);@unlink($file);//删除文件if ($result_code == 0) {return true;}return fal;}/*** 创建一个临时文件*/public function createtemporaryfile(){$bapath = sys_get_temp_dir();if (fal === $file = @tempnam($bapath, null)) {throw new rvererrorhttpexception('unable to generate a temporary filename');}return $file;}/***水浒传章节目录 获取视频信息* @param $source string 需要获取时长的资源*/public function getattributes($source){ob_start();$command = $this->ffmpeg . ' -i "'. $source .'" 2>&1';passthru(18大主要内容$command);$getcontent = ob_get_contents();ob_end_clean();$duration = 0;$widht = 0;$height = 0;if (preg_match("/duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $getcontent, $match)) {$matchs = explode(':', $match[1]);$duration = $matchs[0] * 3600 + $matchs[1] * 60 + $matchs[2]; //转换播放时间为秒数}if (preg_match("/video: (.*?), (.*?), (.*?)[,\s]/", $getcontent, $match)) {$matchs = explode('x', $match[3]);$widht = $matchs[0];$height = $matchs[1];}return ['duration' => intv瑞脑消金兽al($duration),'widht' => intval($widht),'height' => intval($height),];}}
这里注意如果无法执行ffmpeg,实例化时需要传入ffmpeg的安装地址,例如linux下ffmpeg安装地址为/usr/local/ffmepg,那么实例化时需要传入/usr/local/ffmpeg/bin/ffmpeg
$ffmpeg = new ffmpegvideo();$ffmpeg ->titlewater('xxx',//原视频'xxx',//处理后保存视频'xxx',//文字['x' => 30,//水平距离'y' => 30,//垂直距离'fontsize' => 20,//文字大小'fontcolor' => 'red',//文字颜色'shadowy' => 2,//文字阴影],200,//每秒移动步长2//个性喜帖文字出现时间(秒));
$ffmpeg = new ffmpegvideo();$ffmpeg->audiomute('xxx',//原视频'xxx',//处理后保存视频);
$ffmpeg = new ffmpegvideo();$ffmpeg->clipvideo('xxx',//原视频'xxx',//处理后保存视频0,//裁剪开始时间10//裁剪时长);
$ffmpeg = new ffmpegvideo();$ffmpeg->concatvideo(['xxx', 'xxx'],//需要拼接的视频'xxx',//处理后保存视频);
$ffmpeg = new ffmpegvideo();$ffmpeg->mergevideoaudio('xxx',//视频'xxx',//音频'xxx',//处理后保存视频0//音频插入视频延时时间(秒));
$ffmpeg = new ffmpegvideo();$ffmpeg->getattributes('xxx',//视频);
到此这篇关于php实现ffmpeg处理视频的实践的文章就介绍到这了,更多相关php ffmpeg处理视频内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 03:41:28,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/b3a03befadeaf5de27e3f948dd94bcce.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:php实现ffmpeg处理视频的实践.doc
本文 PDF 下载地址:php实现ffmpeg处理视频的实践.pdf
留言与评论(共有 0 条评论) |