#! /usr/bin/perl -w
# Embed cover art (from folder.jpg) into all MP3 files in directory
# 
# Attempts to preserve the timestamp on the resulting file by saving it
# to a reference file first
#
# Requires mp3info2 (packaged in 'libmp3-tag-perl') from CPAN:
# http://search.cpan.org/~ilyaz/MP3-Tag-0.9712/examples/mp3info2
# 
# NOTE: libmp3-tag-perl in Debian Etch (0.9708) and Debian Lenny (0.9710)
# doesn't contain the required functionality, as it was only added in 0.9711
# and 0.9712 was the next actual release:
#
# http://cpansearch.perl.org/src/ILYAZ/MP3-Tag-0.9712/Changes
#
# Based on hints at:
# http://www.richardfarrar.com/embedding-album-art-in-mp3-files/
#
# Written by Ewen McNeill <ewen@naos.co.nz>, 2009-12-28
#---------------------------------------------------------------------------

use strict;

my $IMAGE_FILE = "folder.jpg";
my $MP3_INFO   = "mp3info2";
my $TIME_REF   = "timeref";

die "Cover art ($IMAGE_FILE) missing\n" unless (-f $IMAGE_FILE);
die "'$TIME_REF' already exists, cannot continue\n" if (-f $TIME_REF);

foreach my $mp3 (glob "*.mp3") {
   print "Adding image to $mp3\n";
   system ("touch",   "-r", $mp3,   $TIME_REF);
   system ($MP3_INFO, "-F", "APIC < $IMAGE_FILE", $mp3);
   system ("touch",   "-r", $TIME_REF, $mp3);
   unlink($TIME_REF);
}
