How-to create an audiobook mp3/m4b from a bunch of mp3′s – take #2
Posted: January 30th, 2009 | Author: ari.bjornsson | Filed under: Tech | Tags: bash, convert to audiobooks, faac, faad, id3v2, join mp3 files, lame, Linux, m4b, mp3, mp3check, mpgtx, mplayer, python, reencode, wav | 6 Comments »Lately I’ve been fooling around with Python, to be exact I took the opportunity to learn the programming language because I wanted to remake my “jencode” a.k.a. “join-and-encode” bash script, which converts multiple mp3 files to one m4b file (iTunes audiobook format), in a more powerful programming language. To make a long story short, I’ve done so.
A brief summary of the issue; acquiring a pre-ripped audiobooks from the internet is easy but usually they’re broken up into multiple mp3 files and strangely enough some of those mp3 files may not be of the same quality as the others. This is a problem for those who rather want their audiobook to be in one file, mp3 or m4b.
Like I mention before I created a linux bash script to do that but now I’ve recreated it as Python script, actually I’ve created several Python scripts which essentially have the same functionality as the lone bash script – I did this for ease of use. Unfortunately these Python scripts only work on linux. Speaking for myself, I use these scripts extensively and find them very useful and much better than any gui program I’ve come across.
I’d appreciate feedback on things that go wrong or how to improve these scripts or more importantly what methods or packages to use in conversion of these audio files. It’s ok to comment here or just drop me a line; ari.bjornsson (at) gmail.com
Before continuing make sure that you grab a copy of these scripts:
- audiojoin.py – provides useful information about mp3 files as well as to join them together as one
- mp3reencode.py – encodes mp3 files contained in a given folder
- audio2m4b.py – converts mp3 files contained in a given folder to m4b format
- m4b2mp3.py – converts m4b files contained in a given folder to mp3 format
also make sure you have the following packages installed:
- faac, faad, mpgtx, lame, id3v2, mp3check and mplayer
now, place the scripts in directory: /usr/bin so they can be called without giving full path.
audiojoin.py
This script uses the mpgtx package to join together multiple mp3 files. These files, however, need to have the same bitrate, sampling rate and channels. Before using the join function of this script it is recommended that -s flag is used to if there are any discrepancies between the files and if there are any then its necessary to reencode all files using the mp3reencode.py script. The output of this script is a joined mp3 file.
Note: you may encounter a batch of mp3 files which are identical but encoded with encoders like mpg123 which are incompatible with mpgtx – in these cases it’s also recommended to use mp3reencode.py script.
Examples:
- #audiojoin.py -s % provides statistical summary of all mp3 in current directory
- #audiojoin.py -i Track01.mp3 % provides more detailed information about a single mp3 file
- #audiojoin.py -jr % joins together all mp3 files in current directory as well as all mp3 files in subdirectories
- #audiojoin.py -jd /media/audiobooks/Cinderella % joins together mp3 files contained in supplied folder
mp3reencode.py
This script uses the lame package to reencode mp3 files according to supplied parameters, by default these parameters are; sampling rate [22.05 kHz], bitrate [32 kbps], bitmode [constant bitrate] and channels [sterio]. These parameters can be overwritten by using appropriate flags and values, refer to: #mp3reencode.py -h . The output of this script is a mp3 file which has the same name as the input mp3 file but with _REENCODED_.mp3 ending.
Examples:
- #mp3reencode.py -d /media/audiobooks/The\ Silmarillion % reencodes all mp3 files contained in supplied directory using default values
- #mp3reencode.py -s 44.1 -b 128 -c j % reencodes all mp3 files in current directory with numerous flags, bitmode is set as default value
- #mp3reencode.py -r -b 32 -m abr % reencodes all mp3 files in current directory as well as files in subdirectories with average bitrate 32 kbps set but others default value
audio2m4b.py
This script uses mplayer to decode the input file to am expanded pcm format where as faac is used to encode pcm file to m4b, no input parameters are necessary. Note: by default the script goes through subdirectories as well.
Examples:
- #audio2m4b.py % encodes all mp3 files to m4b files, including files in subdirectiories
- #audio2m4b.py -wd /media/audiobooks/Hidden\ Empire % encodes all wave files to m4b which are found in given directory and its subdirectories
m4b2mp3.py
This script uses faad package to decode the m4b files provided or contained in the provided directory and simutaneously converted to mp3 using lame and no input parameters are necessary. Note: by default the script goes through subdirectories as well.
Examples:
- #m4b2mp3.py % decodes m4b files in current directory and subdirectories and consequently encodes the output to mp3
- #m4b2mp3.py -d /media/audiobook/Bourne\ Supremacy % decodes m4b flies in supplied directory and subdirectories and encodes the output to mp3



Recent Comments