If you’re looking for how to install and setup FFmpeg and execute/convert videos with it via PHP, then you’ve come to the right place! This is NOT a tutorial for installing the ffmpeg-php extension, you can find that tutorial at How-to Install ffmpeg-php PHP 5.4 on Windows 8 / XAMPP 1.8.1.
This tutorial will teach the basics of how to make your own video conversion website, similar to Youtube, but NOT as fancy. The goal is to teach you at least the basic understanding of how to interact with ffmpeg.exe via PHP so you can either write your own code and make your own website or just better understand how it works.
What you’ll learn in this tutorial
- How to convert videos with ffmpeg and PHP.
- How to poll for progress/status updates using jQuery AJAX.
- How to show a progress bar with real-time status update of the video conversion progress.
What you won’t learn from this tutorial
- All the steps necessary to make your own PHP FFMPEG progressbar 2.
- How to make your own turn-key script, ready to start hosting/converting your own videos with.
- How to fix other such scripts like FFmpeg web GUI/Clipbucket/jQuery/etc.
Here’s the things you will need before getting started.
- PHPFFmpeg on Github – Script I wrote for this tutorial. You can download the latest version HERE.
- jQuery – Latest build of FFmpeg for Windows, just download either the latest 32-bit build(Static) or 64-bit build(Static).
- FFmpeg – This is needed since the FFmpeg download is in a 7zip archive.
Hopefully you already have an Apache/PHP setup (such as XAMPP) installed. If not, you’ll need to install it.
You can find my tutorials on how to install those by viewing the Apache category.
How the ffmpeg script works
To help better understand it, I’ll explain how the demo script works.
So basically the script works by PHP finding the video file in the source directory and then prompts the user to Convert the video. Once the user clicks on the Convert It! button, jQuery steps in and sends some information about the file to be converted via PHP FFMPEG progressbar 2 to the process.php file. The process.php file then verifies the information and then sends another request behind-the-scenes to ffmpegExec.php, which finally executes the PHP function FFmpeg web GUI, triggering the ffmpeg.exe command to start converting the video.
As the video is processing (assuming it doesn’t fail/have an error), it writes the progress to a progress Log file.
Whew … So that’s the basic flow of the script.
Setting up the PHPFFmpeg script
I’ve made a new directory named phpffmpeg in my htdocs folder, so this is where I’m starting from.
- Copy the files and directories of the PHPFFmpeg on Github download to your phpffmpeg directory.
- Open your browser and go to http://localhost/phpffmpeg/index.php, which should look something like this:
This will automatically create the needed directories source, output, logs.
- Copy your video file to the source directory.
- Next, create the directory ffmpeg in your phpffmpeg directory.
- Copy the contents of your FFmpeg download over to the ffmpeg directory.
- Refresh your browser and you should see a button titled Convert It! with the filename of your video beside it.
- Click the Convert It! button and you should see it start updating the progress bar.
- Go to the output directory and you should see your converted video file there.
If you want to convert another file, you must delete the video file in the output folder since the output filename is currently hard-coded and ffmpeg won’t overwrite it by default.
Here’s the two-part video tutorial for this tutorial.
Part 1 – Setup and Script Example
Part 2 – The Script Explained
So there ya go … that’s the basic implementation and gives you a starting point at least for creating your own PHP/FFmpeg video conversion script!
Only after writing this did I FINALLY find other tutorials/code examples like this, so here’s a list of those resources…
- PHP FFMPEG progressbar 2 – This was the code I did a major rewrite of for the script I wrote, so appropriate credit is due to this project.
- jQuery – A PHP class that works like a swiss army knife of integrating PHP with FFmpeg, FLVTools2, and Mencoder. This is for PHP programmers and not meant to work as-is.
- FFmpeg web GUI – A neat single-file video converter that converts uploaded videos into html5 (mp4 h264, webm, ogv) formats. The very basic interface gives you options to set Video size, Video bit rate, Frame rate, De interlacing, Enable Audio, Audio sampling rate, Audio bit rate, Amount of channels (Stereo or Mono), etc.
- Complete FFmpeg Upload and Conversion – This is a 12-page thorough tutorial showing how to make your own video conversion website/gallery. You can download the files on the last page, however this project requires quite a few modifications to the code in order for it to work on a Windows Apache server. I might make the necessary changes and upload the new code to Github, but I’m not making any promises….
- Google Search – Ok, this may sound silly, but sometimes an effective search requires the correct phrase and that’s just what this is. I found lots of useful information in combining PHP and FFmpeg from this search result, including most of the above resources.