Here is my code to capture a frame and generate an image from a video ...
// get the duration and a random place within that
$cmd = "ffmpeg -i " . $videoPath . " 2>&1";
if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
$total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
$second = rand(1, ($total - 1));
}
exec($cmd);
// get the screenshot
exec("ffmpeg -i " . $videoPath . " -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg " . $imageOutput . " 2>&1");
$ The second variable is a random number from 0 to the total duration. and the second exec () is to create an image file from the selected frame.
$ imageOutput is the absolute location of the path to the generated image. for example: /home/ariawan/image-generated.jpg
source
share