January 26, 2017 3:27 pm

Video Editing Tutorial With PHP and FFMPEG

In this short article, I will share about how to use PHP for video editing. We will create video converter by leveraging a free tool called FFMPEG. This tool is powerful for video editing and becoming the base of many video editor software out there. This script can do the following jobs:
– Convert video file to GIF image.
– Cut the length of given video.
– Convert given video to mp4 format.

If user do not specify anything other than the uploaded video, then it will automatically cut the video duration to 10 seconds from the start of the video and output it in the same format.

Testing Environments:
Here is a little information about the testing environments I used for writing this script:
OS: Windows 10 64bit
Local server: XAMPP version 7.0.13
Browser: Firefox Developer Edition version 52.0a2
FFMPEG Version: 20170123-e371f03 64bit for Windows

In order to handle larger file upload, I increased “post_max_size” and “upload_max_filesize” in php.ini file and then restart apache. So, maybe you also need to check yours to ensure that it will not block the upload process.

Before you start writing your FFMPEG-PHP script, make sure that you have downloaded FFMPEG binary file suitable for your OS here https://ffmpeg.org/download.html. Don’t forget to check the “More downloading options” to see more download choices and select the static build. You will only need the binary file containing ffmpeg on its name for this purpose. It is located inside the bin folder in your downloaded zip file.

Let’s now create some folders to organize our script.

First, create the main folder with a name “videoedit”, you can use any name you like. Next, let us put 3 other folders inside the folder we just created.
1. “binaries” – Put the downloaded FFMPEG binary files inside this directory.
2. “input” – This is the temporary folder to hold user’s upload before it is being processed with ffmpeg.
3. “output” – The result of video editing will be served and presented to the user from here.

Now, let’s move on to writing our PHP script.

1. Create an index.php file and put this html codes inside it.

The codes above is the base template for our video editor script. It contains a header and a form to handle user upload. You can add other format names as the additional options for the “select” field. You can refer here https://ffmpeg.org/general.html#File-Formats to see the supported format by ffmpeg.

2. Put the following PHP codes right after the closing form tag “

“.

Let’s break down some part of the codes above to see what is it doing.

First, we obtain the path to input and output folder using the following codes:

After user hit the “edit” button or submitting their query, we check whether they upload any file or not with this code:

If they are not uploading any file, we tell them that there are no file was uploaded.

Because we only want to accept video file, then we check whether the uploaded file is video or not by identifying its mime type. We then tell the user to upload a video and stop the process if the file isn’t containing video mime type.

If the mime check is passed, then we grab some information from the video and then define the necessary identities to handle it.

Next, we check what file extension user want for the output and let’s save it into a variable.

Now, let’s save user’s upload inside the “input” directory we created before.

After the video file has successfully being moved into “input” folder, we will then start to edit it with ffmpeg per user request and then provide them the access to its final result.

3. To make the interface a little tidier, let’s add some css rules. Create a css file with a name “style.css” and put the following codes inside:

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:
  • Anonymous

    can this can make GIF from a Youtube video online as well?

  • Max John

    Has your website been hacked ? When you look at the right side of your website we can see under

    Recent Posts

    Hacked By MuhmadEmad
    Hacked by White HAt Hacker

  • Jim Kasprowicz

    how can we use it on ubuntu can you please also post demo for ubuntu.

  • how can we use it on ubuntu can you please also post demo for ubuntu.