Browse > Home / Computer / Blog article: Convert The Economist Audio Edition into an iPod audiobook with chapters - dayel.com

Convert The Economist Audio Edition into an iPod audiobook with chapters

April 18th, 2010 Posted in Computer

Figure 1: Economist audiobook on ipod nano.  The little vertical lines on the bottom bar are the chapter marks

This script re-orders The Economist Audio Edition mp3 files into an iPod audiobook, allowing you to resume from the last listened position and includes chapter marks for each story, and original artwork.

I’ve been very happy since The Economist started their audio edition—now I can listen to the whole of the magazine on my iPod!  There are two snags, though:

  1. It doesn’t remember your position—if you want to listen to something else on your ipod, you have to remember which story you were on when you go back.
  2. I usually don’t want to listen to the stories in the order they come in—for example, I’d rather hear the Science and Technology, Business, and Finance sections before hearing about Britain and Europe.

I wrote a little script to fix both of these issues, and I’m posting it in case anyone else might find it useful.

Figure 2: Economist audiobook chapter view, each story marked as its own chapter.

The script re-orders the stories, and then packages them into one single file in ipod audiobook format, with chapter marks for each story. This means you can still navigate by either skipping stories by hitting forward or reverse while listening (Figure 1) or going directly to the story you want from the chapter list (Figure 2).  If you listen to something else and then come back, just select ‘Resume’ (Figure 2) to carry on where you left off.

The script requires a number of free command line utilities to be installed:

  • faac and sox are essential for converting the mp3 files into an ipod audiobook (aac m4b file)
  • mp4chaps is needed to set the chapter marks
  • mp4art and eyeD3 are used to set the artwork
  • AtomicParsley sets the file info
  • mp4file optimizes the final file (comes with mp4chaps above in the mp4v2 package)


These can all be installed on OS X with macports using the line:

sudo port install faac sox mp4v2 mpeg4ip py-eyed3 AtomicParsley

or on Ubuntu or Debian Linux with:

apt-get install faac sox mpeg4ip-utils libmp4v2-0 libmp4v2-dev eyed3 atomicparsley

(linux note: mpeg4ip-utils libmp4v2-0 are from multiverse repos, and you need to install mp4chaps manually—download from Google Code and do make ; sudo make install)


Once you have the tools installed, you’re good to go. Here’s the script:

#!/usr/bin/env bash
#
# Script to combine Economist Audiobook mp3s into a single
# iPod 'Audiobook' file, with chapter marks for each story
#
# Mark J Dayel 2010
#
# Download the zip file from economist.com
# extract the mp3s and run this script
#
# Dependencies
# ------------
# this script needs the following programs to be installed:
# ffmpeg sox mp4chaps mp4art mp4file AtomicParsley eyeD3
#
# What it does
# ------------
# - Copies mp3 files into a temporary directory, re-ordering
#   them as specified.
# - Finds the length of each track and keeps a running total
#   to figure out where the 'chapter' marks should be.
# - Combines the mp3 files (with sox) and pipes the combined stream to faac
#   for encoding into a single aac file.
# - adds the chapter marks and the artwork.
#

rm -f working.chapters.txt
rm -f working.m4b

mkdir -p renumbered

echo
echo -n Rearranging tracks

ls -1 *.mp3  | while read FILENAME
do
echo -n .
LOWERFILE=$(echo $FILENAME | tr [:upper:] [:lower:])

# change the order below to change the order of the stories

case ${LOWERFILE:4:100} in

