From f976940798fd8bef2ca995438c35d1ca4118f433 Mon Sep 17 00:00:00 2001 From: upuv Date: Sat, 19 May 2018 15:08:57 +1000 Subject: [PATCH 1/5] getopts update --- AAXtoMP3 | 100 +++++++++++++++++++++++++++++++++++++++--------------- README.md | 6 ++-- 2 files changed, 76 insertions(+), 30 deletions(-) diff --git a/AAXtoMP3 b/AAXtoMP3 index 293de3f..143a114 100755 --- a/AAXtoMP3 +++ b/AAXtoMP3 @@ -1,10 +1,38 @@ #!/usr/bin/env bash -set -o errexit -o noclobber -o nounset -o pipefail - +# ======================================================================== +# Command Line Options +usage=$'\nUsage: AAXtoMP3.sh [--flac] [--aac] [--opus ] [--single] [--authcode ]\n[--output_dir ] {FILES}\n' codec=libmp3lame extension=mp3 mode=chaptered +auth_code= +targetdir= + +while true; do + case "$1" in + -f | --flac ) codec=flac; extension=flac; shift ;; + -a | --aac ) codec=copy; extension=m4a; mode=single ;shift ;; + -o | --opus ) codec=libopus; extension=ogg; shift ;; + -s | --single ) mode=single; shift ;; + -t | --target_dir ) targetdir="$2"; shift 2 ;; + -A | --authcode ) auth_code="$2"; shift 2 ;; + -d | --debug ) DEBUG=1; shift ;; # Not so secret flag for debug output. to use in code [ $DEBUG == 1 ] && sample=2 + -h | --help ) printf "$usage" $0 ; exit ;; + -- ) shift; break ;; + * ) break ;; + esac +done + +if [ "$#" -eq 0 ]; then + printf "$usage" $0 + exit 1 +fi + +# ======================================================================== +# Variable validation +set -o errexit -o noclobber -o nounset -o pipefail + GREP=$(grep --version | grep -q GNU && echo "grep" || echo "ggrep") if ! [[ $(type -P "$GREP") ]]; then @@ -14,30 +42,29 @@ if ! [[ $(type -P "$GREP") ]]; then exit 1 fi -if [ "$#" -eq 0 ]; then - echo "Usage: bash AAXtoMP3.sh [--flac] [--single] AUTHCODE {FILES}" - exit 1 -fi - -if [[ "$1" = '--flac' ]] -then - codec=flac - extension=flac - shift -fi - -if [[ "$1" == '--single' ]] -then - mode=single - shift -fi - -if [ ! -f .authcode ]; then - auth_code=$1 - shift -else +# Obtain the authcode from either the command line, local directory or home directory. +if [ -z $auth_code ]; then + if [ -r .authcode ]; then auth_code=`head -1 .authcode` + elif [ -r ~/.authcode ]; then + auth_code=`head -1 ~/.authcode` + fi fi +if [ -z $auth_code ]; then + echo "ERROR Missing authcode" + echo "$usage" + exit 1 +fi + +# Check the target dir for if set if it is writable +if [[ ! -w "${targetdir}" || ! -d "${targetdir}" ]] ; then + echo "ERROR Target Directory is not writable: \"$targetdir\"" + echo "$usage" + exit 1 +fi + +# ======================================================================== +# Utility Functions debug() { echo "$(date "+%F %T%z") ${1}" @@ -67,6 +94,8 @@ normalize_whitespace() { echo $* } +# ======================================================================== +# Main Transcode Loop for path do debug "Decoding ${path} with auth code ${auth_code}..." @@ -74,21 +103,29 @@ do save_metadata "${path}" genre=$(get_metadata_value genre) artist=$(get_metadata_value artist) - title=$(get_metadata_value title) - output_directory="$(dirname "${path}")/${genre}/${artist}/${title}" + title=$(get_metadata_value title | sed 's/'\:'/'-\ '/g' | xargs -0) + if [ ! -z targetdir ] ; then + output_directory="${targetdir}/${genre}/${artist}/${title}" + else + output_directory="$(dirname "${path}")/${genre}/${artist}/${title}" + fi mkdir -p "${output_directory}" full_file_path="${output_directory}/${title}.${extension}" + # This is the primary transcode. All the heavy lifting is here. "${playlist_file}" @@ -105,7 +142,16 @@ do read -r -u9 _ _ chapter chapter_title="${title} - $(printf %0${#chaptercount}d $chapternum) ${chapter}" chapter_file="${output_directory}/${chapter_title}.${extension}" - ... +bash AAXtoMP3 [-f|--flac] [-s|--single] [-a|--authcode ] [-o|--output_dir ] ... bash AAXtoM4A [AUTHCODE] ... bash AAXtoM4B [AUTHCODE] ... ``` @@ -52,9 +52,9 @@ bash AAXtoM4B [AUTHCODE] ... ### Defaults * Specifying the AUTHCODE. In order of __precidence__. - 1. __[AUTHCODE]__ The command line option. With the highest precidence. + 1. __--authcode [AUTHCODE]__ The command line option. With the highest precidence. 2. __.authcode__ If this file is placed in the current working directory and contains only the authcode it is used if the above is not. - 3. __~/.aaxto_config__ a global config file for all the tools. And is used as the default if none of the above are specified. + 3. __~/.authcode__ a global config file for all the tools. And is used as the default if none of the above are specified. Note: At least one of the above must be specified. The code must also match the encoding for the user that owns the AAX file(s). ## Anti-Piracy Notice From 10ca1e53e3f0458a6444341a9124dbb9ed01d30d Mon Sep 17 00:00:00 2001 From: upuv Date: Sat, 19 May 2018 15:41:23 +1000 Subject: [PATCH 2/5] some cleanup --- AAXtoMP3 | 4 ++++ README.md | 21 +++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/AAXtoMP3 b/AAXtoMP3 index 143a114..7dd767b 100755 --- a/AAXtoMP3 +++ b/AAXtoMP3 @@ -1,5 +1,6 @@ #!/usr/bin/env bash + # ======================================================================== # Command Line Options usage=$'\nUsage: AAXtoMP3.sh [--flac] [--aac] [--opus ] [--single] [--authcode ]\n[--output_dir ] {FILES}\n' @@ -100,6 +101,9 @@ for path do debug "Decoding ${path} with auth code ${auth_code}..." + # Check for Presense of Audiobook + [ ! -r "${path}" ] && echo "ERROR: Input file $path missing" ; exit 1 + save_metadata "${path}" genre=$(get_metadata_value genre) artist=$(get_metadata_value artist) diff --git a/README.md b/README.md index f52ad6c..eab30e6 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ The purpose of this software is to convert AAX files to common MP3, M4A and M4B formats through a basic bash script frontend to FFMPEG. -Audible uses this file format, AAX to maintain DRM restrictions on their audio +Audible uses this file format to maintain DRM restrictions on their audio books and if you download your book through your library it will be stored in this format. @@ -31,7 +31,7 @@ Thanks to kbabioch, this script has also been packaged in the [AUR](https://aur. ## Usage(s) ``` -bash AAXtoMP3 [-f|--flac] [-s|--single] [-a|--authcode ] [-o|--output_dir ] ... +bash AAXtoMP3 [-f|--flac] [-o|--opus] [-a|-aac] [-s|--single] [-A|--authcode ] [-o|--output_dir ] ... bash AAXtoM4A [AUTHCODE] ... bash AAXtoM4B [AUTHCODE] ... ``` @@ -41,11 +41,24 @@ bash AAXtoM4B [AUTHCODE] ... ### MP3 Encoding +* This is the default encoding * Produces 1 or more mp3 files for the AAX title. If you desire a single file use the **--single** option * If you want a mp3 file per chapter do not use the -single option. Note a m3u playlist file will also be created in this instance. -* If you desire flac encoding. use the **--flac** option. It's a bit faster but also a bit less compatible. -### M4A Encoding +### Ogg/Opus Encoding +* Can be done by using the -o or --opus command line switches +* Is designed to efficiently code speech and general audio in a single format +* Opus coded files are stored in the ogg container format for better compatibilitt. + +### AAC Encoding +* Can be done by using the -a or --aac command line switches +* Designed to be the successor of the MP3 format +* Generally achieves better sound quality than MP3 at the same bit rate. +* This will only produce 1 audio file as output. + +### FLAC Encoding +* Can be done by using the -f or --flac command line switches +* FLAC is an open format with royalty-free licensing ### M4B Encoding From 41a2803e181fc197ca27dcfe2a952db36a9b01bd Mon Sep 17 00:00:00 2001 From: upuv Date: Sat, 19 May 2018 15:48:08 +1000 Subject: [PATCH 3/5] some cleanup --- AAXtoMP3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AAXtoMP3 b/AAXtoMP3 index 7dd767b..bbb85d7 100755 --- a/AAXtoMP3 +++ b/AAXtoMP3 @@ -102,7 +102,7 @@ do debug "Decoding ${path} with auth code ${auth_code}..." # Check for Presense of Audiobook - [ ! -r "${path}" ] && echo "ERROR: Input file $path missing" ; exit 1 + # [[ ! -r "${path}" ]] && echo "ERROR: Input file $path missing" ; exit 1 save_metadata "${path}" genre=$(get_metadata_value genre) From dee0d7e9a0590e3cb9953bc9b8d0eb74a94f50d9 Mon Sep 17 00:00:00 2001 From: upuv Date: Sat, 19 May 2018 15:51:40 +1000 Subject: [PATCH 4/5] some cleanup --- AAXtoMP3 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/AAXtoMP3 b/AAXtoMP3 index bbb85d7..862bb1c 100755 --- a/AAXtoMP3 +++ b/AAXtoMP3 @@ -102,7 +102,10 @@ do debug "Decoding ${path} with auth code ${auth_code}..." # Check for Presense of Audiobook - # [[ ! -r "${path}" ]] && echo "ERROR: Input file $path missing" ; exit 1 + if [[ ! -r "${path}" ]] ; then + echo "ERROR: Input Audiobook file $path missing" + exit 1 + fi save_metadata "${path}" genre=$(get_metadata_value genre) From ccfaf070210c53753941e697b7c32a4ea3a6f951 Mon Sep 17 00:00:00 2001 From: upuv Date: Sat, 19 May 2018 15:52:38 +1000 Subject: [PATCH 5/5] some cleanup --- AAXtoMP3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AAXtoMP3 b/AAXtoMP3 index 862bb1c..6fa2421 100755 --- a/AAXtoMP3 +++ b/AAXtoMP3 @@ -12,7 +12,7 @@ targetdir= while true; do case "$1" in - -f | --flac ) codec=flac; extension=flac; shift ;; + -f | --flac ) codec=flac; extension=flac; mode=single ; shift ;; -a | --aac ) codec=copy; extension=m4a; mode=single ;shift ;; -o | --opus ) codec=libopus; extension=ogg; shift ;; -s | --single ) mode=single; shift ;;