laravel 使用PHP-FFMpeg处理视频文件

鑫鑫
阅读 170 标签:php  

Ubuntu 18.04 安装 FFmpeg
1. 下载源码编译安装
1.1 下载源码
github 地址:github.com/PHP-FFMpeg/PHP-FFMpeg

1.2 安装依赖的库
主要安装三个:yasm ,sdl1.2 和 sdl2.0

安装 yasm
sudo apt-get install yasm
安装sdl1.2
sudo apt-get install libsdl1.2-dev
安装 sdl2.0
sudo apt-get install libstdl2-devsudo apt-get install libstdl2-dev

如果 sdl2.0 安装出现错误的话可以选择编译安装方式:
官网下载最新版本: www.libsdl.org/download-2.0.php
解压后进入到目录中,依次执行以下命令:

./configure
make
sudo make install

1.3 编译安装 ffmpeg
进入 ffmpeg 文件夹,依次执行以下命令:

laravel 使用PHP-FFMpeg处理视频文件

 ./configure
make
sudo make install

在这里插入图片描述
1.4 测试是否安装成功


ffmpeg -version
ffplay -version

查看 ffprobe 所在位置 whereis ffprobe
查看 whereis ffmpeg 所在位置 whereis whereis ffmpeg

laravel 安装 PHP-FFMpeg 扩展

composer require php-ffmpeg/php-ffmpeg

基本使用
1.1、 引入到项目
引入完成,它需要制定 两个配置文件信息,以便我们正常使用,也就是上文所讲的 ffmpeg 和 ffprobe
1.2、全局配置

到 AppServiceProvider.php 中添加代码

    public function boot()
    {
        $this->registerSingleObject();
    }
     private function registerSingleObject()
    {
//       $ffmpeg = FFMpeg::create(array(
//           'ffmpeg.binaries'  => '/usr/local/ffmpeg/ffmpeg',
//           'ffprobe.binaries' => '/usr/local/ffmpeg/ffprobe',
//           'timeout'          => 3600, // The timeout for the underlying process
//           'ffmpeg.threads'   => 12,   // The number of threads that FFMpeg should use
//       ));
        $this->app->singleton('ffmpeg', function ($app) {
            return FFMpeg::create([
                'ffmpeg.binaries'  => '/usr/local/ffmpeg/ffmpeg',
                'ffprobe.binaries' => '/usr/local/ffmpeg/ffprobe',
            ]);
        });
        $this->app->singleton('ffprobe', function ($app) {
            return FFProbe::create([
                'ffprobe.binaries' => '/usr/local/ffmpeg/ffprobe',
            ]);
        });
    }

使用单例模式获取 FFMpeg 和 FFProbe 对象,其中 exec('which ffmpeg') 是获取 程序位置信息,以便创建类

基础封装

举例:

  • 视频的第一秒为封面
  • 获取视频基础信息
  • <?php
    
    namespace AppHelpers;
    
    use FFMpegCoordinateTimeCode;
    use IlluminateSupportStr;
    
    class FFMpegUtil
    {
    
        // 获取视频信息
        public static function getVideoInfo($streamPath)
        {
            $ffprobe = app('ffprobe');
            $stream  = $ffprobe->streams($streamPath)->videos()->first();
            return $stream ? $stream->all() : [];
        }
    
        // 截取
        public static function getCover($streamPath, $fromSecond)
        {
            $ffmpeg   = app('ffmpeg');
            $video    = $ffmpeg->open($streamPath);
            $frame    = $video->frame(TimeCode::fromSeconds($fromSecond)); //提取第几秒的图像
            $fileName = 'video/' . Str::random(12) . '.jpg';
            if (!is_dir(storage_path("video"))) {
                mkdir(storage_path("video"), 0777);
            }
            $frame->save(storage_path($fileName));
            return $fileName;
        }
    }

业务使用

接受 Request 对象传入的 视频 为例子

public function saveVideotoQiniu($file)
    {
        Auth::loginUsingId(1);
        if ($user = getUser()) {

            // 1.判断是否存在此视频
            $path  = $file->getRealPath();
            $hash  = md5_file($path);
            $video = Video::firstOrNew(['json->hash' => $hash]);
            if ($video->id) {
                $video->touch();
                return $video;
            }

            // 2.保存到 云
            $cdn_path = $this->saveFile($file);
            $db_path  = getPath($cdn_path);

            // 3.获取截图
            $fileName = FFMpegUtil::getCover($path, 1);
            $image    = $this->saveImage(new UploadedFile(storage_path($fileName), 'file.jpg'));

            //4.设置视频信息
            $data     = [];
            $data     = FFMpegUtil::getVideoInfo($path);
            $duration = array_get($data, 'duration');
            $duration = $duration > 0 ? ceil($duration) : $duration;

            $video->path    = $db_path;
            $video->user_id = $user->id;
            $video->setJsonData('width', array_get($data, 'width'));
            $video->setJsonData('height', array_get($data, 'height'));
            $video->duration = $duration;
            $video->setJsonData('cover', $image->path);
            $video->save();
        }
    }

例子中的 saveImage 是将图片上传到 云端的函数,返回上传后的图片 url

文章来源:网络 版权归原作者所有,如涉及知识产权问题,请权利人联系我们,我们将立即处理.
标签: php
鑫鑫
文章 48 获得 0个赞 共 0个粉丝

推荐阅读 更多精彩内容