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
  • http://www.clothes4u.com.au Volker Apparel

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