Add superrip2
[cmccabe-bin] / audible-to-mp3.sh
1 #!/usr/bin/env bash
2
3 #
4 # Convert Audible .aax files to mp3s.
5 #
6
7 die() {
8     echo $@
9     exit 1
10 }
11
12 usage() {
13     cat <<EOF
14 $0: convert Audible .aax files to mp3s.
15
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.
21 EOF
22 }
23
24 debug=0
25 while getopts  "a:dhi:o:" flag; do
26     case $flag in
27     a)  authcode=$OPTARG;;
28     d)  debug=1;;
29     h)  usage; exit 0;;
30     i)  input=$OPTARG;;
31     o)  output=$OPTARG;;
32     *) usage; exit 1;;
33     esac
34 done
35 shift $((OPTIND-1))
36
37 which AAXtoMP3 &> /dev/null || die "Failed to locate AAXtoMP3.  Please install it."
38 which audiobooker &> /dev/null || die "Failed to locate audiobooker.  Please install it."
39 which tagger.py &> /dev/null || die "Failed to locate tagger.py.  Please install it."
40 [[ -v authcode ]] || die "You must specify the authcode."
41 [[ -v input ]] || die "You must specify the input file."
42 [[ -f "${input}" ]] || die "Not a normal file: ${input}"
43 [[ -v output ]] || die "You must specify the output directory."
44 [[ -e "${output}" ]] && die "Output directory already exists: ${output}"
45 mkdir "${output}" || die "Failed to create a directory at ${output}"
46 rmdir "${output}" || die "Failed to rmdir the directory at ${output}"
47
48 tmpdir="`mktemp -d -t audible-to-mp3.XXXXXXXXXX`" || exit 1
49 [[ -v debug ]] || trap "rm -rf ${TMPDIR}; exit" INT TERM EXIT
50 chmod 700 "${tmpdir}"
51 echo "-> mkdir ${tmpdir}"
52
53 cp -f "${input}" "${tmpdir}/input.aax" || die "failed to copy ${input} to ${tmpdir}/input.aax"
54 echo "-> AAXtoMP3 --authcode ${authcode} --single ${tmpdir}/input.aax"
55 AAXtoMP3 --authcode "${authcode}" --single "${tmpdir}/input.aax"
56 [[ $? -eq 0 ]] || die "AAXtoMP3 failed."
57 # For some reason, AAXtoMP3 doesn't give us much control over the output file name.
58 # Let's find out what it is using the "find" command, and change it to something well-behaved.
59 pushd "${tmpdir}" &> /dev/null || die "failed to pushd ${tmpdir}"
60 find -name '*.mp3' -type f -print0 | xargs -0 -I{} mv {} "${tmpdir}/input.mp3"
61 popd &> /dev/null
62 [[ $? -eq 0 ]] || die "Failed to locate and move the AAXtoMP3 output."
63 mkdir -p "${tmpdir}/output" || die "failed to create ${tmpdir}/output directory" 
64 pushd "${tmpdir}/output" &> /dev/null
65 [[ $? -eq 0 ]] || die "Failed to pushd to ${tmpdir}/output"
66 echo "-> echo y | audiobooker ../input.mp3"
67 echo y | audiobooker ../input.mp3
68 [[ $? -eq 0 ]] || die "audiobooker failed."
69 popd &> /dev/null
70 echo "-> mv ${tmpdir}/output ${output}"
71 mv -f "${tmpdir}/output" "${output}" || die "failed to move the output directory to ${output}"
72 echo "-> tagger.py -A "${output}""
73 tagger.py -A "${output}"
74 [[ $? -eq 0 ]] || die "tagger.py failed.  Output remains in ${output}."