#!/usr/local/bin/perl -w
 use strict;
 use File::Temp qw( tempdir );
 use File::Copy qw( move );
 use DateTime::Duration;
 use DateTime::Format::Duration;
 use Image::Magick;

 my %seen = ();

 my $video = shift @ARGV;
 if( !defined $video ) {
   die "usage: $0 video";
 }

 my $tmpdir = tempdir( CLEANUP => 1 );
 my $magick = Image::Magick->new;

 while( <> ) {
   chomp;
   my $second = $_;
   next if $seen{ $second }++;

   system "mplayer", "-ss", $second, $video,
     "-vo", "jpeg:outdir=$tmpdir",
     "-ao", "null", "-frames", 1;

   my( $frame ) = glob "$tmpdir/0*";
   my $newname = sprintf
     "$tmpdir/frame-%s.jpg",
       secs_format( $second );
   move $frame, $newname;
   $magick->Read( $newname );
 }

 my $montage = $magick->Montage(
     label  => "%f",
     shadow => "True",
     tile   => "5",
 );

 $montage->Write( "motion-meta.jpg" ) and
   die "write failed";

 sub secs_format {
   my( $secs ) = @_;

   my $fmt =
     DateTime::Format::Duration->new(
       pattern => "%T" );

   return $fmt->format_duration(
     DateTime::Duration->new(
       seconds => $secs )
   );
 }