Forkcasting
As clear as a puddle of mud

Audiobooks, again

I don't like how much time I spend extracting and converting them.

In my previous article on audiobooks I showed a short script that converts *.mp3 audiobook files into much smaller *.ogg files. At the end of that article I mentioned that I have a very ugly process for handling whole zip archives of audiobooks.

Today I show another terrible shell script! You guessed it, it fixes some of the problems of my first script.

#!/bin/sh

# Safer shell scripting. -x also helps debugging
set -eufx

INPUT_ZIP="${1}"
TRACK_LIST=$(mktemp)

# Find out what's in the zip file
zipinfo -1 "${INPUT_ZIP}" > "${TRACK_LIST}"

# Cout the number of items in the zip file
TRACK_COUNT=$(wc -l "${TRACK_LIST}" | tr -s ' ' \
  | cut -f 1 -d ' ')

# Assume 4 CPUs.
printf "%d \n" $(( "${TRACK_COUNT}" / 4 ))
TRACKS_PER_CPU=$(( "${TRACK_COUNT}" / 4))

# Batch the tracks into 4 groups, one per CPU
split -l "${TRACKS_PER_CPU}" "${TRACK_LIST}"

# My old shell script is just called "convert"
CONVERT="/home/turner/Audio Books/convert"

# Start 4 background processes converting the files.
cat xaa | xargs -n1 -d '\n' "${CONVERT}" "${INPUT_ZIP}"  &
cat xab | xargs -n1 -d '\n' "${CONVERT}" "${INPUT_ZIP}"  &
cat xac | xargs -n1 -d '\n' "${CONVERT}" "${INPUT_ZIP}"  &
cat xad | xargs -n1 -d '\n' "${CONVERT}" "${INPUT_ZIP}"  &

This just wrangles 4 processes for extracting and converting the audiobook files. Not much else from my previous wishlist: No automatic downloaded, no cleaning up zip files, etc. It even leaves the results of split hanging around.

But hey, I can set it and forget it!

Oh, and this doesn't work if the zip file has only 1 entry. You get a weird error. That's pretty rare, so I'm happy to ignore it for a bit.

So yea, future wish-list:

  1. Automatic downloading
  2. Automatic directory hierarchy management
  3. Automatic archiving the zip
  4. Automatically sending the files to Google Drive so I can pick them up on my phone
  5. Cleaning up after itself

That's Future Dan's problem!