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
2009-05-06 18:44:47
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
2009-05-06 19:12:09
Sample: if (strpos(`ffmpeg --help`, 'ffmpeg') > -1) echo 'Installed!';
Cd-MaN
2009-05-27 06:20:55
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
2009-05-07 22:33:52
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
2009-05-16 13:38:33
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
2009-05-25 04:51:32
+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
2009-05-25 17:32:46
A:
"It's enough to check for the executable itself" any hints on how to do this?
Shaedo
2010-08-23 19:36:02