Changes between Version 3 and Version 4 of PHP


Ignore:
Timestamp:
Nov 1, 2012, 2:57:33 PM (7 years ago)
Author:
burek
Comment:

minor typo changes

Legend:

Unmodified
Added
Removed
Modified
  • PHP

    v3 v4  
    99== Choosing a model ==
    1010
    11 Web 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).
     11Web 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 whose execution times are too small to notice).
    1212
    1313What 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:
     
    2121== Running ffmpeg in the background ==
    2222
    23 ''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.''
     23''The following examples are intended for Linux-based operating systems. For Windows-based operating systems, please continue reading this article, but after you read it, 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 logic to Windows-based operating systems.''
    2424
    2525Let's consider the natural way to run ffmpeg from PHP scripts:
     
    3232}}}
    3333
    34 There 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:
     34There are several issues that need to be 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 ffmpeg 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:
    3535 '''''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.'''''
    3636
    3737Don'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.
    3838
    39 So, to work around this issue, wee need to do something like this:
     39So, to work around this issue, we need to do something like this:
    4040{{{
    4141<?php
     
    4545?>
    4646}}}
    47 The 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].
     47The 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 (effectively ignoring any error log messages). 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].
    4848
    4949An 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.
     
    6262= Conclusion =
    6363
    64 Running 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.
     64Running 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") means that you are restricted to use a very old version of ffmpeg, without possibility to update it to the latest version. Since a lot of changes/improvements are being made, inside ffmpeg's code, every day, it makes ffmpeg-php incompatible with the latest ffmpeg.
    6565
    66 The 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. All of that can also be achieved using FFmpeg's command-line tools (ffmpeg and ffprobe mostly). 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.
     66The thing is, most of the people don't really need this kind of API to accomplish usual tasks like generating a thumbnail, resizing a video, converting videos to another formats, etc. All of that can also be achieved using FFmpeg's command-line tools (ffmpeg and ffprobe mostly). 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.
    6767
    68 In 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.
     68In 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 any version of ffmpeg command-line tool, from within your web scripts, with minor modifications of command-line options, depending on which version of ffmpeg you are using. Of course, using the latest (and greatest) version of ffmpeg provides you with all the benefits of new features and bug fixes.