Makefile: add pickrand
[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 # On MacOS, use gfind since AAXtoMP3 needs it.
25 FIND=find
26 which gfind &> /dev/null
27 if [[ $? -eq 0 ]]; then
28     FIND=gfind
29 fi
30
31 debug=0
32 while getopts  "a:dhi:o:" flag; do
33     case $flag in
34     a)  authcode=$OPTARG;;
35     d)  debug=1;;
36     h)  usage; exit 0;;
37     i)  input=$OPTARG;;
38     o)  output=$OPTARG;;
39     *) usage; exit 1;;
40     esac
41 done
42 shift $((OPTIND-1))
43
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}"
55
56 tmpdir="`mktemp -d -t audible-to-mp3.XXXXXXXXXX`" || exit 1
57 [[ -v debug ]] || trap "rm -rf ${TMPDIR}; exit" INT TERM EXIT
58 chmod 700 "${tmpdir}"
59 echo "-> mkdir ${tmpdir}"
60
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"
69 popd &> /dev/null
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."
77 popd &> /dev/null
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}."