Tag Archives: unix

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.

Diving into Node.js – Very First App

What do I have till now?

After Node.js is istalled, described in my previous post, I can simply run this command:

stoimenpopov:~# node server.js

and this will start the server with the code within server.js. But what’s the code of server.js?

Following the instructions of Node’s homepage and most of the tutorials I’ve found, I can simply copy/paste the code from the first lines of Node’s page:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');

There are several things I find interesting in this code, making it different from JavaScript as we know it. First of all what is

var http = require(‘http’)

and why I need it? What is the purpouse of 8124 and 127.0.0.1?

Node is built in modules and to use one of them you must first include it with require. Just like the example above with require(‘http’). In the same manner you can include every module of Node.

What are the Node’s modules are pretty well described in the API page. Well I’d like to say that the API page is quite insufficient. That is very bad, cause most of the code you’ll need developing a node.js applicatoin isn’t described/explained there. Continue reading Diving into Node.js – Very First App

Connect MySQL from Zend Server CE trough unix socket!

First of all, let me introduce you to my case. I’m working on a Mac with both Xampp and Zend Server CE. Both have separate MySQL servers installed from their default installation on my machine. Both have their separate phpMyAdmins.

The one thing I couldn’t find in the man page of Zend Server CE is how to I access this MySQL server installation from my PHP scripts.

The answer is quite simple – trough UNIX socket. The ZS CE default db socket is:

/usr/local/zend/mysql/tmp/mysql.sock

and yet again let me say that this is for Mac.