PHP - Converting images to video using ffmpeg

2019-06-02 04:42发布

问题:

I am good in php but very new to ffmpeg and exec stuffs.Now I have successfully installed ffmpeg via ssh.I referred some stackoverflow here and found below code to make video using several images.

<?php

echo exec('ffmpeg -f image2 -i image%d.jpg video.mpg');

?>

I have image1.jpg and image2.jpg in same folder.I run this php code but nothing happened...where does video.mpg gets saved ? and how to check if exec function ran successfully or how to debug it ?Any help is appreciated.

回答1:

If it successfully worked, video.mpg got saved in your current working directory. If needed you can change filenames to absolute paths.

To check if exec() function ran successfully you can pass the third argument to get the exit status:

exec('ffmpeg -f image2 -i image%d.jpg video.mpg', $output, $exit_status);

If successful, $exit_status will got 0 value, so:

if ($exit_status === 0) {
    // ran fine
} else {
    // failed...
}

If it's failling you might want to get errors on $output. You can move STDERR to STDOUT ignoring original STDOUT this way:

exec('ffmpeg -f image2 -i image%d.jpg video.mpg 2>&1 >/dev/null', $output, $exit_status);

Then you could dump $output to see what you got on STDERR:

if ($exit_status !== 0) {
    print implode("\n", $output);
}

If the video.mpg file already exists the command will get stuck, you might want to pass -y flag to ffmpeg to overwrite any existing file:

exec('ffmpeg -y -f image2 -i image%d.jpg video.mpg 2>&1 >/dev/null', $output, $exit_status);


标签: php video ffmpeg