4 # Convert Audible .aax files to mp3s.
14 $0: convert Audible .aax files to mp3s.
16 -a [authcode] Set the authcode for the Audible file.
17 -d Set debug mode, in which we do not delete temporary files.
18 -h Display this help message.
19 -i [file] The Audible file.
20 -o [path] Set the final directory output path.
24 # On MacOS, use gfind since AAXtoMP3 needs it.
26 which gfind &> /dev/null
27 if [[ $? -eq 0 ]]; then
32 while getopts "a:dhi:o:" flag; do
44 which AAXtoMP3 &> /dev/null || die "Failed to locate AAXtoMP3. Please install it."
45 which audiobooker &> /dev/null || die "Failed to locate audiobooker. Please install it."
46 which tagger.py &> /dev/null || die "Failed to locate tagger.py. Please install it."
47 which mp3splt &> /dev/null || die "Failed to locate mp3splt. Please install it."
48 [[ -v authcode ]] || die "You must specify the authcode."
49 [[ -v input ]] || die "You must specify the input file."
50 [[ -f "${input}" ]] || die "Not a normal file: ${input}"
51 [[ -v output ]] || die "You must specify the output directory."
52 [[ -e "${output}" ]] && die "Output directory already exists: ${output}"
53 mkdir "${output}" || die "Failed to create a directory at ${output}"
54 rmdir "${output}" || die "Failed to rmdir the directory at ${output}"
56 tmpdir="`mktemp -d -t audible-to-mp3.XXXXXXXXXX`" || exit 1
57 [[ -v debug ]] || trap "rm -rf ${TMPDIR}; exit" INT TERM EXIT
59 echo "-> mkdir ${tmpdir}"
61 cp -f "${input}" "${tmpdir}/input.aax" || die "failed to copy ${input} to ${tmpdir}/input.aax"
62 echo "-> AAXtoMP3 --authcode ${authcode} --single ${tmpdir}/input.aax"
63 AAXtoMP3 --authcode "${authcode}" --single "${tmpdir}/input.aax"
64 [[ $? -eq 0 ]] || die "AAXtoMP3 failed."
65 # For some reason, AAXtoMP3 doesn't give us much control over the output file name.
66 # Let's find out what it is using the "find" command, and change it to something well-behaved.
67 pushd "${tmpdir}" &> /dev/null || die "failed to pushd ${tmpdir}"
68 $FIND -name '*.mp3' -type f -print0 | xargs -0 -I{} mv {} "${tmpdir}/input.mp3"
70 [[ $? -eq 0 ]] || die "Failed to locate and move the AAXtoMP3 output."
71 mkdir -p "${tmpdir}/output" || die "failed to create ${tmpdir}/output directory"
72 pushd "${tmpdir}/output" &> /dev/null
73 [[ $? -eq 0 ]] || die "Failed to pushd to ${tmpdir}/output"
74 echo "-> echo y | audiobooker ../input.mp3"
75 echo y | audiobooker ../input.mp3
76 [[ $? -eq 0 ]] || die "audiobooker failed."
78 echo "-> mv ${tmpdir}/output ${output}"
79 mv -f "${tmpdir}/output" "${output}" || die "failed to move the output directory to ${output}"
80 echo "-> tagger.py -A "${output}""
81 tagger.py -A "${output}"
82 [[ $? -eq 0 ]] || die "tagger.py failed. Output remains in ${output}."