Skip to content

Instantly share code, notes, and snippets.

@jblyberg
Created January 29, 2012 04:22
Show Gist options
  • Select an option

  • Save jblyberg/1697182 to your computer and use it in GitHub Desktop.

Select an option

Save jblyberg/1697182 to your computer and use it in GitHub Desktop.
Creates an iPod/iTunes .m4b audiobook file from a bunch of MP3s
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment