MEncoder rocks

Lately I've had a spate of making simple music videos for upload to youtube, painfully simple really. Just a jpeg + an mp3 but I found that creating these files in the GUI video editing suites I tried was painfully slow and not particularly good. So here's a little perl script that makes life a little easier:

 #!/usr/bin/perl
 use strict;
 
 my $outputdir = $ENV{HOME};
 
 my ($conf, $prev);
 for(@ARGV) {   
   if( $_ =~ /^-(i|a|l|o)$/ ) {
     $prev = $1;
     next;
   } 
   if( $prev ) {
     $conf->{$prev} = $_;
     if( $prev =~ /^(i|a)$/ && ! -f $conf->{$prev} ) {
       die "File: $conf->{$prev} does not exist. You have failed me for the last time.\n";
     }
     $prev = 0;
   }
 }
 
 if( ! $conf->{l} ) {
   my ($min, $sec);
   my $mtime = `exiftool '$conf->{a}' | grep Duration`;
   $mtime =~ s/^Duration\s+:\s?[0]?(\d?[:]?\d+).*$/$1/i;
   if( $mtime =~ ':' ) { 
     ($min, $sec) = split ':', $mtime;
   } else {
     $min = 0;
     $sec = $mtime;
   }
   $sec = 0 if $sec =~ /00/;
   $conf->{l} = ($min * 60) + $sec;
 }
 
 my $mcmd = "mencoder mf://$conf->{i} -ovc lavc -oac copy -audiofile '$conf->{a}'  -fps 1/$conf->{l} -ofps 30 -o $outputdir/$conf->{o}.avi";
 
 print "Executing: $mcmd\n";
 
 system $mcmd;
 

Note this depends on having exiftool and mencoder installed. The syntax is very simple: ./script -i '/path/to/image_file.jpg' -a '/path/to/audio_file.mp3' -o output_file_name

It defaults to outputting to an avi in your home directory but the file size is nice and small and works fine with youtube.