Introduction
- First of all, install FFmpeg in your composer. Perform the steps below to install it on Ubuntu:
composer require pbmedia/laravel-FFmpeg
- Start by updating the packages list:
$ sudo apt update
- In your app.php use these
In providers
Pbmedia\LaravelFFmpeg\FFmpegServiceProvider::class
- In your controller
Use the following on top of the controller-use FFmpeg;
use FFmpeg\Coordinate\Dimension;
use FFmpeg\Format\Video\X264;
– Next, install FFmpeg by typing the following command:
$ sudo apt install FFmpeg
– To check FFmpeg is installed properly on your server.use following command:
$ FFmpeg -version
– The output looks something like this:
FFmpeg -version
FFmpeg version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2000-2019 the FFmpeg developers
– To print available FFmpeg’s encoders and decoders type:
$ FFmpeg -encoders
$ FFmpeg -decoders
That’s it. FFmpeg 3 is now installed properly on your system and you can start using it by the commands.
– In aliases
'FFmpeg' => Pbmedia\LaravelFFmpeg\FFmpegFacade::class
– In your controller method:
$file = $request->MediaFile;
$fileNameArray = explode(".", $file->getClientOriginalName());
if ($fileType == 'video'){
$filename = $file->getClientOriginalName(); //get name of file.
$extension = $file->extension(); //to get extension of file
$storage_path= Storage::disk('public')->makeDirectory('uploads'); // to make directory uploads in public folder
$storage_path_full = '/'.$filename; //to make variable for storage path
$localVideo = Storage::disk('public')->put($storage_path_full, file_get_contents($file)); //to put file in public folder
if($extension == 'mp4'){
$compressedFileName = $fileNameArray[0] . date('his') . '.' . $extension;
$docPath = 'userMedia/Story/' . $fileNameArray[0] . date('his') . '.' . $extension;
}
else{
$compressedFileName = $fileNameArray[0] . date('his') . '.mp4' ;
$docPath = 'userMedia/Story/' . $fileNameArray[0] . date('his') . '.mp4' ;
}
$time = Carbon::now();
$fileArray =[
'filename'=>$filename,
'extension'=>$extension,
'docPath'=>$docPath,
'name'=>Auth::user()->name,
'email'=>Auth::user()->email,
'compressedFileName'=>$compressedFileName,
];
$fileObject = (object)$fileArray;
$response = uploadMedia::dispatch($fileObject);
}
In your job use echo shell for command
Make job class in your app:-
php artisan make:job compressVideo
When you run this command compressVideo.php file is created in your App\Jobs folder
– Go to your job class and at the top use these namespaces-
use FFmpeg;
use FFmpeg\Coordinate\Dimension;
use FFmpeg\Format\Video\X264;
– In your construct function-
public function __construct($file)
{
$this->file = $file;
}
– In your handle function-
public function handle()
{
$localVideoPath = storage_path('app/public/').$this->file->filename;
$compressedPath = storage_path('app/public/').$this->file->compressedFileName;
if($this->file->extension == 'mp4'){
$string = "FFmpeg -i ".$localVideoPath." -vf 'scale=iw/4:ih/4 ' ".$compressedPath; //FFmpeg command for compression
echo shell_exec($string); //to run command on the terminal
}
else{
$stringChangeExtension = "FFmpeg -i ".$localVideoPath." -f mp4 -vf 'scale=iw/4:ih/4 ' -preset fast -profile:v main -acodec aac ".$compressedPath." -hide_banner";//FFmpeg command for compression and change extension of video
echo shell_exec($stringChangeExtension);//to run command on the terminal
}
$s3resp = Storage::disk('s3')->put($this->file->docPath, \Illuminate\Support\Facades\File::get($compressedPath));
$response = Storage::disk('public')->delete($this->file->filename);
$deleteFile = Storage::disk('public')->delete($this->file->compressedFileName);
$responseDir = Storage::disk('public')->deleteDirectory('uploads');
}
Result:
By using this example you can do video compression.