Yet another technology tutorial blog.

Fedora Linux Create Video Thumbnail Montage Perl Script

By • Aug 7th, 2010 • Category: Linux, Perl

Here is a Perl script to create a montage of sequential video thumbnails in Fedora Linux 13.

First, Install Packages:

> su –
> yum install perl mplayer ffmpeg ImageMagick
> exit

Log into root and install the necessary packages. The RPM Fusion repository needs to be configured for mplayer, ffmpeg, and ImageMagick.

Copy Perl Script:

> vi thumbs.pl
#!/usr/bin/perl -w
use strict;
use POSIX;
my $dir = getcwd();
my ($width, $height, $length);
opendir(DIR, $dir) or die $!;
while (my $file = readdir(DIR)) {
   next unless ($file =~ m/\.mp4$/);
   my @info = `mplayer -benchmark -nosound -quiet -vo null -frames 1 -identify $file`;
   foreach (@info) {
      if (substr($_, 0, 15) eq "ID_VIDEO_WIDTH=") {
         $width = substr($_, 15);
      elsif (substr($_, 0, 16) eq "ID_VIDEO_HEIGHT=") {
         $height = substr($_, 16);
   elsif (substr($_, 0, 10) eq "ID_LENGTH=") {
         $length = substr($_, 10);
   $width = floor($width / 3);
   $height = floor($height / 3);
   my ($tile_x, $tile_y, $frame) = (6, 4, 1);
   my ($position, $files);
   while ($frame < ($tile_x * $tile_y + 1)) {       $position = $frame * floor($length / ($tile_x * $tile_y));
      system("ffmpeg -ss $position -i $file -vcodec mjpeg -vframes 1 -s $width"."x"."$height -an -f rawvideo -y __$frame.jpg 2> /dev/null");
      $files = $files . " __$frame.jpg";
      $frame += 1;
   system("montage -title \"$file\" -geometry $width"."x"."$height"."+"."1"."+"."1 -tile $tile_x"."x"."$tile_y$files $file.jpg");
   system("rm __*.jpg");

You can also download the thumbs.pl file.

There are a few things you may wish to configure. First is the script only creates montages for mp4 files. Change the regular expression for your needs. Second is the scale of each thumbnail relative to the resolution of the video. I have selected one-third of the original size. Third is the tile layout. I have selected 6 horizontal by 4 vertical tiles. If you are editing the script, note that the -ss option must be before the -i option or else ffmpeg will crawl while creating thumbnails.

Why both mplayer and ffmpeg? mplayer has the ability to do montages but I usually have issues with the -sstep option, and framestep is way too slow. You can certainly pull video data with ffmpeg -i but mplayer -identify is much easier to parse and I was lazy. Sorry.

Run the Perl Script:

> perl thumbs.pl

Place the script into a directory and execute it. You can run it in the background with nohup perl thumbs.pl & instead. The script will find every mp4 file in its directory and make a sequential video thumbnail montage for it.

Update February 14, 2011:

mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.
> vi ~/.mplayer/config

Linux Infrared Remote Control (LIRC) support in mplayer is compiled by default in Fedora Linux. If you do not have a remote or if you wish to disable support, edit the mplayer config file in your home directory to add the nolirc option.

is a technology junkie.
Email this author | All posts by

2 Responses »

  1. -nolirc will also do the same as editing the config


    “mplayer -nolirc -really-quiet -nosound -vo png:z=3 -frames 1 -ss $position $file”

    should work with most any file you have a codec to play

    .. cheers

  2. Great script, thanks!

Leave a Reply