github位址:github.com/PHP-FFMpeg/PHP-FFMpeg
#主要安裝三個: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 安裝出現錯誤的話可以選擇編譯安裝方式:
官網下載最新版本: www.libsdl.org/download-2.0.php
解壓縮後來進入目錄中,依序執行以下指令:
./configure make sudo make install
1.3編譯安裝ffmpeg
進入ffmpeg資料夾,依序執行以下指令:
./configuremakesudo make install
在這裡插入圖片描述
1.4 測試是否安裝成功
ffmpeg -version ffplay -version
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
更多laravel技術文章,請造訪 laravel教學專欄!
以上是詳解laravel如何安裝FFmpeg並進行視訊檔案處理的詳細內容。更多資訊請關注PHP中文網其他相關文章!