Here's a script I wrote a while ago that I use for adding covers to my
mp3 mirrored directory so they display on my iPhone. It uses
Audio::TagLib which is somewhat hard to compile, and I wouldn't choose
if I was writing this today, but it works well enough.
Code:
--------------------
#!/usr/bin/perl
#
# Insert cover.jpg into mp3 files that don't already have cover art
use strict;
use File::Find::Rule;
use File::Path;
use File::Slurp qw(read_file);
use File::Spec::Functions;
use Audio::TagLib;
$|++;
my %missing;
my $dir = shift || die "Usage: $0 directory\n";
# Now look for files in the given directory
my @mp3_files = File::Find::Rule->file()->name('*.mp3', '*.MP3')->in($dir);
for my $file ( @mp3_files ) {
next if $file =~ /\.AppleDouble/;
# Look for a cover.jpg file
my ( $src_volume, $src_directories, $src_file ) =
File::Spec->splitpath($file);
my $cover_file = catfile( $src_directories, 'cover.jpg' );
unless ( -r $cover_file ) {
#print "no cover file found, skipping\n";
$missing{$src_directories} = 1;
next;
}
# Check if the file already has embedded artwork
my $f = Audio::TagLib::MPEG::File->new($file);
my $id3v2 = $f->ID3v2Tag() || next;
my $frameListMap = $id3v2->frameListMap();
my $bv = Audio::TagLib::ByteVector->new('APIC');
if ( $frameListMap->contains($bv) ) {
print "already contains APIC ($file)\n";
next;
}
# Add cover to APIC
my $jpg = read_file($cover_file) or die "Unable to read cover file:
$cover_file ($!)";
my $apic = Audio::TagLib::ID3v2::AttachedPictureFrame->new();
$apic->setType( 'FrontCover' );
$apic->setMimeType( Audio::TagLib::String->new( 'image/jpeg' ) );
my $pic = Audio::TagLib::ByteVector->new();
$pic->setData( $jpg, length($jpg) );
$apic->setPicture( $pic );
$id3v2->addFrame( $apic );
$f->save();
print "$file: added cover to tags\n";
}
# Log missing covers
open my $fh, '>', 'missing-covers.txt';
for my $dir ( sort keys %missing ) {
print $fh "$dir\n";
}
close $fh;
--------------------
--
andyg
------------------------------------------------------------------------
andyg's Profile: http://forums.slimdevices.com/member.php?userid=3292
View this thread: http://forums.slimdevices.com/showthread.php?t=76358
_______________________________________________
ripping mailing list
[email protected]
http://lists.slimdevices.com/mailman/listinfo/ripping