Aaron
Aaron

Reputation: 732

Detect FFmpeg installation and Version

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

Answers (5)

Ahmad Balavipour
Ahmad Balavipour

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

Alix Axel
Alix Axel

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

Peter Stuifzand
Peter Stuifzand

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

StudioKraft
StudioKraft

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

X-Istence
X-Istence

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

Related Questions