Reputation: 732
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?"
Upvotes: 14
Views: 60547
Reputation: 31
hi i search for this issue and i can get version of ffmpeg by this code: echo(shell_exec('/usr/bin/ffmpeg -version'));
Upvotes: 1
Reputation: 154691
Try:
$ffmpeg = trim(shell_exec('which ffmpeg')); // or better yet:
$ffmpeg = trim(shell_exec('type -P ffmpeg'));
If it comes back empty ffmpeg is not available, otherwise it will hold the absolute path to the executable, which you may use in the actual ffmpeg call:
if (empty($ffmpeg))
{
die('ffmpeg not available');
}
shell_exec($ffmpeg . ' -i ...');
Upvotes: 12
Reputation: 5104
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.
Upvotes: 6
Reputation: 120
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.
Upvotes: -1
Reputation: 16675
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.
Upvotes: 3