FFMpeg Logo

FFmpeg: Create Thumbnail Image From a Video

I’ve been doing a lot of work with FFmpeg recently, which if you don’t know already is a really awesome video/audio conversion library, so I thought I would do a few posts on some of the basics of what FFmpeg can do. We’ll start with creating a thumbnail image from a video.

Make sure FFmpeg is Installed

The first thing to do is verify that FFmpeg is installed on your web server. If you’re on shared hosting, contact your provider and ask them if it’s installed and how you can access it. I’m on shared hosting at DreamHost and their install is located at /usr/bin/ffmpeg which is very similar to most servers. If you log into Shell, run the following command to make sure FFmpeg is installed:

# ffmpeg
FFmpeg version 0.5.3, Copyright (c) 2000-2009 Fabrice Bellard, et al.
configuration: --disable-avisynth --disable-demuxer=v4l --disable-demuxer=v4l2 --extra-cflags=-O2 --enable-avfilter --enable-libfaac --enable-libmp3lame --enable-libx264 --enable-gpl --enable-nonfree --enable-postproc --enable-pthreads
libavutil     49.15. 0 / 49.15. 0
libavcodec    52.20. 1 / 52.20. 1
libavformat   52.31. 0 / 52.31. 0
libavdevice   52. 1. 0 / 52. 1. 0
libavfilter    0. 4. 0 /  0. 4. 0
libpostproc   51. 2. 0 / 51. 2. 0
built on Dec  9 2010 16:05:17, gcc: 3.4.6 20060404

This will give you the version as well as the configuration options.

If you’re on a private server, you will need to install FFmpeg yourself. This can be difficult if you do not how to compile and install binaries using SSH Shell access to your server. I will probably come back to making an install post once I get time but for now the best thing to do would be to search google for an install walkthrough. You will also need to install and link any specific video/audio libraries ( AAC, x264, LAME, FLV, etc ) depending on what you would like to do.

Creating the Thumbnail

Ok now that we have FFmpeg installed lets get down to the thumbnail script. This is the easy part and I’m assuming you have a video available on your server already.

// where ffmpeg is located
$ffmpeg = '/usr/bin/ffmpeg';

//video dir
$video = 'path/to/video';

//where to save the image
$image = 'path/to/image.jpg';

//time to take screenshot at
$interval = 5;

//screenshot size
$size = '640x480';

//ffmpeg command
$cmd = "$ffmpeg -i $video -deinterlace -an -ss $interval -f mjpeg -t 1 -r 1 -y -s $size $image 2>&1";
$return = `$cmd`;
echo $return;

Lets break down the input options in the FFmpeg command:

-i = input file
-deinterlace = deinterlace pictures
-an = disable audio recording
-ss = start time in the video (seconds)
-t = duration of the recording (seconds)
-r = set frame rate
-y = overwrite existing file
-s = resolution size
-f = force format

The last bit, “2>&1”, will just return the results of the command to our variable. It’s good for debugging.

Thumbnail at a Random Interval

Now if we don’t want a screenshot at a set time, we can one at a random time by replacing $interval with the following:

$cmd = "$ffmpeg -i $video 2>&1";
if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
	$total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
	$interval = rand(0, $total);

This will compute the total time of the video then choose a random position between start and end of the video.

We’re Done

Now we have a thumbnail image of our video which we can use to show prior to loading the video (ie. HTML5 poster attribute). This is one of great things you can do with FFmpeg. I will try to touch soon on encoding videos, audio, creating videos from images, creating animated gifs from a video.

Continue Reading

  • No Related Posts
  • Thanks a lot. Especially for listing the commands and what they mean. I needed to know how to change the thumb size. Cheers.

  • Eric Wheeler

    Have you ever tried to convert a .swf to an animated .gif by chance? A search in Google yielded little but adding zlibs-dev which I already have installed as well as build-essentials. I’m running Fedora 22. Just on the outside chance you may have run across needing to do this and know how.

  • I have a directory (with also sub-directory) that contain bunch of videos which i want to generate all their thumbnails in just one click.
    The image thumbnail must be generated in the same directory with it’s video and has similar name with the video file.
    for example : in folder “My video” there are 2 video file “video1.mp4” and “video2.mp4”, so the generated thumbnails will be in same directory with name “video1.mp4.jpg” and “video2.mp4.jpg”

    any clue on how to do this?

  • Mohammad Shaju

    insert your codes into a .php file. and visit on a browser. output is below.

    ffmpeg version N-89882-g4dbae00bac-static https://johnvansickle.com/ffmpeg/ Copyright (c) 2000-2018 the FFmpeg developers
    built with gcc 6.4.0 (Debian 6.4.0-11) 20171206
    configuration: –enable-gpl –enable-version3 –enable-static –disable-debug –disable-ffplay –disable-indev=sndio –disable-outdev=sndio –cc=gcc-6 –enable-fontconfig –enable-frei0r –enable-gnutls –enable-gray –enable-libfribidi –enable-libass –enable-libvmaf –enable-libfreetype –enable-libmp3lame –enable-libopencore-amrnb –enable-libopencore-amrwb –enable-libopenjpeg –enable-librubberband –enable-librtmp –enable-libsoxr –enable-libspeex –enable-libvorbis –enable-libopus –enable-libtheora –enable-libvidstab –enable-libvo-amrwbenc –enable-libvpx –enable-libwebp –enable-libx264 –enable-libx265 –enable-libxvid –enable-libzimg
    libavutil 56. 7.100 / 56. 7.100
    libavcodec 58. 9.100 / 58. 9.100
    libavformat 58. 5.101 / 58. 5.101
    libavdevice 58. 0.101 / 58. 0.101
    libavfilter 7. 11.101 / 7. 11.101
    libswscale 5. 0.101 / 5. 0.101
    libswresample 3. 0.101 / 3. 0.101
    libpostproc 55. 0.100 / 55. 0.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from ‘v.mp4’:
    major_brand : isom
    minor_version : 512
    compatible_brands: isomiso2avc1mp41
    title : 418433341907549
    encoder : Lavf56.40.101
    Duration: 00:03:32.65, start: 0.000000, bitrate: 228 kb/s
    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 224×400, 203 kb/s, 16.58 fps, 16.58 tbr, 90k tbn, 33.10 tbc (default)
    handler_name : VideoHandler
    Stream #0:1(und): Audio: aac (HE-AAC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 20 kb/s (default)
    handler_name : SoundHandler
    ss.jpg: Permission denied