I recently implemented the thumbnail generation for Video using FFMPEG(https://github.com/PHP-FFMpeg/PHP-FFMpeg)
To generate the thumbnail from the video you need, duration of video so that you can get any from between this duration. I implemented the logic to get 3 frame durations from the video, I am showing 3 thumbnails by rotating them on hover of thumbnail like youtube. To get the dynamic frame durations from the video you can write below function which returns 3 frame durations for thumbnails.
Use below code to get duration from video:
$ffprobe = FFMpeg\FFProbe::create();
$duration = $ffprobe->streams($file) // extracts streams informations
->videos() // filters video streams
->first() // returns the first video stream
->get('duration');
Use below code to get 3 frame durations:
$thumbnailIntervalTimeArr = $this->getThumbnailIntervalTimeArr($duration);
private function getThumbnailIntervalTimeArr($duration){
$durationInSec = intval($duration);
$intervalArr = range(0,$duration, $duration/4);
return array_slice($intervalArr, 1, -1);
}
Now you have to generate the thumbnails for $thumbnailIntervalTimeArr, you can do it using below loop:
$counter = 1;
foreach ($thumbnailIntervalTimeArr as $interval){
$framePath = $this->contentBasePath."/frame_$counter.jpg";
$video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($interval))->save($framePath);
// use code listen below code is optional to generate the thumbnail image from frame
$counter++;
}
Above code will generate the 3 thumbnail frames which has same resolutions as the Video, best practice is to save the thumbnail by keeping its aspect ratio and with less size so that it can load faster, and to achieve that I used http://image.intervention.io/
$image = Image::make($imagePath)->encode('jpg', 50);
$image->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
});
$image->save("some path.jpg");