• How To Run / Execute FFmpeg with PHP on Windows PC

    by  • December 19, 2012 • Tutorials • 10 Comments

    FFmpeg PHP logo

    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

    Prerequisites

    Here’s the things you will need before getting started.

    Downloads

    • 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.

    FFmpeg is a standalone program that’s used via the Command-line Interface.
    PHP FFMPEG progressbar 2 is a web server program that integrates with Apache and can make Command-line Interface calls.

    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.

    Now … back on the user-end, the jQuery javascript makes polling AJAX requests to process.php for the current number of seconds the script has processed.  process.php uses the class ffmpegConvert (in the functions.php file) to parse the progress Log file and returns the current encoded seconds and total seconds, which the AJAX script takes the values and updates the progress until 100% is reached.

    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.

    1. Copy the files and directories of the PHPFFmpeg on Github download to your phpffmpeg directory.
      How To Run - Execute FFmpeg with PHP on Windows PC - phpffmpeg files
    2. Open your browser and go to http://localhost/phpffmpeg/index.php, which should look something like this:
      How To Run - Execute FFmpeg with PHP on Windows PC - phpffmpeg index
      This will automatically create the needed directories sourceoutputlogs.
    3. Copy your video file to the source directory.
      How To Run - Execute FFmpeg with PHP on Windows PC - phpffmpeg source video file
    4. Next, create the directory ffmpeg in your phpffmpeg directory.
    5. Copy the contents of your FFmpeg download over to the ffmpeg directory.
      How To Run - Execute FFmpeg with PHP on Windows PC ffmpeg files
    6. Refresh your browser and you should see a button titled Convert It! with the filename of your video beside it.
      How To Run - Execute FFmpeg with PHP on Windows PC - phpffmpeg convert it
    7. Click the Convert It! button and you should see it start updating the progress bar.
      How To Run - Execute FFmpeg with PHP on Windows PC - phpffmpeg progressing video
    8. 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.

    Video Tutorials

    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!

    Additional Resources

    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 sizeVideo bit rateFrame rateDe interlacingEnable AudioAudio sampling rateAudio bit rateAmount 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.

     

    About

    I'm a freelance PHP developer spending much of my time deep in WordPress theme and plugin development. My personal info can be found at http://amereservant.com .

    http://myownhomeserver.com

    10 Responses to How To Run / Execute FFmpeg with PHP on Windows PC

    1. Rishabh
      June 27, 2016 at 8:13 AM

      how to combine two video ????

    2. November 25, 2015 at 11:29 AM

      FFMPEG PASSWORD? What the heck is that?

    3. Larry
      October 17, 2015 at 5:21 PM

      I am getting “Request Failed” running on EasyPHP.

      Is an ffmpeg password needed? If so then how is it obtained?

      Does ffmpeg-php extension have to be installed?

      The submitted ffmpeg paramers were:

      -acodec libvo_aacenc -ac 2 -ab 128 -ar 22050 -s 1024×768 -vcodec libx264 -fpre “C:\Program Files (x86)\EasyPHP\data\localweb\phpffmpeg-sample\ffmpeg\presets\libvpx-360p.ffpreset” -b 1200k -f mp4 -threads 0

    4. Martin
      August 17, 2015 at 10:43 AM

      Excellent, easy to follow and works first time. Thank you.

    5. Huy Hoang-Nguyen
      December 11, 2013 at 3:17 PM

      For people with “Request Failed” above. If you read logs file, you need to update ffmpeg password in Config.php. Good luck.

    6. November 6, 2013 at 7:29 AM

      Doesn’t work!

      ffmpeg-progress: FFMPEG progress log does not exist! FILE: C:\xampp\htdocs\phpffmpeg\logs\58d7bd65.ffmpeg.log

    7. Darlei
      September 6, 2013 at 2:26 PM

      it is giving me request failed JS error??any help mate……………………. (2)

    8. June 7, 2013 at 2:56 AM

      it is giving me request failed JS error??any help mate…………………….

    Leave a Reply