#!/usr/bin/perl -w
#
# script to return title, author, copyright and comment of mp3 track
#
# Version 1
#basic MP3 info

#modules
use MP3::Info;

#Variable declarations
my ($mp3, $file, $AAC_file, $command, $AAC_bookmarked_file);
my (@tags);

#get the filename for the command line
$file = $ARGV[0] || die 
"Useage: $0 <filename>\npconverts a .mp3 file to aac.  Requires ffmpeg\n";

#documentation for tag names
#set_mp3tag (FILE, TITLE, ARTIST, ALBUM, YEAR, COMMENT, GENRE [, TRACKNUM])
@tags = qw(FILE TITLE ARTIST ALBUM YEAR COMMENT GENRE);

#define your ffmpeg location here
$ffmpeg = "/usr/bin/ffmpeg";

#check we have a valid file and ffmpeg
#unless  (-f $ffmpeg) { die "no ffmpeg encoder found at $ffmpeg\n"; } 
unless (-f $file) { die "no such file: $file\n"; }

#create the OO object and die on error
$mp3 = new MP3::Info $file || die "Cannot get mp3 information from $file because: $!";

#get the metadata from the mp3
print "\n";

for $tag_info (@tags)
{
	print "$tag_info,";
	if ($mp3->{$tag_info})
	{
		print $mp3->{$tag_info};
	}
	print "\n";
}

#get the filename to make an .m4a AAC version
$file =~ m/(.+)\.mp3$/;
$AAC_file = ($1 . ".m4a");
$AAC_bookmarked_file = ($1 . ".m4b");

#run ffmpeg on the file to produce an AAC with the correct metatdata
#print "\nAACFilename: $AAC_file\n";

$command = $ffmpeg;
$command .= " -i $file ";
$command .= "-title ";
$command .= '"' . $mp3->{TITLE} . '" ';
$command .= "-author ";
$command .= '"' . $mp3->{ARTIST} . '" ';
$command .= "-copyright ";
$command .= '"' . $mp3->{ARTIST} . '" ';
$command .= "-comment ";
$command .= '"' . $mp3->{COMMENT} . '" ';
#$command .= "-hq ";
#generate a stereo file, otherwise it locks up iPods
$command .= "-ac 2 ";
$command .= $AAC_file;

#print "\n$command\n";
if (system $command) { die "\nffmpeg conversion failed: $0\n"; }


#now make the resulting AAC file bookmarkable 
rename ($AAC_file, $AAC_bookmarked_file) || die "could not rename files because: $!";


=pod
=head1 mp3ToAAC.pl

useage: B<mp3ToAAC.pl> <mp3 file>

uses ffmpeg to convert a mp3 file to a bookmark-able AAC file suitable for transferring to an iPod.  Due to a bug with iPod firmware (as of 12/06/05), it forces mono files to stereo (otherwise they lock up).  It also transfers as much metadata as possible from the mp3 to the AAC.  Unfortunately, FFmpeg or AAC format (not sure which), only manages title, author, copyright and comment.

Requires ffmpeg;
http://ffmpeg.sourceforge.net

Written by john@kript.net.  Licensed under the GPL.

Check for updates at http://cgi.kript.net/blosxom.cgi/code/

Download the latest version; http://www.kript.net/perl/mp3ToAAC.pl

=cut
