广告
返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >php实现ffmpeg处理视频的过程是怎样的
  • 661
分享到

php实现ffmpeg处理视频的过程是怎样的

2023-06-22 02:06:40 661人浏览 薄情痞子
摘要

本篇文章给大家分享的是有关PHP实现FFmpeg处理视频的过程是怎样的,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。最近有一个项目需要使用ffmpeg处理视频,这里我写了一个d

本篇文章给大家分享的是有关PHP实现FFmpeg处理视频的过程是怎样的,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

最近有一个项目需要使用ffmpeg处理视频,这里我写了一个demo,方便我们来实现视频操作

ffmpeg操作demo

<?phpnamespace common\helpers;use common\models\Config;use common\models\VideoapiLog;use Yii;use yii\helpers\ArrayHelper;use common\helpers\Universal;use yii\helpers\FileHelper;use yii\Httpclient\Client;use yii\WEB\ServerErrorHttpException;class FfmpegVideo{    public $ffmpeg = 'ffmpeg';    public function __construct($ffmpeg = null)    {        if ($ffmpeg) {            $this->ffmpeg = $ffmpeg;        }    }        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  false;    }        public function imageWater($source, $saveFile, $waterImage, $left, $top, $star = null, $duration = null)    {        $waterImage = str_replace('\\', '/', $waterImage);        $waterImage = str_replace(':', '\\:', $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  false;    }        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  false;    }        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  false;    }        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  false;    }        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  false;    }        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  false;    }        public function spliceVideo($sources, $saveFile)    {        $commands = [];        $temporaryFile = [];        $basePath = sys_get_temp_dir();        $index = 0;        foreach ($sources as $i => $source) {            $file = $basePath . '/' . $i . '.ts';            $commands[$index] = $this->ffmpeg . ' -y -i '. $source .' -vcodec copy -acodec copy -vbsf h364_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;    }        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  false;    }    const ROTATE_90 = 'transpose=1';    const ROTATE_180 = 'hflip,vflip';    const ROTATE_270 = 'transpose=2';        public function transposeVideo($source, $saveFile, $rotate)    {        $command = $this->ffmpeg . ' -y -i ' . $source . ' -vf ""transpose=1"" ' . $saveFile;        exec($command, $output, $result_code);        if ($result_code == 0) {            return true;        }        return  false;    }        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  false;    }        public function concatVideo($sources, $saveFile)    {        $file = $this->createTemporaryFile();        $fileStream = @fopen($file, 'w');        if($fileStream === false) {            throw new ServerErrorHttpException('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++;            }        }        else {            throw new ServerErrorHttpException('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);        fclose($fileStream);        @unlink($file);//删除文件        if ($result_code == 0) {            return true;        }        return  false;    }        public function createTemporaryFile()    {        $basePath = sys_get_temp_dir();        if (false === $file = @tempnam($basePath, null)) {            throw new ServerErrorHttpException('Unable to generate a temporary filename');        }        return $file;    }        public function getAttributes($source)    {        ob_start();        $command = $this->ffmpeg . ' -i "'. $source .'" 2>&1';        passthru($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' => intval($duration),            'widht' => intval($widht),            'height' => intval($height),        ];    }}

使用简单示例

这里注意如果无法执行ffmpeg,实例化时需要传入ffmpeg的安装地址,例如linux下ffmpeg安装地址为/usr/local/ffmepg,那么实例化时需要传入/usr/local/ffmpeg/bin/ffmpeg

1:给视频添加文字

