views:

728

answers:

4
+1  Q: 

Detect FFMPEG?

I'm writing a PHP script that converts uploaded video files to FLV on the fly, but I only want it to run that part of the script if the user has FFMPEG installed on the server. Would there be a way to detect this ahead of time? Could I perhaps run an FFMPEG command and test whether it comes back "command not found?"

+2  A: 

You answered your own question, you can run the command and if it comes back negative you know it is not installed, or you can check the default paths the user has set for possible ffmpeg binaries.

X-Istence
Thanks. Could you demonstrate how I could evaluate the return of an exec() command in PHP? Is PHP smart enough to return false if the command doesn't work? That would surprise me.
Aaron
Sample: if (strpos(`ffmpeg --help`, 'ffmpeg') > -1) echo 'Installed!';
Cd-MaN
A: 

You could give this a try:

function commandExists($command) {
    $command = escapeshellarg($command);
    $exists = exec("man ".$command,$out);
    return sizeof($out);
}

if (commandExists("ffmpeg")>0) {
   // FFMPeg Exists on server
} else {
   // No FFMPeg
}

Reusable for other functions as well - not certain of security concerns.

StudioKraft
This is a very bad way to check for a command. Installed man pages don't mean that the program is installed - and vice-versa. It's enough to check for the executable itself.
viraptor
Fair enough, it was the first idea that popped to mind and worked on the server that I tested it on, thought I would see if it worked for the OP as well.
StudioKraft
+1  A: 

The third parameter to the exec() function is the return value of the executed program. Use it like this:

exec($cmd, $output, $returnvalue);
if ($returnvalue == 127) {
    # not available
}
else {
    #available
}

This works on my Ubuntu box.

Peter Stuifzand
A: 

"It's enough to check for the executable itself" any hints on how to do this?

Shaedo