Ubuntu 18.04 安裝FFmpeg
1.下載源碼編譯安裝
1.1 下載源碼
github地址:github.com/PHP-FFMpeg/PHP-FFMpeg
1.2 安裝依賴的庫
主要安裝三個(gè):yasm ,sdl1.2 和 sdl2.0
安裝 yasmsudo apt-get install yasm 安裝sdl1.2sudo apt-get install libsdl1.2-dev 安裝 sdl2.0sudo apt-get install libstdl2-devsudo apt-get install libstdl2-dev
如果sdl2.0 安裝出現(xiàn)錯(cuò)誤的話可以選擇編譯安裝方式:
官網(wǎng)下載最新版本: www.libsdl.org/download-2.0.php
解壓后進(jìn)入到目錄中,依次執(zhí)行以下命令:
./configure make sudo make install
1.3編譯安裝ffmpeg
進(jìn)入ffmpeg文件夾,依次執(zhí)行以下命令:
./configuremakesudo make install
在這里插入圖片描述
1.4 測試是否安裝成功
ffmpeg -version ffplay -version
laravel 安裝PHP-FFMpeg擴(kuò)展
composer require php-ffmpeg/php-ffmpeg
基本使用
1.1、 引入到項(xiàng)目
引入完成,它需要制定 兩個(gè)配置文件信息,以便我們正常使用,也就是上文所講的 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
對(duì)象,其中 exec('which ffmpeg')
是獲取 程序位置信息,以便創(chuàng)建類
基礎(chǔ)封裝
舉例:
- 視頻的第一秒為封面
- 獲取視頻基礎(chǔ)信息
<?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; }}
業(yè)務(wù)使用
接受 Request 對(duì)象傳入的 視頻 為例子
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.設(shè)置視頻信息 $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
是將圖片上傳到 云端的函數(shù),返回上傳后的圖片 url