$ffmpeg = new FfmpegVideo();$ffmpeg ->titleWater(    'XXX',//原视频    'XXX',//处理后保存视频    'XXX',//文字    [        'x' => 30,//水平距离        'y' => 30,//垂直距离        'fontsize' => 20,//文字大小        'fontcolor' => 'red',//文字颜色        'shadowy' => 2,//文字阴影    ],    200,//每秒移动步长    2//文字出现时间(秒));

2:将视频设为静音

$ffmpeg = new FfmpegVideo();$ffmpeg->audioMute(    'XXX',//原视频    'XXX',//处理后保存视频);

3:视频裁剪

$ffmpeg = new FfmpegVideo();$ffmpeg->clipVideo(    'XXX',//原视频    'XXX',//处理后保存视频    0,//裁剪开始时间    10//裁剪时长);

4:视频拼接

$ffmpeg = new FfmpegVideo();$ffmpeg->concatVideo(    ['XXX', 'XXX'],//需要拼接的视频    'XXX',//处理后保存视频);

5:将音频合并到视频中

$ffmpeg = new FfmpegVideo();$ffmpeg->mergeVideoAudio(    'XXX',//视频    'XXX',//音频    'XXX',//处理后保存视频    0//音频插入视频延时时间(秒));

6:获取视频信息(长,宽,时长)

$ffmpeg = new FfmpegVideo();$ffmpeg->getAttributes(    'XXX',//视频);

以上就是php实现ffmpeg处理视频的过程是怎样的,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网PHP编程频道。

--结束END--

本文标题: php实现ffmpeg处理视频的过程是怎样的

本文链接: https://www.lsjlt.com/news/302150.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • php实现ffmpeg处理视频的过程是怎样的
    本篇文章给大家分享的是有关php实现ffmpeg处理视频的过程是怎样的,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。最近有一个项目需要使用ffmpeg处理视频,这里我写了一个d...
    99+
    2023-06-22
  • php实现ffmpeg处理视频的实践
    最近有一个项目需要使用ffmpeg处理视频,这里我写了一个demo,方便我们来实现视频操作ffmpeg操作demo<php namespace common\helpers; use common\mo...
    99+
    2022-01-30
    ffmpeg 视频
  • Golang与FFmpeg: 实现实时视频流转发与处理的技术
    Golang与FFmpeg可以一起使用来实现实时视频流转发和处理的技术。Golang是一种高效的编程语言,而FFmpeg是一个强大的...
    99+
    2023-10-08
    Golang
  • ASP.NET的请求处理过程是怎样的
    这篇文章主要介绍“ASP.NET的请求处理过程是怎样的”,在日常操作中,相信很多人在ASP.NET的请求处理过程是怎样的问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”ASP.NET的请求处理过程是怎样的”的疑...
    99+
    2023-06-17
  • JavaScript 错误处理的解决过程是怎样的
    本篇文章为大家展示了JavaScript 错误处理的解决过程是怎样的,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。什么是编程中的错误我们的开发过程中并不总是一帆风顺...
    99+
    2022-10-19
  • Linux内核处理中断的过程是怎样的
    Linux内核处理中断的过程是怎样的,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。中断是现代 CPU 工作方式中重要的部分。例如:当你每次在键盘上按下一个按键后,CPU 会...
    99+
    2023-06-28
  • ASP.NET对请求处理过程的操作是怎样的
    本篇内容介绍了“ASP.NET对请求处理过程的操作是怎样的”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!当请求一个*.aspx文件的时候,这...
    99+
    2023-06-17
  • linux主机中病毒处理过程是怎么样的
    这期内容当中小编将会给大家带来有关linux主机中病毒处理过程是怎么样的,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。问题现象服务器一直往外大量发包,占用流量和cpu,导致服务器响应很慢甚至无响应,怀疑是...
    99+
    2023-06-05
  • Hive方便地实现存储过程是怎样的
    Hive方便地实现存储过程是怎样的,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。第一种是 HPL/SQL。这种方式目前还不完善,比如游标使用限制多,很多功能无法实现,对变量...
    99+
    2023-06-03
  • ASP.NET的HTTP模块和处理程序的模块实现是怎样的
    本篇文章给大家分享的是有关ASP.NET的HTTP模块和处理程序的模块实现是怎样的,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。实现一个提供安全服务的HTTP模块现在我们实现一...
    99+
    2023-06-17
  • Linux内核驱动fsync机制实现过程是怎样的
    Linux内核驱动fsync机制实现过程是怎样的,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。在Linux内核中的IO模型基本分为4类: 1、同步阻塞I/O 2、同步非阻塞I/...
    99+
    2023-06-13
  • SpringBoot实现文件在线预览功能的过程是怎样的
    SpringBoot实现文件在线预览功能的过程是怎样的,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。背景最近公司内部oa系统升级,需要增加文件在线预览服务,最常见的文件就是of...
    99+
    2023-06-25
  • PyTorch简单手写数字识别的实现过程是怎样的
    本篇文章给大家分享的是有关PyTorch简单手写数字识别的实现过程是怎样的,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。一、包导入及所需数据的下载torchvision包的主要...
    99+
    2023-06-25
  • Java实现学生信息管理系统的流程是怎样的
    这篇文章给大家介绍Java实现学生信息管理系统的流程是怎样的,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。一、项目简述功能包括: 用户的登录注册,学生信息管理,教师信息管理,班级信 息管理,采用mvcx项目架构,覆盖增...
    99+
    2023-06-25
  • 通过抓包实现Python模拟登陆各网站的原理分析是怎样的
    这篇文章将为大家详细讲解有关通过抓包实现Python模拟登陆各网站的原理分析是怎样的,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。  一、教程简介  1.1 基本介绍(私信小编001 、00...
    99+
    2023-06-02
  • Java 实战中精品养老院管理系统的实现流程是怎样的
    这期内容当中小编将会给大家带来有关Java 实战中精品养老院管理系统的实现流程是怎样的,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。一、项目简述本系统功能包括:通知公告,老人管理,护工管理,问答管理等等功...
    99+
    2023-06-25
  • 滑轮滚动到页面底部ajax加载数据配合jsonp实现过程是怎样的
    这期内容当中小编将会给大家带来有关滑轮滚动到页面底部ajax加载数据配合jsonp实现过程是怎样的,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。 ...
    99+
    2022-10-19
  • 怎么用shell脚本实现对OGG进程过期trail文件的删除处理
    本篇内容主要讲解“怎么用shell脚本实现对OGG进程过期trail文件的删除处理”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用shell脚本实现对OGG进程过期trail文件的删除处理”...
    99+
    2023-06-04
  • 处理大数据打包,PHP编程算法的实现技巧是什么?
    随着互联网的发展,越来越多的数据被产生和存储。对于处理大量数据时,我们需要考虑如何打包和压缩数据,以减少数据的存储和传输所占用的空间和时间。在PHP编程中,我们可以采用一些算法来实现数据打包和压缩。本文将介绍一些处理大数据打包和PHP编程...
    99+
    2023-06-24
    编程算法 大数据 打包
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作