Compress video in bitrate and resolution – PHP & FFmpeg

0
(0)

When it comes in compressing the video, there are many options which can be adopted. The most basic and effective one is to decrease the bitrate of video. Changing bitrate will result in decreasing the quality of video, thus will reduce the size of video. Decreasing the quality of video will help you in easily transfer the file over the internet. Lower size video will consume less bandwidth and transfer faster than large size videos. The highest quality video having highest bitrate value is 4K as of writing this today. 4K display also known as the retina display because it gives the clear picture of video as you see with naked eye. Not 3D of course, but talking in terms of quality. Similarly, the least quality video having lowest bitrate value is 144P and it looks good on small screens only. In fact, there are very few devices with such small screen are left today. So in the near future, this quality most probably be removed at all from the quality format table. Most of the movies you see in Blu-ray are usually in 720P or in 1080P, these are also known as high definition videos HD. As of today, we also have 2K and 4K display but you do not really wanted to compress the video to 2K. Usually you want high quality video to compress to lower quality.

BitrateFormat
350k240p
700k360p
1200k480p
2500k720p
5000k1080p

Another option to compress the video is to change it’s resolution. Resolution in videos is defined by their width and height (w x h), also known as dimension of video. The 4K display usually have the resolution of 2560 x 1600 which you can get in Macbook Pro 2017 and onwards. Decreasing the resolution of video is useful especially when you wanted to view the video in small screens. For example, you have a video in 2560 x 1600 resolution and you wanted to see this in mobile phone. Playing such high resolution video in mobile devices sometimes makes the device run slow, since not all mobile phones have such high processing speeds. Moreover, playing the video in 1200 x 720 resolution is enough to view it in high quality in small screens. There are some phones which can play 1920 x 1080 video seamlessly.

YouTube (Example)

When it comes to uploading the video on YouTube, it is recommended to upload the video to at-least 720P which means the video dimension must be at-least 1200 x 720. If you have a video of 720P then YouTube will automatically gives users an option to view the video in lower formats and it also gives your video an HD tag which will be seen on bottom right corner of video thumbnail. So if you have any device that can record 4K video, Macbook Pro or Panasonic or DSLR, you can create the video in 4K but later can reduce the resolution to 1080 or 720P without having the fear to lose the quality of video.

Compressing the bitrate

<link rel="stylesheet" type="text/css" href="bootstrap-darkly.min.css">

<div class="container" style="margin-top: 200px;">
    <div class="row">
        <div class="col-md-4 offset-md-4">
            <h1>Change bitrate</h1>

            <form method="POST" enctype="multipart/form-data" action="change-bitrate.php">
                <div class="form-group">
                    <label>Select video</label>
                    <input type="file" name="video" class="form-control" required="" accept="video/*">
                </div>

                <div class="form-group">
                    <label>Select bitrate</label>
                    <select name="bitrate" class="form-control">
                        <option value="350k">240p</option>
                        <option value="700k">360p</option>
                        <option value="1200k">480p</option>
                        <option value="2500k">720p</option>
                        <option value="5000k">1080p</option>
                    </select>
                </div>

                <input type="submit" name="change_bitrate" class="btn btn-info" value="Change bitrate">
            </form>
        </div>
    </div>
</div>

Now we need to create a page that will process this request. So create a new file named “change-bitrate.php” and paste the following code in it:

<?php

$video = $_FILES["video"]["tmp_name"];
$bitrate = $_POST["bitrate"];

$command = "/usr/local/bin/ffmpeg -i $video -b:v $bitrate -bufsize $bitrate output.mp4";
system($command);

echo "File has been converted";

Compressing the resolution

<div class="container" style="margin-top: 50px; margin-bottom: 100px;">
	<div class="row">
		<div class="col-md-4 offset-md-4">
			<h1>Change resolution</h1>

			<form method="POST" enctype="multipart/form-data" action="change-resolution.php">
				<div class="form-group">
					<label>Select video</label>
					<input type="file" name="video" class="form-control" required="">
				</div>

				<div class="form-group">
					<label>Select resolution</label>
					<input type="text" name="resolution" class="form-control" placeholder="640x480">
				</div>

				<input type="submit" name="change_resolution" class="btn btn-info" value="Change resolution">
			</form>
		</div>
	</div>
</div>

change-resolution.php

<?php

$video = $_FILES["video"]["tmp_name"];
$resolution = $_POST["resolution"];

$command = "/usr/local/bin/ffmpeg -i $video -s $resolution output2.mp4";
system($command);

echo "File has been converted";

Now you can run the script in your browser and you will be able to change the resolution of selected video file. The original file won’t get affected, there will always be a new file created separately with compressed bitrate or resolution. When you reduce the resolution of video, then it may have a slight effect on video size. For example, 1920 x 1080 video will be compressed to 1280 x 720 but the size of video may have been reduced to just 2 or 3 MB. This is because changing the resolution does not reduct the quality of video, it just convert the high resolution video into low resolution. This is useful when you wanted to see the videos in small screen or when you wanted to share your video on social network.

That’s all for now. I have also attached all the source files below, make sure you download them and let us know if you face any issue.

Kindly login or create a new account to download the files.

How useful was this post

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post





10 Replies to “Compress video in bitrate and resolution – PHP & FFmpeg”

  1. I really like your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it for you?
    Plz answer back as I’m looking to design my own blog and would like to know where u got this from.
    thanks a lot

  2. I do not even know how I ended up here, but I thought this post was great.

    I do not know who you are but definitely you’re going to a famous blogger if you are not already ;
    ) Cheers!

  3. thank you for you Article sir
    but i have a question please
    this script doesn’t accept files larger than 40 MB.
    how can i make it accept more than this file size?

    thank you

    1. You can do this via PHP in your code:
      ini_set(‘post_max_size’, ‘512M’);
      ini_set(‘upload_max_filesize’, ‘512M’);

      Or via php.ini file:
      upload_max_filesize = 512M
      post_max_size = 512M

      Set any value you want.

        1. It will be saved in the root directory. If there is any error in converting the video, you can append 2> at the end of command to see the error logs.

Comments are closed.

Please disable your adblocker or whitelist this site!