php 检测 FFmpeg 安装和版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/831081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:00:28  来源:igfitidea点击:

Detect FFmpeg installation and Version

phpffmpegcommandversion

提问by Aaron

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.

我正在编写一个 PHP 脚本,可以将上传的视频文件动态转换为 FLV,但如果用户在服务器上安装了 FFmpeg,我只希望它运行脚本的那部分。

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?"

有没有办法提前检测到这一点?我可以运行一个 FFmpeg 命令并测试它是否返回“找不到命令”?

回答by Alix Axel

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:

如果返回空的 ffmpeg 不可用,否则它将保存可执行文件的绝对路径,您可以在实际的 ffmpeg 调用中使用它:

if (empty($ffmpeg))
{
    die('ffmpeg not available');
}

shell_exec($ffmpeg . ' -i ...');

回答by Peter Stuifzand

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

exec()函数的第三个参数是执行程序的返回值。像这样使用它:

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

This works on my Ubuntu box.

这适用于我的 Ubuntu 盒子。

回答by X-Istence

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.

您回答了自己的问题,您可以运行该命令,如果返回否定结果,您就知道它没有安装,或者您可以检查用户为可能的 ffmpeg 二进制文件设置的默认路径。

回答by StudioKraft

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.

也可重用于其他功能 - 不确定安全问题。

回答by Ahmad Balavipour

hi i search for this issue and i can get version of ffmpeg by this code: echo(shell_exec('/usr/bin/ffmpeg -version'));

嗨,我搜索这个问题,我可以通过以下代码获取 ffmpeg 的版本: echo(shell_exec('/usr/bin/ffmpeg -version'));