itayperl/aa2mp3

Use ffmpeg if avconv is not available.

michaelcadilhac opened this issue · 4 comments

Arguably, ffmpeg should be encouraged over avconv, as avconv has been abandoned by its historical support, Debian. In any case, changing avconv to ffmpeg in aa2mp3.sh does the job, but the script could easily check for one or the other, and issue a message if none are found. (Similarly, issue #1 should be pointed out to the user: if dump.exe fails, a friendly message could ask "Have you logged in in the Audible app? (Either by Authenticating or by going on the Podcast page)" —I had to go to the Podcast just as the user of #1).

Thanks for this. A patch/pull request would be highly appreciated!

Sure! This is mostly cosmetic though :-) I also went through the Makefile, as the name of the compiler seems to change from distro to distro. Thanks for your work!

diff --git a/Makefile b/Makefile
index c3da5de..a272f66 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,18 @@
-DLL_PATH := AAXSDKWin.dll
+DLL := AAXSDKWin.dll

-CC := i586-mingw32msvc-gcc
+POSSIBLE_CCS := i586-mingw32msvc-gcc i686-w64-mingw32-gcc
+
+MINGWCC := $(firstword $(foreach cc,$(POSSIBLE_CCS), \
+            $(shell which $(cc) 2> /dev/null)))
+
+ifndef MINGWCC
+  $(error "No mingw32 compiler found")
+endif

 all: dump.exe

-dump.exe: dump.c
-   $(CC) -o $@ dump.c $(DLL_PATH)
+$(DLL):
+   $(error "The DLL $(DLL) from the Audible app should be provided.")
+
+dump.exe: dump.c $(DLL)
+   $(MINGWCC) -o $@ dump.c $(DLL)
diff --git a/aa2mp3.sh b/aa2mp3.sh
index f2c86eb..31e970c 100755
--- a/aa2mp3.sh
+++ b/aa2mp3.sh
@@ -2,19 +2,52 @@

 set -e

-AVCONV_ARGS="-ab 96k"
+QUALITY=96

-if [ -z "$2" ]; then
-    echo Usage: $0 in.aa out.mp3
-    exit
-fi
-
-if [ -f "$2" ]; then
-    echo "Output file exists."
+fail () {
+    for e in "$@"; do echo "(E) $e" >&2; done
     exit 1
-fi
+}
+
+usage () {
+    cat <<EOF
+Usage: $0 [-q quality-in-kbps] in.aa out.mp3
+Prerequisites: 
+ - ffmpeg or avconv
+ - Being logged in in the Audible app, either by 'Activate'
+   or via the 'Podcast' section.
+EOF
+    exit 0
+}
+
+while getopts "hq:" opt; do
+    case $opt in
+   "h") usage;;
+   "q") expr "$OPTARG" : '[0-9]\+' > /dev/null || \
+          fail "Invalid number $OPTARG";
+        QUALITY=$OPTARG;;
+   "?") fail "Invalid option -$opt";;
+    esac
+done
+
+
+AVCONV_ARGS="-ab $QUALITY"
+
+shift $((OPTIND - 1))
+
+[ $# -eq 2 ] || usage
+[ -f "$1" ] || fail "Input file \"$1\" does not exist."
+[ -f "$2" ] && fail "Output file \"$2\" exists."
+[ -f "dump.exe" ] || fail "dump.exe not found at the root."
+
+FFMPEG=$(which ffmpeg 2> /dev/null || which avconv 2> /dev/null) || \
+    fail "Neither ffmpeg(1) nor avconv(1) were found." \
+    "Please install one of them---both are not needed."
+
+echo -n "Identifying the input file...  "
+PARAMS=$(wine dump.exe -i "$1") || \
+    fail "Identification of \"$1\" failed.  Are you logged in in the Audible app," \
+    "either by 'Activate' or via the 'Podcast' section?"

-PARAMS=$(wine dump.exe -i $1)
-if [ $? -eq 0 ]; then
-    wine dump.exe $1 | avconv $PARAMS -i - $AVCONV_ARGS $2
-fi
+echo "Launching conversion (wine dump.exe $1 | $FFMPEG $PARAMS -i - $AVCONV_ARGS $2)"
+wine dump.exe "$1" | $FFMPEG $PARAMS -i - $AVCONV_ARGS "$2"

Please review carefully :-)
Cheers!

The line:

+AVCONV_ARGS="-ab $QUALITY"

should read:

+AVCONV_ARGS="-ab ${QUALITY}k"

Committed, thanks! :D