PHP - Converting images to video using ffmpeg

2019-06-02 04:24发布

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.

标签: php video ffmpeg
1条回答
走好不送
2楼-- · 2019-06-02 04:41

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);
查看更多
登录 后发表回答