Welcome folks today in this blog post we will be compressing video bitrate
and resolution
in php using ffmpeg
library in browser. All the full source code of the application is shown below.
Get Started
In order to get started you need to make an index.html
file and copy paste the following code
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<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> |
As you can see we allow the user
to select the video file and then we have the select box to select the bitrate
of the video file to compress the size of the video. And then we have the button to change the bitrate
of the video. And then inside the form
we are making the post
request to the change-bitrate.php
script. Now we need to make this php script as shown below
change-bitrate.php
1 2 3 4 5 6 7 8 9 |
<?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"; |
As you can see inside the php
script we are getting the video
using the temporary
path and then we are getting the value of the bitrate
and then we are executing the ffmpeg
command to change the bitrate of the video.
Changing Resolution of Video
Now we need to make the index.html
file which will contain the form for changing the resolution of the video as shown below
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<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> |
Now we need to make the change-resolution.php
script where we will be changing the resolution
of the video
change-resolution.php
1 2 3 4 5 6 7 8 9 |
<?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"; |