Converting audiobooks to m4b sucks!

Ok, I download stuff from the internet so sue me! Since I'm an iSheep I want to use my iPod/iPhone to listen to to my audiobooks that I download. The problem with this is that all releases are in MP3 format and this messes up my play lists when doing a shuffle of all songs; all of a sudden there is a chapter from an audiobook read. So, what to do. A cupple of months ago I wrote a script that converted m3u to m4b, and now I've rewritten it. So, here it is! Execute the script with directory-with-m3u-files as argument.

#!/usr/bin/perl
 
use File::Basename;
my @m3u;
my @files;
my $f;
my $count = 1;
my $discs;
opendir(DIR, $ARGV[0]) or die("Can't open \"$ARGV[0]\"!\n");
@files = readdir(DIR);
closedir(DIR);
$path = $ARGV[0];
foreach $f ( @files ) {
        if ( ! -d $f ) {
                if ( ($f =~ m/^.*\.m3u$/) && ! ( $f =~ m/^0000.*$/) ) {
                        push(@m3u, $f);
                }
 
        }
}
@m3u = sort(@m3u);
$discs = @m3u;
print "Found $discs m3u files.\n";
foreach $m ( @m3u ) {
        my @data;
        $file = $m;
        $file =~ s/^(.*)\..*/\1/;
        $outfile = $file.".m4b";
        open ID3, "<$ARGV[0]/$m" or die("File \"$ARGV[0]/$m\" not found!\n");
        while ( <ID3> ) {
                push (@data,$_);
        }
        close(ID3);
        if ( -f tmpfile ) {
                print "tmpfile found.. Deleteing\n";
                unlink(tmpfile);
        }
        open(TMPFILE, ">tmpfile");
        foreach $line (@data) {
                if ( $line =~ m/^#.*/ ) {
                } else {
                        if ( $line !=~ m/.*\.rar.*/ ) {
                                print TMPFILE "$path/$line";
                        }
                }
        }
        close(TMPFILE);
        my $id3 = `head -1 tmpfile`;
        print "Getting id3-data from $id3\n";
        @id3 = `id3v2 --list $id3`;
        foreach $line (@id3) {
                if ( $line =~ m/^TALB.*/ ) {
                        $TITLE = $line;
                        $TITLE =~ s/^.*: (.*)\n$/\1/;
                }
                if ( $line =~ m/^TPE1.*/ ) {
                        $AUTH = $line;
                        $AUTH =~ s/^.*: (.*)\n$/\1/;
                }
        }
        print "Got:\n";
        print "Title:  $TITLE\n";
        print "Author: $AUTH\n";
        print "Output: $outfile\n";
        print "Converting..\n";
        system ("mpg321 -@ tmpfile -q -w - | faac -w --disc \"$count/$discs\" --track \"1/1\" --artist \
\"$AUTH\" --title \"$TITLE\" --album \"$TITLE\" --writer  \"$AUTH\" \
--genre \"Audiobook\" -o \"$file.m4b\" - ");
 
        $count++;
}
        print "Done, I think.. Have a nice day!\n";
 

Tags: , , , , , ,

3 Responses to “Converting audiobooks to m4b sucks!”

  1. Gus says:

    Hello, I use a windows computer and I dont know a hell lot about scripting so could you please mail me some instructions or something like that?

    somchai92@gmail.com

  2. Scott Williams says:

    I am writing on behalf of a group of parents of dyslexic students. We use audiobooks very heavily and have been faced with the same problem you describe. It just so happens that all of the students are Mac users. The problem is I’m not at all familiar with script writing. It will be a challenge for me to figure this out. So it’s important for me to have good reason to think this will work before I delve any further. We are needing to convert books on CDROM to .m4b. We have not come across any .m3u files. Does your script convert .mp3 directly to .m4b? Or are there some intermediate steps?

Leave a Reply