#!/usr/bin/perl -w # # Copyright 2004, Marcus Thiesen (marcus@thiesen.org) # # This program is free software; you can redistribute it and/or modify # it under the terms of either: # # a) the GNU General Public License as published by the Free Software # Foundation; either version 1, or (at your option) any later # version, or # # b) the "Artistic License" which comes with Perl. # # On Debian GNU/Linux systems, the complete text of the GNU General # Public License can be found in `/usr/share/common-licenses/GPL' and # the Artistic Licence in `/usr/share/common-licenses/Artistic'. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # ######################################################################### # # Put an image into a MP3 File # ./id3image image mp3file... # # Prereq: Image::Magick, MP3::Tag # Version: 0.1 # ######################################################################### use strict; use warnings; use Image::Magick; use MP3::Tag; use constant APIC => "APIC"; use constant TYPE => "jpg"; use constant HEADER => ( chr(0x0) , "image/" . TYPE , chr(0x3), "Cover Image"); use constant DEBUG => 0; if (@ARGV < 2) { print "Usage: ./$0 image mp3file...\n"; exit 1; } my $imagefile = shift; my $image = new Image::Magick; if (my $x = $image->Read($imagefile)) { print "Couldn't read the image $imagefile: $x\n"; } my $imagedata = $image->ImageToBlob(magick => TYPE ); undef $image; foreach my $mp3file (@ARGV) { if ( ! -r $mp3file || ! -w $mp3file ) { print "File $mp3file is not rw\n"; next; } my $mp3 = MP3::Tag->new($mp3file); my @retvla = $mp3->get_tags(); unless ($mp3) { print "Couldn't read MP3 $mp3file: $!\n"; next; } my $id3; if (exists $mp3->{ID3v2}) { print "Using old ID3v2 tag\n" if DEBUG; $id3 = $mp3->{ID3v2}; } else { print "Creating new ID3v2 tag\n" if DEBUG; $id3 = $mp3->new_tag("ID3v2"); } my $frames = $id3->supported_frames(); if (!exists $frames->{APIC}) { print "Something is wrong, APIC is not a supported frame!\n"; exit 2; } my $frameids = $id3->get_frame_ids(); if (exists $$frameids{APIC}) { print "Replacing existing APIC entry\n" if DEBUG; $id3->change_frame(APIC, HEADER, $imagedata); } else { print "Creating new APIC entry\n" if DEBUG; $id3->add_frame(APIC,HEADER, $imagedata); } $id3->write_tag(); $mp3->close(); print "Successfully added Image to $mp3file\n"; }