From 03f1a58638daf8018bf50dde2795e4a11d0687d0 Mon Sep 17 00:00:00 2001 From: upuv Date: Wed, 6 Jun 2018 00:39:33 +1000 Subject: [PATCH] Verify aax files options Improvements --- AAXtoMP3 | 77 +++++++++++++++++++++++++++++++++++++++++++++++-------- README.md | 15 ++++++++--- 2 files changed, 79 insertions(+), 13 deletions(-) diff --git a/AAXtoMP3 b/AAXtoMP3 index f8a4774..9761f38 100755 --- a/AAXtoMP3 +++ b/AAXtoMP3 @@ -5,7 +5,7 @@ # Command Line Options # Usage Synopsis. -usage=$'\nUsage: AAXtoMP3 [--flac] [--aac] [--opus ] [--single] [--chaptered]\n[-e:m4a] [-e:m4b] [--authcode ] [--output_dir ]\n[--complete_dir ] {FILES}\n' +usage=$'\nUsage: AAXtoMP3 [--flac] [--aac] [--opus ] [--single] [--chaptered]\n[-e:mp3] [-e:m4a] [-e:m4b] [--authcode ]\n[--output_dir ] [--complete_dir ] [--validate]\n{FILES}\n' codec=libmp3lame # Default encoder. extension=mp3 # Default encoder extention. mode=chaptered # Multi file output @@ -13,6 +13,7 @@ auth_code= # Required to be set via file or option. targetdir= # Optional output location. Note default is basedir of AAX file. completedir= # Optional location to move aax files once the decoding is complete. container=mp3 # Just in case we need to change the container. Used for M4A to M4B +VALIDATE=0 # Validate the input aax file(s) only. No Transcoding of files will occur DEBUG=0 # Default off, If set extremely verbose output. # ----- @@ -45,7 +46,9 @@ while true; do # Authorization code associate with the AAX file(s) -A | --authcode ) auth_code="$2"; shift 2 ;; # Extremely verbose output. - -d | --debug ) DEBUG=1; shift ;; + -d | --debug ) DEBUG=1; shift ;; + # Validate ONLY the aax file(s) No transcoding occures + -V | --validate ) VALIDATE=1; shift ;; # Command synopsis. -h | --help ) printf "$usage" $0 ; exit ;; # Standard flag signifying the end of command line processing. @@ -194,11 +197,62 @@ if [[ "x${completedir}" != "x" ]]; then fi # ----- -# Clean up if someone hits ^c +# Clean up if someone hits ^c or the script exits for any reason. trap 'rm -r -f "${working_directory}"' EXIT + +# ----- +# Set up some basic working files ASAP. Note the trap will clean this up no matter what. working_directory=`mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir'` metadata_file="${working_directory}/metadata.txt" +# ----- +# Validate the AAX and extract the metadata associated with the file. +validate_aax() { + local media_file + media_file="$1" + + # Test for existance + if [[ ! -r "${media_file}" ]] ; then + log "ERROR File NOT Found: ${media_file}" + return + else + if [[ "${VALIDATE}" == "1" ]]; then + log "Test 1 SUCCESS: ${media_file}" + fi + fi + + # Clear the errexit value we want to capture the output of the ffprobe below. + set +e errexit + + # Take a look at the aax file and see if it is valid. + output="$(ffprobe -loglevel warning -activation_bytes ${auth_code} -i "${media_file}" 2>&1)" + + # If invalid then say something. + if [[ $? != "0" ]] ; then + # No matter what lets bark that something is wrong. + log "ERROR: Invalid File: ${media_file}" + elif [[ "${VALIDATE}" == "1" ]]; then + # If the validate option is present then lets at least state what is valid. + log "Test 2 SUCCESS: ${media_file}" + fi + + # This is a big test only performed when the --validate swicth is passed. + if [[ "${VALIDATE}" == "1" ]]; then + output="$(ffmpeg -hide_banner -activation_bytes ${auth_code} -i "${media_file}" -vn -f null - 2>&1)" + if [[ $? != "0" ]] ; then + log "ERROR: Invalid File: ${media_file}" + else + log "Test 3 SUCCESS: ${media_file}" + fi + fi + + # Dump the output of the ffprobe command. + debug "$output" + + # Turn it back on. ffprobe is done. + set -e errexit +} + # ----- # Inspect the AAX and extract the metadata associated with the file. save_metadata() { @@ -231,11 +285,14 @@ get_bitrate() { for aax_file do - # Check for Presense of Audiobook. Note this break the processing of - # of a list of books once a single missing file is found. - if [[ ! -r "${aax_file}" ]] ; then - echo "ERROR: Input Audiobook file $aax_file missing" - exit 1 + # Validate the input aax file. Note this happens no matter what. + # It's just that if the validate option is set then we skip to next file. + # If however vlaidate is not set and we proceed with the script any errors will + # case the script to stop. + validate_aax "${aax_file}" + if [[ ${VALIDATE} == "1" ]] ; then + # Don't bother doing anything else with this file. + continue fi # ----- @@ -263,7 +320,6 @@ do log "$(printf '\n----Decoding---%s%s--%s--' "${title}" "${dashline:${#title}}" "${auth_code}")" log "Source ${aax_file}" - # Big long DEBUG output. Fully describes the settings used for transcoding. # Not this is a long debug command. It's not critical to operation. It's purely for people debugging # and coders wanting to extend the script. @@ -272,6 +328,7 @@ do # ----- # This is the main work horse command. This is the primary transcoder. # This is the primary transcode. All the heavy lifting is here. + debug 'ffmpeg -loglevel error -stats -activation_bytes "${auth_code}" -i "${aax_file}" -vn -codec:a "${codec}" -ab ${bitrate} -map_metadata -1 -metadata title="${title}" -metadata artist="${artist}" -metadata album_artist="${album_artist}" -metadata album="${album}" -metadata date="${album_date}" -metadata track="1/1" -metadata genre="${genre}" -metadata copyright="${copyright}" "${output_file}"' ] [-t|--target_dir ] [-C|--complete_dir ] [-d|--debug] [-h|--help] ... +bash AAXtoMP3 [-f|--flac] [-o|--opus] [-a|-aac] [-s|--single] [-c|--chaptered] [-e:mp3] [-e:m4a] [-e:m4b] [-A|--authcode ] [-t|--target_dir ] [-C|--complete_dir ] [-V|--validate] [-d|--debug] [-h|--help] ... ``` * **<AAX INPUT_FILES>**... are considered input file(s), useful for batching! @@ -40,6 +40,7 @@ bash AAXtoMP3 [-f|--flac] [-o|--opus] [-a|-aac] [-s|--single] [-c|--chaptered] [ * **-A** or **--authcode <AUTHCODE>** for this execution of the command use the provided <AUTHCODE> to decode the AAX file. * **-t** or **--target_dir <PATH>** change the default output location to the named <PATH>. Note the default location is ./Audiobook of the directory to which each AAX file resides. * **-C** or **--complete_dir <PATH>** a directory to place aax files after they have been decoded successfully. Note make a back up of your aax files prior to using this option. Just in case something goes wrong. +* **-V** or **--validate** Perform 2 validation tests on the supplied aax files. This is more extensive than the normal validation as we attempt to transcode the aax file to a null file. This can take a long period of time. However it is usefull when inspecting a large set of aax files prior to transcoding. As download errors are common with Audible servers. * **-e:mp3** Identical to defaults. * **-e:m4a** Create a m4a audio file. This is identical to --aac * **-e:m4b** Create a m4b aduio file. This is the book version of the m4a format. @@ -63,7 +64,6 @@ In order of __precidence__. 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 exist. The code must also match the encoding for the user that owns the AAX file(s). If the authcode does not match the AAX file no transcoding will occure. - ### MP3 Encoding * This is the **default** encoding * Produces 1 or more mp3 files for the AAX title. @@ -89,7 +89,7 @@ __Note:__ At least one of the above must be exist. The code must also match the * FLAC is an open format with royalty-free licensing * Note: There is an bug with the ffmpeg software that prevents the splitting of flac files. Chaptered output of flac files will fail. -### M4A and M4B Containers ### +### M4A and M4B Containers * These containers were created by Apple Inc. They were meant to be the successor to mp3. * M4A is a container that is meant to hold music and is typically of a higher bitrate. * M4B is a container that is meant to hold audiobooks and is typically has bitrates of 64k and 32k. @@ -97,6 +97,15 @@ __Note:__ At least one of the above must be exist. The code must also match the * Both support coverart internall * The default mode is **single** +### Validating AAX files +* The **--validate** option will result in only a validation pass over the supplied aax file(s). No transcoding will occure. This is usefull when you wish to ensure you have a proper download of your personal Audible audio books. With this option all supplied books are validated. +* If you do NOT supply the **--validate** option all audio books are still validated when they are processed. However if there is an invalid audio book in the supplied list of books the processing will stop at that point. +* A third test is performed on the file where the entire file is inspected to see if it is valid. This is a lengthy process. However it will not break the script when an invalid file is found. +* The 3 test current are: + 1. aax present + 1. meta data header in file is valid and complete + 1. entire file is valid and complete. _only executed with the **--validate** option._ + ### Defaults * Default out put directory is the base directoy of each file listed. Plus the genre, Artist and Title of the Audio Book. * The default codec is mp3