Tag Archives: FFmpeg

An FFMPEG Question – Why PHP’s exec() Doesn’t Return the Command Output

string exec (…)

Here’s the tricky part. As we know from the exec() man page this function executes the given command. As described there, the exact declaration of this method is:

string exec  ( string $command  [, array &$output  [, int &$return_var  ]] )

So far so good. Normally exec() will return the output of the command.

FFmpeg version SVN-r21880, Copyright (c) 2000-2010 Fabrice Bellard, et al.
  built on Feb 18 2010 15:53:28 with gcc 4.3.2
  configuration: --enable-gpl --enable-nonfree --enable-shared --enable-postproc --enable-avfilter --enable-avfilter-lavf --enable-pthreads --enable-x11grab --enable-bzlib --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-version3 --enable-libdc1394 --enable-libfaac --enable-libfaad --enable-libfaadbin --enable-libgsm --enable-libmp3lame --enable-libschroedinger --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-zlib --prefix=/usr/local
  libavutil     50. 9. 0 / 50. 9. 0
  libavcodec    52.54. 0 / 52.54. 0
  libavformat   52.52. 0 / 52.52. 0
  libavdevice   52. 2. 0 / 52. 2. 0
  libavfilter    1.17. 0 /  1.17. 0
  libswscale     0.10. 0 /  0.10. 0
  libpostproc   51. 2. 0 / 51. 2. 0
...

However sometimes that is not so true. Let’s see an example with the popular FFMPEG program. In one hand when you try to start a video converting program with PHP you can simply call exec with the FFMPEG command as the first parameter. The output is OK and you can dump it as in the example here.

echo exec('/usr/local/bin/ffmpeg -i input.mp4 -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre normal -threads 0 output.mp4', $output);	
var_dump($output);

In some cases you can call FFMPEG only with the -i option to get some info about the input file. Such info can be the size of the image, the bitrate of the source file or the length of the movie. Executing this command:

echo exec('/usr/local/bin/ffmpeg -i input.mp4', $output);	
var_dump($output);

in the terminal you’ll see the output you wish. However if you trying to execute the command via exec() the output remains empty?

Why the Output Parameter is Empty?

Looking at the terminal screen you can’t see the difference, but actually in the second case, when you execute FFMPEG only with the -i option, the output is redirected to the standard error output. That’s why you can’t see it in the exec() second parameter. However there is a solution.

Sometimes PHP's exec() doesn't return the command output. You've to be sure that the command output is not redirected to the standard error stream.
Sometimes PHP's exec() doesn't return the command output. You've to be sure that the command output is not redirected to the standard error stream.

Solution and Conclusion

The solution is quite simple. You’ve to redirect the output of the FFMPEG command to the standard output.

/usr/local/bin/ffmpeg -i /var/www/android/test.mp4 2>&1

Now you can call this line with exec().

echo exec('/usr/local/bin/ffmpeg -i input.mp4 2>&1', $output);	
var_dump($output);

This showcase with the FFMPEG is important not only because now you can call FFMPEG’s command without problems, but because you now know that exec() can’t read the standard error output. Thus you’ve to be careful and always redirect to the standard output.

Few Thoughts on Web Video

On Air


It’s really full of video sharing websites out there, but in fact almost all of them use Flash player to display their video files. This is the reality now, but with the coming of HTML5, perhaps the things are changing a bit!

First of all if you start dealing with video sharing platforms the first thing to do maybe is to find a good Flash (.flv) player and to convert all your video content in FLV.

Few things to know with Flash video players:

  1. The user can play those videos only after installing the Flash Plugin to his browser;
  2. The video must be encoded either in FLV (or FLash Video) or in MPEG-4 with h.264 codec. Only than the Flash player can play it;

The HTML5, which can be described as variety of new “things” in the HTML comes with a native <video> tag. Something like the <img> tag where you can just point the source of the image in the src attribute. Continue reading Few Thoughts on Web Video

How to Make MP4 Progressive with qt-faststart

A big part of the developers worldwide, who have dealt with video converting, sooner or later have been used FFMPEG. In fact I’d say that two of the main software on the video converting scene are FFMPEG and Mencoder. I began to use Mencoder back in 2005 and I used it as a DVD ripping tool, but my experience with it ends there, while now I use FFMPEG.

However FFMPEG is a converting program and many things depend on what kind of video files come as an input and what do you want to achieve as an output. Here comes the codec question, where various libraries can help. In my case, as I wrote few posts ago, I use MP4 as output encoded with h.264, both encoded with ffmpeg/libx264. So far so good, but there is one main problem that anyone used this pair knows.

Flash Player and MP4

As far as I know Flash Player 9 and beyond plays video files encoded with MP4/h.264 except the typical FLV (flash video files). This can help video sites not only to share their video content via web – within browser based flash players, but also to show them via some mobile device interface where MP4/h.264 is perhaps the only one reasonable solution. In this case the file is accessible in both medias.

By default Flash Player can play such FFMPEG encoded .mp4 files, but fist the whole file must be downloaded by the browser and only than the playback can begin. That’s because the FFMPEG puts meta info at the end of the video file, while the flash player needs it at the beginning and only than to begin playing while the file is still downloading.

qt-faststart

As the name of this software says this program helps you move the important meta info from the end to the beginning of the file. This helps the video to playback as early as possible. In an ideal circumstances everything works just fine, but there is a serious problem in fact.

qt-faststart and Server Crash

qt-faststart is responsible to move the meta info where it must be – at the beginning of the video file, but cannot finish execution when the video file is broken or the meta info doesn’t exists. Actually this is not a problem of qt-faststart, but to the video file. However this was a critical problem in my case, because the video files remains stuck for hours of execution and 99% of CPU usage.

Thankfully there is a solution and it can be found in the qt-faststart wrapper – qtfaststart.py

qtfaststart.py

written on Python this script overcomes the disadvantages of qt-faststart. As described by his author Daniel G. Taylor:

  1. Works everywhere Python can be installed
  2. Handles both 32-bit (stco) and 64-bit (co64) atoms
  3. Handles any file where the mdat atom is before the moov atom
  4. Preserves the order of other atoms
  5. Can replace the original file (if given no output file)

With its help not only qt-faststart doesn’t crash, but also the video files remain progressive as desired. The installation process is as difficult as copy/paste on the server where Python must be installed.

ffmpeg, libx264 and presets

When working with FFMPEG you can convert/encode in various formats, but in my case this should be an MP4 file, encoded with libx264. This combination is quite well known in the web community, because those videos are playable under almost any flash player and Apple products.

Now the problem is that this comes with very large list of options, which are difficult to setup, especially for newbies like me. Than everything’s difficult, once because the resulting quality is not always the same as the input file’s quality.

The Solution

Is to use presets. Thankfully ffmpeg with libx264 can be used with presets, i.e. NORMAL, HD etc. which means that you don’t have to setup a command by hand and the resulting quality is the same as the input quality.

Here’s a sample command using normal preset:

ffmpeg -i source.mp4 -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre normal -threads 0 -crf 22 output.mp4

here the source can be either mp4 or some other file format as FLV for instance.