Generate thumbnail using uploaded videos in PHP

Let Us look at how we can create thumbnails from videos we uploaded. Generating Thumbnails is important especially if we are working with media files on our system for managing because the thumbnail is useful to easily identify the video's content.

#php
Apr. 24, 2023. 5:57 AM
Ads

After searching the world wide web for how to generate thumbnail images using the video I uploaded. I came across this answer in stackoverflow: php - Generate preview image from Video file? - Stack Overflow

First, we have to install PHP-FFMpeg here: PHP-FFMpeg/PHP-FFMpeg. This package requires FFmpeg.

composer require php-ffmpeg/php-ffmpeg

Then you can use it like this:

<?php
require 'vendor/autoload.php';

$sec = 10;
$movie = 'test.mp4';
$thumbnail = 'thumbnail.png';

$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open($movie);
$frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($sec));
$frame->save($thumbnail);
echo '<img src="'.$thumbnail.'">';

Learning from the idea above we can use the class `FFMpeg`. using the open function we can pass the path of the video. then we can use frame to generate by passing fromSeconds static function from TimeCode class by passing a seconds arguments to get thumbnail based on video timeline. We can then use save function passing in the path, with the file name.


If you enjoy this article and would like to show your support, you can easily do so by buying me a coffee. Your contribution is greatly appreciated!

Jenuel Ganawed Buy me Coffee