Reduce the size of a video with PHP

Asked

Viewed 866 times

2

I’m creating a website where the user can post images and videos. The user can put up a video with up to 10MB initially, but I wanted to reduce this, on my site, to 1MB or less (any reduction for me is great, but the more better rsrs). I made a resizing system with the images to reduce their weight, but with video I do not know if it is 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 don’t know if it is possible in my current situation. I did some research, but I didn’t see anything that could help me with that.

  • 1

    Luizinho if you cannot install ffmpeg on your server there is an alternative which is an online service: https://answall.com/a/113600/3635 I hope it helps

  • Oops, thanks Guillerme! But I believe that to install ffmpeg on my server.

1 answer

5


There is nothing like this with native PHP, no site uses the language itself to do the process, what they use is own software for conversion installed on the server, the software that will probably solve your problem is the ffmpeg

You will have to install it on your server, if you have access via SSH it may be possible, after installed you can reduce the video size 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 as it keeps the video size ratio, in the first command reduces the video to 320 the width, in the second reduces to 640 the width, in both the height will be adjusted equivalent, then you can run it 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 best, so as the user device you deliver the video smaller or larger.


Compressing the video as much as possible

A suggestion from reply on Soen, that 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 https://github.com/PHP-FFMpeg/PHP-FFMpeg

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

composer require php-ffmpeg/php-ffmpeg

This library also requires the ffmpeg installed, ie it is only to facilitate the writing of the parameters, from my point of view, for your specific case it is a bit of 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, like the user choosing the type of video conversion and format, example of use:

$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 the user wants the Webm format, he would exchange the last line for:

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

Codecs

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

ffmpeg -codecs

If you cannot install ffmpeg on your website there is an alternative to a third-party service, as I replied on /a/113600/3635

read about the limitations of the free version:

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

Register for http://www.online-convert.com and then install the library via:

So 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.