Generate thumbnails of video – PHP & FFmpeg
In this tutorial, we will be creating a script in PHP and FFmpeg. We will provide a video to our script. And our script will generate thumbnails of that video after regular intervals.
Thumbnails of video
Thumbnail is just a preview of a video so user can see what’s inside the video before playing it. You can show single image as a thumbnail, or you can show a GIF image which is a series of multiple frames. The video which has a GIF thumbnail allows you to hover your mouse over the video to play the GIF. The first frame will be seen as a static image. You can generate a list of thumbnails from any video or you can create just one.
Get single thumbnail
If you wanted to get just 1 image from video (also known as screen capture) you need to tell the time duration from where to capture the screen. For example, you need to tell the time in this format (hh:mm:ss). But if you wanted to generate multiple images of video, like YouTube and Facebook does, you need to tell the frame interval. You can tell to capture image every 60 seconds, for example. When you upload any video on YouTube, by default it generates 3 images from the video. Similarly Facebook generates 10 images when you upload the video.
What we are going to do in this post ?
In this tutorial, we will be creating a similar script that will generate thumbnails from video and save them as separate files. In order to generate thumbnails from video, you must have FFmpeg installed in your system. You can download it from it’s official site. We are creating a form that will allow you to select video whose thumbnail needs to be generated.
Creating layout
<link rel="stylesheet" href="bootstrap-darkly.min.css">
<div class="container" style="margin-top: 200px;">
<div class="offset-md-4 col-md-4">
<form method="POST" enctype="multipart/form-data" action="generate-thumbnail.php">
<div class="form-group">
<label>Select video</label>
<input type="file" name="video" accept="video/*" class="form-control" required>
</div>
<input type="submit" class="btn btn-primary" value="Generate">
</form>
</div>
</div>
Generating thumbnails of video
At this point if you run the script, you will see a form with 1 input field which allows you to select the video whose thumbnails needs to be generated and a submit button. Now you need to create a new file named “generate-thumbnail.php” and paste the following code in it:
<?php
$video = $_FILES["video"]["name"];
$command = "/usr/local/bin/ffmpeg -i " . $video . " -vf fps=1/60 thumbnail-%03d.png";
system($command);
echo "Thumbnail has been generated";
Now you can run the script and you will see a form with an input field from where you will be able to select the video from your computer. After selecting the video file when you hit the submit button, you will see several new image files will be created at the root of your project’s folder with a prefix thumbnail-“. The time required to process the video and generate thumbnails depends on the length and quality of video. The videos of larger length and of high quality will take longer to process than the one in shorter length.
php.ini
When uploading large files, php.ini plays an important role either you are working on localhost or on live server. Mostly it is located in your XAMPP “etc” folder if you are using Linux or Mac, or if your site is live on server. If you are using Windows OS, then it will be in your XAMPP “php” folder. You may need to change some of the settings to allow the user to upload larger files. Search “upload_max_filesize=128M” in your php.ini file and change it’s value to 512M or 1GB or any other size you want. In same php.ini file, you may need to search for “post_max_size=128M” and increase it’s value too.
That’s all for now. I have also attached all the source files below, make sure you download them and let us know if you face any issue.
[wpdm_package id=’160′]
HI i have my project on cakephp framework but i am unable to create video to gif . and i also try above method and script but nothing works . i already check path by “whereis ffmpeg ” as root user in terminal it says usr/bin/ffmpeg and /usr/local/bin/ffmpeg i also enable exec from php manager but nothing happens i try
$gif = “/usr/local/bin/ffmpeg -ss 3 -t 2 -i $original_video_file_path -vf ‘fps=10,scale=100:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse’ -loop 0 $genrateGifPath >> video_gif.log 2>&1”;
exec($gif);
but log says : sh: usr/local/bin/ffmpeg: No such file or directory
and i also try : $gif = “ffmpeg -ss 3 -t 2 -i $original_video_file_path -vf ‘fps=10,scale=100:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse’ -loop 0 $genrateGifPath >> video_gif.log 2>&1”;
exec($gif);
now log say : sh: ffmpeg: command not found
i also try shell_exec($gif); it also give same result
what i am doing wrong ??
Make sure you have FFmpeg installed on your server. Contact your hosting provider, they can install FFmpeg.