From c37c837ee385eb4fd72f1af18b3a8523fe817282 Mon Sep 17 00:00:00 2001 From: Colin P. Mccabe Date: Thu, 21 Nov 2019 14:01:24 -0800 Subject: [PATCH] Add audible-to-mp3.sh --- audible-to-mp3.sh | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 74 insertions(+), 0 deletions(-) create mode 100755 audible-to-mp3.sh diff --git a/audible-to-mp3.sh b/audible-to-mp3.sh new file mode 100755 index 0000000..1b2054d --- /dev/null +++ b/audible-to-mp3.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash + +# +# Convert Audible .aax files to mp3s. +# + +die() { + echo $@ + exit 1 +} + +usage() { + cat < /dev/null || die "Failed to locate AAXtoMP3. Please install it." +which audiobooker &> /dev/null || die "Failed to locate audiobooker. Please install it." +which tagger.py &> /dev/null || die "Failed to locate tagger.py. Please install it." +[[ -v authcode ]] || die "You must specify the authcode." +[[ -v input ]] || die "You must specify the input file." +[[ -f "${input}" ]] || die "Not a normal file: ${input}" +[[ -v output ]] || die "You must specify the output directory." +[[ -e "${output}" ]] && die "Output directory already exists: ${output}" +mkdir "${output}" || die "Failed to create a directory at ${output}" +rmdir "${output}" || die "Failed to rmdir the directory at ${output}" + +tmpdir="`mktemp -d -t audible-to-mp3.XXXXXXXXXX`" || exit 1 +[[ -v debug ]] || trap "rm -rf ${TMPDIR}; exit" INT TERM EXIT +chmod 700 "${tmpdir}" +echo "-> mkdir ${tmpdir}" + +cp -f "${input}" "${tmpdir}/input.aax" || die "failed to copy ${input} to ${tmpdir}/input.aax" +echo "-> AAXtoMP3 --authcode ${authcode} --single ${tmpdir}/input.aax" +AAXtoMP3 --authcode "${authcode}" --single "${tmpdir}/input.aax" +[[ $? -eq 0 ]] || die "AAXtoMP3 failed." +# For some reason, AAXtoMP3 doesn't give us much control over the output file name. +# Let's find out what it is using the "find" command, and change it to something well-behaved. +pushd "${tmpdir}" &> /dev/null || die "failed to pushd ${tmpdir}" +find -name '*.mp3' -type f -print0 | xargs -0 -I{} mv {} "${tmpdir}/input.mp3" +popd &> /dev/null +[[ $? -eq 0 ]] || die "Failed to locate and move the AAXtoMP3 output." +mkdir -p "${tmpdir}/output" || die "failed to create ${tmpdir}/output directory" +pushd "${tmpdir}/output" &> /dev/null +[[ $? -eq 0 ]] || die "Failed to pushd to ${tmpdir}/output" +echo "-> echo y | audiobooker ../input.mp3" +echo y | audiobooker ../input.mp3 +[[ $? -eq 0 ]] || die "audiobooker failed." +popd &> /dev/null +echo "-> mv ${tmpdir}/output ${output}" +mv -f "${tmpdir}/output" "${output}" || die "failed to move the output directory to ${output}" +echo "-> tagger.py -A "${output}"" +tagger.py -A "${output}" +[[ $? -eq 0 ]] || die "tagger.py failed. Output remains in ${output}." -- 1.6.6.rc1.39.g9a42