make_m4b
jfroebe
created: 2006-03-04 22:48:02
#!/usr/bin/perl

use warnings;
use strict;

use MP3::Info;
use Getopt::Long;
use Pod::Usage;

##########
##########
##########
sub convert_mp3 {
        my ($filename, $bitrate, %META_DATA) = @_;

        my $metadata = "";
        my $tmp_meta_ref = get_mp3tag($filename);

        ### override the mp3 tags if desired
        foreach my $key (%META_DATA) {
                next unless ($key && $META_DATA{$key});
                $tmp_meta_ref->{$key} = $META_DATA{$key};
        }

        foreach my $key (%$tmp_meta_ref) {
                next unless ($key && $tmp_meta_ref->{$key});
                next if ( lc($key) eq "tagversion" );

                if (lc($key) eq 'tracknum') {
                        $tmp_meta_ref->{'track'} = $tmp_meta_ref->{$key};
                        $key = 'track';
                }

                $tmp_meta_ref->{$key} =~ s/\"/\\\"/g; ### Escape out "s
                $metadata .= sprintf " --%s \"%s\"", lc($key), $tmp_meta_ref->{$key};
        }

        my $outputfilename = $filename;
        $outputfilename =~ s/\.mp3$/\.m4b/;

        system "mpg123 -w - \"$filename\" | faac -w -b$bitrate $metadata -o \"$outputfilename\" -";
}

##########
##########
##########
sub convert_realaudio {
        my ($filename, $bitrate, %META_DATA) = @_;

        require POSIX;

        my $metadata = "";
        my $outputfilename = $filename;
        my $my_fifo = "/tmp/make_m4b.$$";
        $outputfilename =~ s/\.(?:rm|ram|ra)$/\.m4b/i;

        POSIX::mkfifo($my_fifo, 0666)
                or die "ERROR: can't create named pipe $my_fifo\n";

        foreach my $key (%META_DATA) {
                next unless ($key && $META_DATA{$key});
                $META_DATA{$key} =~ s/\"/\\\"/g; ### Escape out "s
                $metadata .= sprintf " --%s \"%s\"", $key, $META_DATA{$key};
        }

        system "faac -w -b$bitrate $metadata -o \"$outputfilename\" $my_fifo \&";
        system "mplayer -nortc -ao pcm:file=$my_fifo $filename";

        unlink $my_fifo;
}


##########
##########
##########
##########
##########
##########
##########

my $man = 0;
my $help = 0;
my ($bitrate, $artist, $title, $album, $genre, $comment, $year, $track);

GetOptions('help|?' => \$help, man => \$man, 'bitrate=s' => \$bitrate, 'artist=s' => \$artist, 'title=s' => \$title, 'album=s' => \$album, 'genre=s' => \$genre, 'comment=s' => \$comment, 'year=s' => \$year, 'track=s' => \$track)
        or pod2usage(2);

pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

$bitrate = 64 unless $bitrate;

foreach my $filename (@ARGV) {
        $filename =~ m/\.(\w+)$/;
        my $suffix = $1;

        if ($suffix =~ /mp3/i) {
                convert_mp3($filename, $bitrate, ('artist' => $artist, 'title' => $title, 'genre' => $genre, 'comment' => $comment, 'year' => $year, 'track' => $track) );
        } elsif ($suffix =~ /(?:ra|rm|rv)/i) {
                convert_realaudio ($filename, $bitrate, ('artist' => $artist, 'title' => $title, 'genre' => $genre, 'comment' => $comment, 'year' => $year, 'track' => $track) );
        }
}

__END__

=head1 NAME

 make_m4b - Converts mp3 and realaudio files to bookmarkable mpeg4 files

=head1 DESCRIPTION

B will convert mp3 or realaudio files to bookmarkable m4b files (ipod).  If MP4 info is not supplied, we will attempt to pull the info from the file (mp3 only).

=head1 SYNOPSIS

 make_m4b [options] [file ...]

=head1 OPTIONS

=over 8

=item B<--help>

 Print a brief help message and exits.

=item B<--man>

 Prints the manual page and exits.

=item B<--bitrate>

 Set the bps for the m4b file (default = 64)

=item B<--artist>

 Set artist to X

=item B<--title>

 Set title to X

=item B<--album>

 Set album to X

=item B<--genre>

 Set genre to X

=item B<--comment>

 Set comment to X

=item B<--year>

 Set year to X

=item B<--track>

 Set track to X

=back

=head1 AUTHOR

Jason L. Froebe (jasonfroebeD0Tnet)

=cut


perlmonks.org content © perlmonks.org and jfroebe

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03