Available languages

Useful links
Читать на русском

Saturday, April 23, 2011

Converting MP3s to M4B audiobooks, and M4B to MP3

Here’s how to convert M4B audiobooks (used by Apple iPod) to MP3, and how to create an M4B from one or more MP3s.
The easy bit first: M4B to MP3
For a single file:
ffmpeg -i -acodec libmp3lame -ar 22050
For a bunch of M4B files:
for m4b in $(ls -1 *.m4b); do ffmpeg -i $m4b -acodec libmp3lame -ar 22050 ${m4b}.mp3; done

The more involved bit is to create an M4B audiobook from a bunch of MP3 files, so I have an automated script.
First thing is to ensure all the MP3 files you want to include in ONE M4B have sequentially numbered names:
filename-01.mp3
filename-02.mp3
or, if you want to create the M4B from more than 100 M4B files:
filename-001.mp3
filename-002.mp3
etc.
Copy and save the script below to the file create_audiobook and make it executable:
chmod 755 create_audiobook
For each M4B audiobook optionally set the tags using environmental variables:
export AUTHOR="The Author"
export TITLE="Some Book"
export YEAR="2007"

Make sure you’ve got plenty of free space on the mount point the source files are on. If you need to have the HUGE temporary files put on another mount point with plenty of space (Allow for more than 3GB of free space) then set the TMP environmental variable to point to a location that has plenty of space and allows users to create/delete files:
export TMP=/tmp
By default, TMP is set to the current directory if not specified like this.
Now run the script, passing the output filename and the input file mask. Here’s two possible usages:
create_audiobook "Terry Pratchett - Colour of Magic" "Terry Pratchett - Colour of Magic - *.mp3"
create_audiobook "Terry Pratchett - Colour of Magic" *.mp3
Note the quotes around the two parameters that allows spaces in filenames.
#!/bin/bash
# create ipPod audiobook from group of sequentially named MP3 files
# Set environmental variables for track ID3 tags
# AUTHOR, TITLE, YEAR
# usage: create_audiobook
#
# Pass output filename on command-line with no extension, and use quotes if there are spaces in filenames
# e.g. create_audiobook "Terry Pratchett - Colour of Magic" *.mp3
# e.g. create_audiobook "Terry Pratchett - Colour of Magic" "Terry Pratchett - Colour of Magic - *.mp3"

if [ "x$TMP" == "x" ]; then
TMP=.
fi
echo “Args: $@”
echo “Author: $AUTHOR”
echo “Title : $TITLE”
if [ "x$1" != "x" ]; then
filename=$1
if [ "x$2" != "x" ]; then
echo “Output: $filename”
filelist=”$(echo “$2″ | sed -e ‘s/ /\\ /g’)”
echo “Filelist : $filelist”
echo “Writing HUGE temporary files to $TMP”
# Use a sub-shell to preserve spaces in filenames passed to mp3wrap
sh -c “mp3wrap -v \”${TMP}/${filename}\” ${filelist}”
if [ $? = 0 ]; then
mplayer -vc null -vo null -ao “pcm:nowaveheader:fast:file=${TMP}/${filename}.pcm” “${TMP}/${filename}_MP3WRAP.mp3″ 2>&1 | tee ${TMP}/create_audiobook_mp.log
# example from log
# AO: [alsa] 44100Hz 2ch s16le (2 bytes per sample)
AUDIO=”$(egrep ‘AO:’ ${TMP}/create_audiobook_mp.log)”
echo “Audio Format: $AUDIO”
RATE=$(expr “$AUDIO” : ‘AO: .* \(.*\)Hz.*’)
CHANS=$(expr “$AUDIO” : ‘AO: .* .*Hz \(.*\)ch.*’)
SAMPLESIZE=$(expr “$AUDIO” : ‘AO: .* .*Hz .*ch s\([0-9]\+\).. .*’)
echo “ENCODING: $RATE Hz, $CHANS channels, $SAMPLESIZE bit sample size”
/usr/bin/faac -R $RATE -B $SAMPLESIZE -C $CHANS -X -w -q 80 –artist “$AUTHOR” –album “$TITLE” –title “$TITLE” –track “1″ –genre “Spoken Word” –year “$YEAR” -o “${filename}.m4b” “${TMP}/${filename}.pcm”
rm -f “${TMP}/${filename}.pcm”
rm -f “${TMP}/${filename}_MP3WRAP.mp3″
rm -f “${TMP}/create_audiobook_mp.log”
echo -e “\n\nCreated ${filename}.m4b”
fi
fi
fi


Article is token from http://intuitivenipple.net/10/converting-mp3s-to-m4b-audiobooks-and-m4b-to-mp3