Skip to content

Instantly share code, notes, and snippets.

@JoshStark
Last active June 18, 2021 06:28
Show Gist options
  • Save JoshStark/3ae19e44f4daf344ce68fba5758ee5b4 to your computer and use it in GitHub Desktop.
Save JoshStark/3ae19e44f4daf344ce68fba5758ee5b4 to your computer and use it in GitHub Desktop.
A script which combines audible-cli and update_chapter_titles.py to create a 1-1 m4b file from an aax file.
#!/bin/bash
# This script has a few pre-requisites:
#
# 1. The audible-cli must be installed and available in the $PATH.
# https://github.com/mkb79/audible-cli
# e.g. export PATH=$PATH:/home/$(whoami)/.local/bin
#
# 2. The update_chapter_titles.py must also be present in the same
# directory as this file. This also relies on python3 being in the $PATH
# https://github.com/mkb79/audible-cli/blob/master/utils/update_chapter_titles.py
#
# 3. The $activation_bytes variable must be set with your own Audible account activation
# bytes. You can get this using the audible-cli when logged in.
#
# 4. asins.txt should be a single-line space-delimited text file containing the ASINS of any
# audiobooks you wish to download and configure chapters. These MUST be audiobooks in
# your own collection.
#
# DESCRIPTION:
# This utilises both mkb79's audible-cli and mkb79's update_chapter_titles.py scripts
# in order to convert an Audible .AAX file into a DRM-free .m4b file. By default,
# converting just the .AAX file will result in unnamed chapters, so this script
# will also grab the raw Audible chapters file and will convert it into a ffmpeg-friendly
# metadata format, which is injected in during the conversion step.
#
# USAGE:
# > ./convert_audible.sh <ACTIVATION_BYTES> <PATH_TO_asins.txt>
#
# NOTE:
# Some audiobooks advertise a different number of chapters compared to what the
# raw chapters JSON file contains. This will be flagged by mkb79's script. It is
# generally safe to hit 'y' when this occurs. Most of the time this is due to the
# Audible intro/outros being included in the chapter metadata.
#
# Example:
# Missmatch between chapter numbers.
# Do you want to continue? [y/N]: y
#
#
output_dir=../
activation_bytes=$1
asins_file=$2
asins=$(cat $asins_file)
for asin in $asins; do
mkdir -p current_job
pushd current_job/
echo "Grabbing audiobook..."
audible download -a $asin --aax
audiobook=$(ls *.aax)
file_base="${audiobook%.*}"
file_proper_fullname="$file_base.m4b"
echo "Grabbing chapter metadata for $file_base"
audible download -a $asin --chapter
proper_chapter_meta=$(ls *.json)
echo "Convert from $audiobook to $file_proper_fullname using $proper_chapter_meta"
echo "Extracting current metadata from audiobook..."
ffmpeg -i $audiobook -f ffmetadata old_meta
echo "Extraction done..."
echo "Converting old metadata (you may need to manually enter Y)"
python3 ../update_chapter_titles.py --separate-branding -f old_meta -a $proper_chapter_meta -o new_meta
echo "Metadata done..."
echo "Ripping and converting $audiobook to m4b..."
ffmpeg -activation_bytes $activation_bytes \
-i $audiobook \
-i new_meta \
-map_metadata 0 \
-map_chapters 1 \
-c copy \
"$output_dir/$file_proper_fullname"
popd
rm -r current_job/
done
@JKamsker
Copy link

You can use this to obtain the activation bytes and even convert smaller sized audiobooks (upto 1.8GB): Audible Tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment