How to execute commands through PHP?

I am trying to convert videos into MP4 using FFMPEG. I have it set up this way:

.
.
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;
}

And in the browser i get the following error: 'C:\xampp\htdocs\Thinksmart First Sprint' is not recognized as an internal or external command".

In my constructor i have it set up to give me the realpath and i suspect that this is what it does in the command line:

C:/xampp/htdocs/Thinksmart FIrst Sprint/ffmpeg/bin/ffmpeg.exe -i (file temp name) (file name i want)

And this should work, but i dont know why it wont. Any ideas? Its my first time working with video conversions.

Upvotes: 0

Views: 76

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78974

As you can see, spaces in your command are used to separate arguments. So if there are spaces in a path you need to quote the entire path with quotes so that the shell/processor knows they aren't separators but are one argument:

$cmd = $cmd = '"' . $this->ffmpegPath . '" -i $tempFilePath $finalFilePath 2>&1';

Which will result in a command something like this:

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

I think only double-quotes work on Windows. You need to quote $tempFilePath and $finalFilePath if they might have spaces in them as well.

Upvotes: 1

Related Questions