如何通过PHP执行命令?

如何通过PHP执行命令?,php,ffmpeg,exec,Php,Ffmpeg,Exec,我正在尝试使用FFMPEG将视频转换为MP4。我是这样设置的: . . private $ffmpegPath; public function __construct($con) { $this->con = $con; $this->ffmpegPath = realpath("ffmpeg/bin/ffmpeg.exe"); } . . public function convertVideoToMp4($tempFilePath, $finalFilePat

我正在尝试使用FFMPEG将视频转换为MP4。我是这样设置的:

.
.
private $ffmpegPath;

public function __construct($con) {
    $this->con = $con;
    $this->ffmpegPath = realpath("ffmpeg/bin/ffmpeg.exe");
}
.
.
public function convertVideoToMp4($tempFilePath, $finalFilePath){
    $cmd = "$this->ffmpegPath -i $tempFilePath $finalFilePath 2>&1";

    $outputLog = array();
    exec($cmd, $outputLog, $returnCode);

    if($returnCode != 0){
        foreach ($outputLog as $line){
            echo $line."<br>";
            return false;
        }
    }

    return true;
}
在浏览器中,我得到以下错误: “C:\xampp\htdocs\Thinksmart First Sprint”未被识别为内部或外部命令

在我的构造函数中,我将其设置为提供realpath,我怀疑这就是它在命令行中所做的:

C:/xampp/htdocs/Thinksmart FIrst Sprint/ffmpeg/bin/ffmpeg.exe-i file temp name file name我想要的文件名


这应该行得通,但我不知道为什么不行。有什么想法吗?这是我第一次使用视频转换

如您所见,命令中的空格用于分隔参数。因此,如果路径中有空格,则需要用引号引用整个路径,以便shell/处理器知道它们不是分隔符,而是一个参数:

$cmd = $cmd = '"' . $this->ffmpegPath . '" -i $tempFilePath $finalFilePath 2>&1';
这将产生如下命令:

C:/xampp/htdocs/Thinksmart First Sprint/ffmpeg/bin/ffmpeg.exe-ic:/path/to/file1 C:/path/to/file2>&1


我认为只有双引号在Windows上有效。如果$tempFilePath和$finalFilePath中可能也有空格,则需要引用它们。

如果路径\目录中有空格,则需要引用it@tim据我所知,realpath将返回文件的根目录。例如,即使您的文件在桌面上,它也会显示C:/users/username/desktop/filename,因此在我的例子中,它已经显示了C:/xampp/htdocs/Thinksmart First Sprint/ffmpeg/bin/ffmpeg.exe,但梯形图3部分不会出现在字符串中,引用它:$cmd='.$this->ffmpegPath.-i$tempFilePath$finalFilePath 2>&1';我认为只有双引号在Windows上有效。如果$tempFilePath$finalFilePath中可能也有空格,则需要引用$tempFilePath$finalFilePath。然后它会说:“.$this->ffmpegPath”不被识别为内部或外部命令,我发誓我已经用这种语言完成了……我在它周围加了引号,并结合了and,它仍然会抛出一个错误。所以我不应该使用realpath?如果我部署这个webapp,那么我将进入服务器并为部署它的每台服务器配置文件的路径?如果我使用Linux服务器呢?我不是故意粗鲁,只是想问一下,尽管听起来很粗鲁。这实际上只是好奇。正如我所说的,我对整个视频转换有点陌生,而且我很少在app的root dir之外的全局范围内使用这些exec命令,我没有提到不使用realpath。答案中的$thi->ffmpegPath来自您的$this->ffmpegPath=realpathffmpeg/bin/ffmpeg.exe;