ffmpeg does not convert images to video with PHP

ffmpeg does not convert images to video with PHP

There can be numerous reasons for this, ranging from incorrect file permissions to incorrect path specifications to corrupt image files.

To at least check the image data for integrity the package “feh” can be used:

feh -ur /var/images

ffmpeg is executed via PHP using shell_exec, so in the next step I tried the output

Command > /path/outputfile

to a log file, but unfortunately this did not work. The corresponding log file was completely empty, despite having the appropriate file permissions.

Accordingly, I had to execute the command requested by PHP manually in the shell. In doing so, I received the following error message:

[libx264 @ 0x5ec6d00] height not divisible by 2 (1200x675)
Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height

In my case the problem was a height of 675 pixels which is not divisible by 2. This problem can be solved by extending the command with

-vf "pad=ceil(iw/2)*2:ceil(ih/2)*2"

to solve this problem.

Thus, the complete command looked like this:

-vf "pad=ceil(iw/2)*2:ceil(ih/2)*2"

/var/bin/ffmpeg -framerate 24 -i /var/CAM/video/image%010d.jpg -c:v libx264 -crf 30 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" /var/CAM/video/test.mp4

The files in this example correspond to the filename “image0000001.jpg”

Since PHP didn’t want to pass the extra part properly, I had to write the new part into a separate variable first:

$command1 = '-c:v libx264 -crf 30 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2"';
$command2 = "$ffmpeg_path -framerate $frames $image $command1 $output_name";

Leave a Reply

Your email address will not be published. Required fields are marked *