Skip to content

Instantly share code, notes, and snippets.

@NatGrayYes
Last active August 3, 2026 12:39
Show Gist options
  • Select an option

  • Save NatGrayYes/85566ef29119a71a41284e0497e57d38 to your computer and use it in GitHub Desktop.

Select an option

Save NatGrayYes/85566ef29119a71a41284e0497e57d38 to your computer and use it in GitHub Desktop.
Convert a folder of aax audiobooks into m4b files
#!/bin/bash
trap 'exit 130' INT
# Fill in default activation bytes value to save using the argument every time
# Reference: https://audible-tools.github.io/
ACTIVATION_BYTES=""
usage() {
echo "Usage: $(basename "$0") [-a activation_bytes]"
echo ""
echo "Converts all .aax files in the current directory to .m4b,"
echo "and saves them in a subfolder called ./conv."
echo "Output files are renamed and organised in subfolders: /artist/album/artist-title.m4b"
echo ""
echo "Options:"
echo " -a Audible activation bytes (default: $ACTIVATION_BYTES)"
echo " -h Show this help message"
}
for arg in "$@"; do [[ "$arg" == "--help" ]] && usage && exit 0; done
while getopts "a:h" opt; do
case $opt in
a) ACTIVATION_BYTES="$OPTARG" ;;
h) usage; exit 0 ;;
esac
done
mkdir -p conv
for i in *.aax; do
author=$(ffprobe -activation_bytes "$ACTIVATION_BYTES" -v quiet -show_entries format_tags=artist \
-of default=noprint_wrappers=1:nokey=1 "$i" \
| tr -d '/:*?"<>|\\' | sed 's/ */ /g;s/^ //;s/ $//')
album=$(ffprobe -activation_bytes "$ACTIVATION_BYTES" -v quiet -show_entries format_tags=album \
-of default=noprint_wrappers=1:nokey=1 "$i" \
| tr -d '/:*?"<>|\\' | sed 's/ */ /g;s/^ //;s/ $//')
title=$(ffprobe -activation_bytes "$ACTIVATION_BYTES" -v quiet -show_entries format_tags=title \
-of default=noprint_wrappers=1:nokey=1 "$i" \
| tr -d '/:*?"<>|\\' | sed 's/ */ /g;s/^ //;s/ $//')
outdir="conv/${author}/${album}"
mkdir -p "$outdir"
echo "${author} - ${title}"
ffmpeg -activation_bytes "$ACTIVATION_BYTES" -i "$i" \
-map 0:a -map 0:v -codec copy -disposition:v attached_pic \
"${outdir}/${author}-${title}.m4b"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment