Changes between Initial Version and Version 1 of PHP


Ignore:
Timestamp:
Sep 24, 2012, 10:44:20 PM (7 years ago)
Author:
burek
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PHP

    v1 v1  
     1= Introduction =
     2
     3This article will try to point out all the important things you need to know when using FFmpeg from within PHP web scripts. It will also show some usage examples to make things more clear. The idea can be applied to other web scripting languages too.
     4
     5= Invoking command-line tools from PHP scripts =
     6
     7== Choosing a model ==
     8
     9Web pages are designed to execute quickly, so that people, browsing your web site, don't have to wait too much for the response. Because if they get bored waiting, they'll just navigate off to another, more responsive (usually your competitor's) web site. That being said, the worst thing you could do is to run a command-line tool (like ffmpeg) from your web script and wait for it to finish the processing to be able to deliver the result back to the waiting online user (the exception to this are small/fast tools which execution time is too small to notice).
     10
     11What you want to do is to separate the command-line tool's long processing from your web script execution, to make your script more responsive. So, you have at least 2 options:
     121. Run the command-line tool (using [http://php.net/manual/en/function.shell-exec.php shell_exec()] for example) in the background and continue with the web script execution, periodically refreshing the status page, showing the current progress
     132. Create a new "job" (usually a new row in a table of your database) and have some background "worker" process (cron job, shell script, batch script) that will monitor that "job list" to see if there are any new jobs that need processing
     14
     15These two methods might seem like the same thing, but they are not. The most important difference is that the 2nd approach scales better with higher web site traffic, because it allows the "worker" process to be running on the completely separate machine. Also, there can be several "worker" machines, working together, splitting the workload, with a trivial synchronization involved.
     16
     17The first method is usually the first choice for most people that want to get the job done quickly, but during the time, when their website becomes popular, then their web server becomes less responsive, due to the constant cpu starvation, caused by multiple command-line tools (ffmpeg instances) running in the background. In that moment, they usually start considering the second method.
     18
     19== Running ffmpeg in the background ==
     20
     21''The following examples are intended for Linux-based operating systems. For Windows-based operating systems, continue reading the article, but after you read it, please also read [http://stackoverflow.com/questions/649634/how-do-i-run-a-bat-file-in-the-background-from-another-bat-file how to run a process in the background using START and CMD]. That will give you an idea how to apply the same idea to Windows-based operating systems.''
     22
     23Let's consider the natural way to run ffmpeg from PHP scripts:
     24{{{
     25<?php
     26        echo "Starting ffmpeg...\n\n";
     27        echo shell_exec("ffmpeg -i input.avi output.avi &");
     28        echo "Done.\n";
     29?>
     30}}}
     31
     32There are several issues that need to pointed out here. The first one is that, although we specified we want ffmpeg to be executed in the background (using the ampersand operator), PHP script will not continue it's execution until the command has finished its execution. This is due to the fact, mentioned in one of the notes for the [http://php.net/manual/en/function.exec.php PHP's exec() function] that says:
     33 '''''If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.'''''
     34
     35Don't be confused about the example showing [http://php.net/manual/en/function.shell-exec.php shell_exec()] call instead of [http://php.net/manual/en/function.exec.php exec()]. All of the [http://php.net/manual/en/ref.exec.php PHP's Program execution functions] share the similar code base and limitations.
     36
     37So, to work around this issue, wee need to do something like this:
     38{{{
     39<?php
     40        echo "Starting ffmpeg...\n\n";
     41        echo shell_exec("ffmpeg -i input.avi output.avi >/dev/null 2>/dev/null &");
     42        echo "Done.\n";
     43?>
     44}}}
     45The part that says "{{{>/dev/null}}}" will redirect the standard OUTPUT (stdout) of the ffmpeg instance to /dev/null (effectively ignoring the output) and "{{{2>/dev/null}}}" will redirect the standard ERROR (stderr) to /dev/null. These two can be combined into a shorter representation: "{{{>/dev/null 2>&1}}}". If you like, you can [http://tldp.org/LDP/abs/html/io-redirection.html read more about I/O Redirection].
     46
     47An important note should be mentioned here. The ffmpeg command-line tool uses stderr for output of error log messages and stdout is reserved for possible use of pipes (to redirect the output media stream generated from ffmpeg to some other command line tool). That being said, if you run your ffmpeg in the background, you'll most probably want to redirect the stderr to a log file, to be able to check it later.
     48
     49One more thing to take care about is the standard INPUT (stdin). Command-line ffmpeg tool is designed as an interactive utility that accepts user's input (usually from keyboard) and reports the error log on the user's current screen/terminal. When we run ffmpeg in the background, we want to tell ffmpeg that no input should be accepted (nor waited for) from the stdin. We can tell this to ffmpeg, using I/O redirection again "{{{</dev/null}}}", so that final example, to safely run ffmpeg command-line tool in the background would be similar to this:
     50{{{
     51<?php
     52        echo "Starting ffmpeg...\n\n";
     53        echo shell_exec("ffmpeg -y -i input.avi output.avi </dev/null >/dev/null 2>/var/log/ffmpeg.log &");
     54        echo "Done.\n";
     55?>
     56}}}
     57
     58The [http://ffmpeg.org/ffmpeg.html#Main-options "-y" option] is used to auto-overwrite the output file (output.avi) without asking for yes/no confirmation. If you need the opposite scenario, to auto-cancel the entire process if the output file already exists, then use "-n" option instead.
     59
     60= Conclusion =
     61
     62Running ffmpeg command-line tool in the background is easy when you know all these little things, mentioned above. Many people have decided to use a PHP's extension named [http://ffmpeg-php.sourceforge.net/ ffmpeg-php] that "adds an easy to use, object-oriented API for accessing and retrieving information from video and audio files" (quoted from their web site). The fact that this extension is not developed since 2007 (and requires "ffmpeg-0.4.9_pre1 or higher") tells that you are restricted to using a very old version of ffmpeg, without possibility to update to the latest version, since a lot of changes have been made inside ffmpeg's code, which make ffmpeg-php incompatible with latest ffmpeg.
     63
     64The thing is, most of the people don't really need this kind of API to accomplish usual tasks like generating a thumbnail, resize a video, convert it to another format, etc (also getting some info about your media, using FFmpeg's command-line tool named "ffprobe"). All of that can also be achieved using FFmpeg's command-line tools. Also, using ffmpeg-php implies that you need some kind of real-time processing of your audio/video media, which is an overkill for web scripts in terms of execution time and bored people, waiting for the web page to complete the loading.
     65
     66In the end, the choice is yours. If you really need to use ffmpeg-php, you'll be restricted to the old version of ffmpeg which doesn't have a lot of cool things and bug fixes implemented lately. Not to mention you will have a hard time getting the support for it. And if you decide to change your web application model a little bit and introduce all the changes, mentioned in this article, you'll be able to run the ffmpeg command-line tool from within your web scripts in no time, having all the benefits of using the latest (and greatest) version of ffmpeg.