"introduction"* )
ORDINAL=A01 ;;
"twtw"* )
ORDINAL=A02 ;;
"the world this week"* )
ORDINAL=A02 ;;
"leaders"* )
ORDINAL=A03 ;;
"letters"* )
ORDINAL=A04 ;;
"briefing"* )
ORDINAL=A05 ;;
"united states"* )
ORDINAL=A06 ;;
"science and technology"* )
ORDINAL=A07 ;;
"international"* )
ORDINAL=A08 ;;
"business"* )
ORDINAL=A09 ;;
"finance and economics"* )
ORDINAL=A10 ;;
"special report"* )
ORDINAL=A11 ;;
"technology quarterly"* )
ORDINAL=A12 ;;
*"bagehot"* )
ORDINAL=A13 ;;
*"charlemagne"* )
ORDINAL=A14 ;;
*"banyan"* )
ORDINAL=A15 ;;
"books and arts"* )
ORDINAL=A16 ;;
"britain"* )
ORDINAL=A17 ;;
"europe"* )
ORDINAL=A18 ;;
* )
ORDINAL=A19 ;;

esac

cp "$FILENAME" renumbered/"$ORDINAL $FILENAME"

done

echo done
echo

function converttomp4 {

TRACKS=`ls -1 *.mp3 | wc -l | xargs`

TRACKNO=0
CHAPTERTIME=0
TIMESTR=00:00:00.000

echo
echo Finding chapter positions...
echo

ls -1 *.mp3 | while read FILENAME
do

((TRACKNO=TRACKNO+1))

BASENAME="`echo ${FILENAME%.*}`"

TITLE="$BASENAME"
TITLE="${TITLE:8:150}"

echo $TRACKNO/$TRACKS $TIMESTR $TITLE
echo $TIMESTR $TITLE >> working.chapters.txt

# We figure out the track length using the number of samples and the sample
# rate for more accuracy (to the nearest millisecond).  We need this
# accuracy because there are nearly 100 tracks, and rounding errors add up.

SAMPLES=`soxi -s "$FILENAME"`
RATE=`soxi -r "$FILENAME"`

LENGTH=`echo "$SAMPLES * 1000 / $RATE" | bc -l`

LENGTH=${LENGTH%%.*}

# Chapter position is the running total of the track lengths
((CHAPTERTIME=CHAPTERTIME+LENGTH))

TIMESTR=`printf "%02d:%02d:%02d.%03d\n" $((CHAPTERTIME/3600000)) $((CHAPTERTIME/60000%60)) $((CHAPTERTIME/1000%60)) $((CHAPTERTIME%1000))`

done

echo
echo Converting to m4b...
echo

# The main conversion command uses sox to decode the mp3s piped to faac to encode as aac
nice -n 10 sox *.mp3 -t wav - | nice -n 10 faac - -b 96 -o working.m4b

echo
echo Setting Chapter marks...
echo

# this adds the chapter info to the mp4 file
mp4chaps --import working.m4b
rm  working.chapters.txt

echo
echo Setting tag to Audiobook...
echo

# tag as an audiobook set the file info
# (we set the author as the issue date so it shows on the browsing page)
# Note: some versions of AtomicParsley need the --DeepScan parameter
AtomicParsley working.m4b --artist "$2" --stik Audiobook --overWrite

echo
echo Adding the cover art...
echo

# eyeD3 pulls the jpg from the first file, and mp4art adds it to our mp4 file
eyeD3 -i . "`find .. -name '*.mp3' | head -n 1`"
mp4art --add "`find . -name *.jpg`" working.m4b

echo
echo Optimizing final file...
echo

# optional, this makes sure the file blocks are optimally arranged
mp4file --optimize working.m4b

echo
echo Finished.  Renaming final file...
echo

mv -v working.m4b ../"$1".m4b

}

DIR="`pwd`"
NAME="`echo ${DIR##*/} | cut -d' ' -f1-8`"
DATE=`echo ${DIR##*/} | cut -d' ' -f3-8`

cd renumbered

converttomp4 "$NAME" "$DATE"

cd ..
rm renumbered/*
rmdir renumbered

One Response to “Convert The Economist Audio Edition into an iPod audiobook with chapters”

  1. zany says:

    Thanks for making this public – really helpful for someone who uses apple mp3 players.

    Have you found any way to download the zip file too? I wish there was some script to download the newest edition without logging into the website manually.

Leave a Reply

*
To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
Click to hear an audio file of the anti-spam word