Video Compression Using Laravel

|
| By Webner

Introduction to Video Compression

Video Compression is a term used to define a method to reduce the data used to encode digital video content. This reduction in data further translates to various benefits such as smaller storage requirements and lower transmission bandwidth requirements, for a clip of the video content.

Note:- The below-given method is most suitable for compressing small-sized files

  1. Add a package in composer as given below-

    composer require pbmedia/laravel-ffmpeg
    
  2. Also, make sure you have FFmpeg binaries installed on your machine. If you’re running Linux, you can easily install it by running the following apt install command-

    sudo apt-get install ffmpeg
    
  3. In your app.php use these

    In providers

    Pbmedia\LaravelFFMpeg\FFMpegServiceProvider::class
    

    In aliases

    'FFMpeg' => Pbmedia\LaravelFFMpeg\FFMpegFacade::class
    
  4. If you want to compress files with the size around 100 MB then you can use compression in your Controller like:

    Use the following on top of the controller-

    use FFMpeg;
    use FFMpeg\Coordinate\Dimension;
    use FFMpeg\Format\Video\X264;
    

    In your controller method:

    $file = $request->file('File');                                       //get file from request 
    $arrayFileName = explode(".", $file->getClientOriginalName());         
                to get name of file in array form                                                                     
    $filename =  $file->getClientOriginalName();            //to get existing name  of file
    $storage_path_full = '/'.$filename;                            //to make path
     $localVideo =  Storage::disk('public')->put($storage_path_full, 
    file_get_contents($file));      
    				//to save the file in your public folder
     $s3FilePath = 'userMedia/' ' . $arrayFileName[0] . date('his') . '.' . $extension;
    			             //to make s3 file path
    $lowBitrateFormat = (new X264('libmp3lame', 'libx264'))->setKiloBitrate(500);
    		  FFMpeg::fromDisk('public')
    			 ->open($filename)
                                      ->addFilter(function ($filters) {
    	                     $filters->resize(new Dimension(960, 540));
    		            })
    		         ->export()
    		        ->toDisk('s3')
    		         ->inFormat($lowBitrateFormat)
    		         ->save($docPath);
    

    NOTE: This library can be used only for mp4 and AVI videos

  5. If you are dealing with large-volume files than you should prefer compressing them using Laravel job.

    First, you have to create a job class

    php artisan make:job compressVideo
    

    When you run this command then in your App\Jobs folder compressVideo.php is automatically created

    Now go to your controller and pass an array

    $filename      =  $file->getClientOriginalName();    //to get existing name of file
    $extension    =  $file->extension();                         //to get extension of existing file
    $user_id        =   Auth::user()->id();
    $arrayFileName = explode(".", $file->getClientOriginalName());         
                to get the name of a file in array form              
               $s3FilePath = 'userMedia/' ' . $arrayFileName[0] . date('his') . '.' . $extension;
    			             //to make s3 file path
               $compressedFileName = $arrayFileName[0] . date('his') . '.' . $extension;
               $fileArray =[
    'filename'=>$filename,
    'extension'=>$extension,			
     'user_id'=>$user_id,
     'docPath'=>$docPath,
     'name'=>Auth::user()->name,
     'email'=>Auth::user()->email,
      'compressedFileName'=>$compressedFileName,
     'localVideo'=>$localVideo
    ];
     $fileObject =  (object)$fileArray;
    $response = compressVideo::dispatch($fileObject);
    
  6. Go to your job and at the top use these-

    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-

     Log::info("data --->   ".print_r($this->file,1));
     $storage_path_full = '/'. $this->file->filename;
       $localVideo =  Storage::disk('public')->put($storage_path_full, $this->file->localVideo);
       $lowBitrateFormat = (new X264('libmp3lame', 'libx264'))->setKiloBitrate(500);
     FFMpeg::fromDisk('public')
      ->open($this->file->filename)
     ->addFilter(function ($filters) {
     $filters->resize(new Dimension(960, 540));	
      })
        ->export()
    ->toDisk('s3')
    ->inFormat($lowBitrateFormat)
    ->save($this->file->docPath);
    

    Now you can try uploading the video files through your application and check the output on s3 storage. The code is capable of reducing the size of a file from one-third to one-fourth of its original size.

Leave a Reply

Your email address will not be published. Required fields are marked *