Reducing the size of a video with PHP

2

I'm creating a website where the user can post images and videos. The user could upload a video with up to 10MB initially, but I wanted to reduce it, on my site, to 1MB or less (any reduction for me is great, but the more the better). I've done a resizing system with the images to reduce its weight but with video I do not know if it's possible to do. I noticed that Youtube has a system that reduces the weight of the videos and wanted to do something similar, but I do not know if it is possible in my current situation. I researched, but I did not see anything that could help me in that regard.

    
asked by anonymous 11.06.2018 / 04:29

1 answer

4

There is nothing like this with native PHP, no website uses the language itself to make the process, which they use are software for conversion installed on the server, the software that will probably solve your problem is ffmpeg

You will have to install it on your server, if you have access via SSH it may be possible, once installed you can reduce the size of the video for each device, for example

ffmpeg -i input.mp4 -vf "scale=320:-1" mobile.mp4
ffmpeg -i input.mp4 -vf "scale=640:-1" desktop.mp4

The value passed to -vf , uses -1 because it keeps the ratio of the video size, in the first command it reduces the video to 320 the width, in the second it reduces to 640 the width, in both the height will be adjusted in an equivalent way, then you can do this using PHP like this:

function resize_video($width, $input, $output)
{
    $input = escapeshellarg($input);
    $output = escapeshellarg($output);

    exec('ffmpeg -i ' . $input . ' -vf "scale=' . $width . ':-1" ' . $output);
}

resize_video(320, 'caminho-do-video-que-fez-upload.mp4', 'assets/mobile.mp4');

resize_video(640, 'caminho-do-video-que-fez-upload.mp4', 'assets/desktop.mp4');

In the desktop examples I used 640 wide, but it's just an example, you can decide which size you think is best, so depending on the user device you deliver the video smaller or larger.

Compressing the video to the fullest

A suggestion of the SOen response, that the AP tested different parameters, was to use -preset veryslow -crf 28 , probably so for mobile:

ffmpeg -i input.mp4 -vf "scale=320:-1" -preset veryslow -crf 28 mobile.mp4

And so for Desktop:

ffmpeg -i input.mp4 -vf "scale=640:-1" -preset veryslow -crf 28 desktop.mp4

PHP-FFMpeg / PHP-FFMpeg Library

As quoted by Valdeir Psr, you can use the library link

To use it, it requires that your project use composer , then in the folder of your project, via terminal or cmd use the command:

composer require php-ffmpeg/php-ffmpeg

This library also requires ffmpeg installed, ie it is just to make it easier to write the parameters, from my point of view, for your specific case it is a bit of an exaggeration, since it will probably only execute a simple command and always the same, but if the goal is to use ffmpeg for various things, then PHP-ffmpeg might come in handy, type the user to choose the conversion type and format of the video, use example:

$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('video.mpg');
$video
    ->filters()
    ->resize(new FFMpeg\Coordinate\Dimension(320, 240))
    ->synchronize();
$video
    ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
    ->save('frame.jpg');
$video
    ->save(new FFMpeg\Format\Video\X264(), 'filme.mp4');

If you want the WebM format, you would change the last line to:

->save(new FFMpeg\Format\Video\WebM(), 'filme.webm');

CODECs

Note that some codecs need to be installed on the server also so that FFMPEG works for different formats, not all formats are supported by ffmpeg, to check the codecs supported on your server use the command:

ffmpeg -codecs

If you can not install ffmpeg on your site, there is an alternative to a third-party service, as I replied at link

  

read about the limitations of the free version:

     
  • Maximum 30 conversions per day (every 24 hours)
  •   
  • Maximum upload limit per file is 100 Megabytes
  •   

Register on link and then install the library via composer:

Then an example:

<?php
define('API_KEY', 'sua chave da API key deve vir aqui');

require 'vendor/autoload.php';

$config = new \OnlineConvert\Configuration();
$config->setApiKey('main', API_KEY);
$client = new \OnlineConvert\Client\OnlineConvertClient($config, 'main');
$syncApi = new \OnlineConvert\Api($client);
    
11.06.2018 / 09:26