From 84335828d9c56739fcf04ef56e3c034f63b037bc Mon Sep 17 00:00:00 2001 From: upuv Date: Sat, 26 May 2018 15:27:39 +1000 Subject: [PATCH] Bug Fix, added --chaptered, Readme updates. --- AAXtoMP3 | 82 ++++++++++++++++++++++++++++++++++--------------------- README.md | 55 +++++++++++++++++++++++-------------- 2 files changed, 85 insertions(+), 52 deletions(-) diff --git a/AAXtoMP3 b/AAXtoMP3 index 67417ae..4a60e9d 100755 --- a/AAXtoMP3 +++ b/AAXtoMP3 @@ -5,14 +5,14 @@ # Command Line Options # Usage Synopsis. -usage=$'\nUsage: AAXtoMP3.sh [--flac] [--aac] [--opus ] [--single] [-e:m4a] [-e:m4b]\n[--authcode ] [--output_dir ] {FILES}\n' +usage=$'\nUsage: AAXtoMP3.sh [--flac] [--aac] [--opus ] [--single] [--chaptered]\n[-e:m4a] [-e:m4b] [--authcode ] [--output_dir ] {FILES}\n' codec=libmp3lame # Default encoder. extension=mp3 # Default encoder extention. mode=chaptered # Multi file output auth_code= # Required to be set via file or option. targetdir= # Optional output location. Note default is basedir of AAX file. DEBUG=0 # Default off, If set extremely verbose output. -container= # Just in case we need to change the container. Used for M4A to M4B +container=mp3 # Just in case we need to change the container. Used for M4A to M4B # ----- # Code tip Do not have any script above this point that calls a function or a binary. If you do @@ -22,17 +22,19 @@ container= # Just in case we need to change the container. Use while true; do case "$1" in # Flac encoding - -f | --flac ) codec=flac; extension=flac; mode=single ; shift ;; + -f | --flac ) codec=flac; extension=flac; mode=single; container=flac; shift ;; # Apple m4a music format. - -a | --aac ) codec=copy; extension=m4a; mode=single ; shift ;; + -a | --aac ) codec=copy; extension=m4a; mode=single; container=m4a; shift ;; # Ogg Format - -o | --opus ) codec=libopus; extension=ogg; shift ;; + -o | --opus ) codec=libopus; extension=ogg; container=flac; shift ;; # If appropriate use only a single file output. -s | --single ) mode=single; shift ;; + # If appropriate use only a single file output. + -c | --chaptered ) mode=chaptered; shift ;; # This is the same as --single option. - -e:mp3 ) codec=libmp3lame; extension=mp3; mode=single; shift ;; + -e:mp3 ) codec=libmp3lame; extension=mp3; mode=single; container=mp3; shift ;; # Identical to --acc option. - -e:m4a ) codec=copy; extension=m4a; mode=single; shift ;; + -e:m4a ) codec=copy; extension=m4a; mode=single; container=m4a shift ;; # Similiar to --aac but specific to audio books. -e:m4b ) codec=copy; extension=m4a; mode=single; container=m4b shift ;; # Change the working dir from AAX directory to what you choose. @@ -104,10 +106,12 @@ 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 +if [[ "x${targetdir}" != "x" ]]; then + if [[ ! -w "${targetdir}" || ! -d "${targetdir}" ]] ; then + echo "ERROR Target Directory is not writable: \"$targetdir\"" + echo "$usage" + exit 1 + fi fi # ======================================================================== @@ -194,12 +198,11 @@ do genre=$(get_metadata_value genre) artist=$(get_metadata_value artist) title=$(get_metadata_value title | sed 's/'\:'/'-'/g' | sed 's/ / /g' | sed 's/- /-/g' | xargs -0) - if [ ! -z targetdir ] ; then + if [ "x${targetdir}" != "x" ] ; 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}" bitrate="$(get_bitrate)k" album_artist="$(get_metadata_value album_artist)" @@ -207,6 +210,8 @@ do album_date="$(get_metadata_value date)" copyright="$(get_metadata_value copyright)" + mkdir -p "${output_directory}" + # Big long DEBUG output. Fully describes the settings used for transcoding. I could probably do this better. # 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. @@ -249,7 +254,7 @@ do # The formating of the chapters names and the file names. # Chapter names are used in a few place. - chapter_title="${title} - $(printf %0${#chaptercount}d $chapternum) ${chapter}" + chapter_title="${title}-$(printf %0${#chaptercount}d $chapternum) ${chapter}" chapter_file="${output_directory}/${chapter_title}.${extension}" @@ -266,7 +271,8 @@ do # Extract chapter by time stamps start and finish of chapter. # This extracts based on time stamps start and end. - > "${playlist_file}" - echo "${chapter_title}.${extension}" >> "${playlist_file}" + echo "${chapter_title}.${container}" >> "${playlist_file}" chapternum=$((chapternum + 1 )) + + # ---- + # Add the cover art to m4a and m4b file types. + if [[ ${extension} == "m4a" && $(type -P mp4art) ]]; then + mp4art -q --add "${cover_path}" "${chapter_file}" + log "Added cover art to ${chapter_title}" + fi + + # ---- + # Detect if we are actuall m4b instead of m4a Then rename the file. + if [[ ${extension} == "m4a" && ${container}="m4b" ]]; then + mv "${chapter_file}" "${chapter_file/.m4a/.m4b}" + fi + fi done 9< "$metadata_file" # Clean up of working directoy stuff. rm "${full_file_path}" - log "Done creating chapters. Chaptered files contained in ${output_directory}." - fi - - # ---- - # Add the cover art to m4a and m4b file types. - if [[ ${extension} == "m4a" && $(type -P mp4art) ]]; then - mp4art --add "${cover_path}" "${full_file_path}" - log "Added cover art." - fi - - # ---- - # Detect if we are actuall m4b instead of m4a Then rename the file. - if [[ ${extension} == "m4a" && ${container}="m4b" ]]; then - mv "${full_file_path}" "${full_file_path/.m4a/.m4b}" + log "Done creating chapters for ${output_directory}." + else + # Perform file tasks on output file. + # ---- + # Add the cover art to m4a and m4b file types. + if [[ ${extension} == "m4a" && $(type -P mp4art) ]]; then + mp4art -q --add "${cover_path}" "${full_file_path}" + log "Added cover art to ${title}.${extension}" + fi + # ---- + # Detect if we are actuall m4b instead of m4a Then rename the file. + if [[ ${extension} == "m4a" && ${container}="m4b" ]]; then + mv "${full_file_path}" "${full_file_path/.m4a/.m4b}" + fi fi - log "Done. ${title}" + log "Done ${title}" # Lastly get rid of any extra stuff. rm "${metadata_file}" done diff --git a/README.md b/README.md index db91440..79f4b74 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,6 @@ your **personal** Audible account. The purpose of this software is to create a method for you to download and store your books just in case Audible fails for some reason. -## Setup -You will need your authentication code that comes from Audible's -servers. This will be used by ffmpeg to perform the initial audio convert. You -can obtain this string from a tool like [audible-activator](https://github.com/inAudible-NG/audible-activator). - ## Requirements * bash 4.3.42 or later tested * ffmpeg version 2.8.3 or later @@ -33,42 +28,65 @@ Thanks to kbabioch, this script has also been packaged in the [AUR](https://aur. ## Usage(s) ``` -bash AAXtoMP3 [-f|--flac] [-o|--opus] [-a|-aac] [-s|--single] [-e:mp3] [-e:m4a] [-e:m4b] [-A|--authcode ] [-t|--target_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 ] [-d|--debug] [-h|--help] ... ``` -* **[AUTHCODE]** **your** Audible auth code (it won't correctly decode otherwise) (required), See below for more information on setting the AUTHCODE. * **<AAX INPUT_FILES>**... are considered input file(s), useful for batching! -## Options ## +## Options * **-f** or **--flac** Flac Encoding and Produces a single file. * **-o** or **--opus** Ogg/Opus Encoding defaults to multiple file output by chapter. The extention is .ogg * **-a** or **--aac** AAC Encoding and produce a m4a single files output. -* **-s** or **--single** Output a single file. If you only want a single ogg file for instance. * **-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. * **-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. +* **-s** or **--single** Output a single file for the entire book. If you only want a single ogg file for instance. +* **-c** or **--chaptered** Output a single file per chapter. The --chaptered will only work if it follows the -aac -e:m4a -em4b options. + + +### [AUTHCODE] +**Your** Audible auth code (it won't correctly decode otherwise) (required). + +#### Determining your own AUTHCODE +You will need your authentication code that comes from Audible's servers. This +will be used by ffmpeg to perform the initial audio convert. You can obtain +this string from a tool like +[audible-activator](https://github.com/inAudible-NG/audible-activator). + +#### Specifying the AUTHCODE. + In order of __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. __~/.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. 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. +* Produces 1 or more mp3 files for the AAX title. +* The default mode is **chaptered** +* If you want a mp3 file per chapter do not use the -single option. +* A m3u playlist file will also be created in this instance in the case of **default** chaptered ouput. ### 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 +* The default mode is **chaptered** * Opus coded files are stored in the ogg container format for better compatibility. ### AAC Encoding * Can be done by using the **-a** or **--aac** command line switches +* The default mode is **single** * 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 +* The default mode is **single** +* 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 ### * These containers were created by Apple Inc. They were meant to be the successor to mp3. @@ -76,20 +94,15 @@ bash AAXtoMP3 [-f|--flac] [-o|--opus] [-a|-aac] [-s|--single] [-e:mp3] [-e:m4a] * M4B is a container that is meant to hold audiobooks and is typically has bitrates of 64k and 32k. * Both formats are chaptered * Both support coverart internall +* The default mode is **single** ### Defaults -* Specifying the AUTHCODE. - In order of __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. __~/.authcode__ a global config file for all the tools. And is used as the default if none of the above are specified. - 4. 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. * 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 -* The default output is by chapter, unless the codec does not support it. +* The default output is by chapter. ## Anti-Piracy Notice -Note that this project does NOT ‘crack’ the DRM. It simply allows the user to +Note that this project **does NOT ‘crack’** the DRM. It simply allows the user to use their own encryption key (fetched from Audible servers) to decrypt the audiobook in the same manner that the official audiobook playing software does.