MPEG Thumbnail Generator

This is a procedure to allow generation of a "thumbnail" image from MPEG videos. A thumbnail will consist of six frames (first, last, and four evenly spaced in between). MPEG-1 and MPEG-2, video only and system (audio and video) files are supported.


Stocking Your Toolbox

As I use it, the procedure requires the following:

1. MPEG2TH

MPEGTH extracts the six frames from the MPEG file. No attempt is made to perform "smart" frame selection, so if the first or last frames in your MPEG file are blank, that's what you will see in the thumbnail image. Last MPEGTH update - Feb. 3, 2001.

MPEGTH was based on the mpeg2decode program provided by the MPEG Software Simulation Group (MSSG). The program may be redistributed freely, but if you post it on your own web site, please acknowledge this site as the source of the program.

2. ImageMagick

This is a package of command line image manipulation utilities. The montage program is used to combine the six frames extracted with MPEGTH into a single JPEG file. Pre-compiled binaries are available for a number of operating systems. Full source code is also available.

3. Perl (optional)

I have written a Perl script to run the program conveniently in batch mode. If you have a Unix system, there's a good chance that Perl is already available on your system. Windows users have several Perl ports available; I use the IndigoPerl version.


Running MPEG2TH

I run mpeg2th and montage with the following options:

mpeg2th -b [MPEG file] -o3 temp%4.4d montage -geom 320x240 -tile 3x2 temp*.ppm [JPEG file]
I have written a Perl script which simplifies running the programs and allows for easy batch mode execution on many MPEG files. For each MPEG file, the script checks to determine whether or not an up to date thumbnail file already exists (the thumbnail date is compared with the MPEG date).

1. Windows

Save the following Perl script in a file (for example, mpegth.pl), and run it in the "MS-DOS prompt" window by typing perl mpegth.pl [MPEG file1] [MPEG file2] ...

You may reference subdirectories using wildcards. Subdirectory specifications follow the Unix standard rather than the usual DOS/Windows standard, so use forward slash ("/") where you would normally use back slash ("\") under Windows. For example, the following command would process all files matching "*.mpg" in subdirectories "subdir1", "subdir2", and "mydir": perl mpegth.pl subdir*/*.mpg mydir/*.mpg.

#!perl @ARGV = glob ("@ARGV"); foreach $file (@ARGV) { $base = $file; $base =~ s/.mpg//i; $base =~ s/.mpeg//i; $skip = 0; # If the JPG (thumbnail) file exists, compare its date with the date of # the MPEG file. Set the "skip" flag so that a thumbnail is not created # if the existing thumbnail is up to date. if (-f $base . ".jpg") { $jdate = -M $base . ".jpg"; $mdate = -M $file; if ($jdate < $mdate) { print "$file: thumbnail is up to date\n"; $skip = 1; } } if (!$skip) { print "Processing $file\n"; system ("mpeg2th -b \"$file\" -o3 temp%4.4d"); @ppmfiles = glob ("*.ppm"); # Change the path to "montage" to whatever is appropriate for your system, # or add the ImageMagick directory to your PATH. system ("/tools/imagemagick-win2k/montage -geom 320x240 -tile 3x2 @ppmfiles \"$base.jpg\""); system ("del temp????.ppm"); } }

2. Unix

Here is a version of the script for Unix users, with just a few minor changes. You will need to change the first line if the Perl executable is located somewhere other than in /usr/local/bin.

#!/usr/local/bin/perl foreach $file (@ARGV) { $base = $file; $base =~ s/.mpg//i; $base =~ s/.mpeg//i; $skip = 0; # If the JPG (thumbnail) file exists, compare its date with the date of # the MPEG file. Set the "skip" flag so that a thumbnail is not created # if the existing thumbnail is up to date. if (-f $base . ".jpg") { $jdate = -M $base . ".jpg"; $mdate = -M $file; if ($jdate < $mdate) { print "$file: thumbnail is up to date\n"; $skip = 1; } } if (!$skip) { print "Processing $file\n"; system ("mpeg2th -b $file -o3 temp%4.4d"); system ("montage -geom 320x240 -tile 3x2 temp????.ppm $base.jpg"); system ("rm temp????.ppm"); } }


This page was created on Jan. 14, 2001.
Last modified on Feb. 23, 2001.

This page is maintained by fbb_fan@hotmail.com.