Bug Fix, added --chaptered, Readme updates.

This commit is contained in:
upuv 2018-05-26 15:27:39 +10:00
parent eaf75eb2e2
commit 84335828d9
2 changed files with 85 additions and 52 deletions

View File

@ -5,14 +5,14 @@
# Command Line Options # Command Line Options
# Usage Synopsis. # Usage Synopsis.
usage=$'\nUsage: AAXtoMP3.sh [--flac] [--aac] [--opus ] [--single] [-e:m4a] [-e:m4b]\n[--authcode <AUTHCODE>] [--output_dir <PATH>] {FILES}\n' usage=$'\nUsage: AAXtoMP3.sh [--flac] [--aac] [--opus ] [--single] [--chaptered]\n[-e:m4a] [-e:m4b] [--authcode <AUTHCODE>] [--output_dir <PATH>] {FILES}\n'
codec=libmp3lame # Default encoder. codec=libmp3lame # Default encoder.
extension=mp3 # Default encoder extention. extension=mp3 # Default encoder extention.
mode=chaptered # Multi file output mode=chaptered # Multi file output
auth_code= # Required to be set via file or option. auth_code= # Required to be set via file or option.
targetdir= # Optional output location. Note default is basedir of AAX file. targetdir= # Optional output location. Note default is basedir of AAX file.
DEBUG=0 # Default off, If set extremely verbose output. 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 # 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 while true; do
case "$1" in case "$1" in
# Flac encoding # 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. # 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 # 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. # If appropriate use only a single file output.
-s | --single ) mode=single; shift ;; -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. # 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. # 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. # Similiar to --aac but specific to audio books.
-e:m4b ) codec=copy; extension=m4a; mode=single; container=m4b shift ;; -e:m4b ) codec=copy; extension=m4a; mode=single; container=m4b shift ;;
# Change the working dir from AAX directory to what you choose. # 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 # Check the target dir for if set if it is writable
if [[ ! -w "${targetdir}" || ! -d "${targetdir}" ]] ; then if [[ "x${targetdir}" != "x" ]]; then
echo "ERROR Target Directory is not writable: \"$targetdir\"" if [[ ! -w "${targetdir}" || ! -d "${targetdir}" ]] ; then
echo "$usage" echo "ERROR Target Directory is not writable: \"$targetdir\""
exit 1 echo "$usage"
exit 1
fi
fi fi
# ======================================================================== # ========================================================================
@ -194,12 +198,11 @@ do
genre=$(get_metadata_value genre) genre=$(get_metadata_value genre)
artist=$(get_metadata_value artist) artist=$(get_metadata_value artist)
title=$(get_metadata_value title | sed 's/'\:'/'-'/g' | sed 's/ / /g' | sed 's/- /-/g' | xargs -0) 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}" output_directory="${targetdir}/${genre}/${artist}/${title}"
else else
output_directory="$(dirname "${path}")/${genre}/${artist}/${title}" output_directory="$(dirname "${path}")/${genre}/${artist}/${title}"
fi fi
mkdir -p "${output_directory}"
full_file_path="${output_directory}/${title}.${extension}" full_file_path="${output_directory}/${title}.${extension}"
bitrate="$(get_bitrate)k" bitrate="$(get_bitrate)k"
album_artist="$(get_metadata_value album_artist)" album_artist="$(get_metadata_value album_artist)"
@ -207,6 +210,8 @@ do
album_date="$(get_metadata_value date)" album_date="$(get_metadata_value date)"
copyright="$(get_metadata_value copyright)" 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. # 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 # 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. # and coders wanting to extend the script.
@ -249,7 +254,7 @@ do
# The formating of the chapters names and the file names. # The formating of the chapters names and the file names.
# Chapter names are used in a few place. # 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}" chapter_file="${output_directory}/${chapter_title}.${extension}"
@ -266,7 +271,8 @@ do
# Extract chapter by time stamps start and finish of chapter. # Extract chapter by time stamps start and finish of chapter.
# This extracts based on time stamps start and end. # This extracts based on time stamps start and end.
</dev/null ffmpeg -loglevel error -stats -i "${full_file_path}" -i "${cover_path}" -ss "${start%?}" -to "${end}" -map 0:0 -map 1:0 -acodec copy ${id3_version_param} \ log "Spliting chapter ${chapternum} start:${start%?}(s) end:${end}(s)"
</dev/null ffmpeg -loglevel quiet -nostats -i "${full_file_path}" -i "${cover_path}" -ss "${start%?}" -to "${end}" -map 0:0 -map 1:0 -acodec copy ${id3_version_param} \
-metadata:s:v title="Album cover" -metadata:s:v comment="Cover (Front)" -metadata track="${chapternum}" -metadata title="${chapter_title}" \ -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (Front)" -metadata track="${chapternum}" -metadata title="${chapter_title}" \
"${chapter_file}" "${chapter_file}"
@ -275,31 +281,45 @@ do
# Playlist creation. # Playlist creation.
duration=$(echo "${end} - ${start%?}" | bc) duration=$(echo "${end} - ${start%?}" | bc)
echo "#EXTINF:${duration%.*},${title} - ${chapter}" >> "${playlist_file}" echo "#EXTINF:${duration%.*},${title} - ${chapter}" >> "${playlist_file}"
echo "${chapter_title}.${extension}" >> "${playlist_file}" echo "${chapter_title}.${container}" >> "${playlist_file}"
chapternum=$((chapternum + 1 )) 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 fi
done 9< "$metadata_file" done 9< "$metadata_file"
# Clean up of working directoy stuff. # Clean up of working directoy stuff.
rm "${full_file_path}" rm "${full_file_path}"
log "Done creating chapters. Chaptered files contained in ${output_directory}." log "Done creating chapters for ${output_directory}."
fi else
# Perform file tasks on output file.
# ---- # ----
# Add the cover art to m4a and m4b file types. # Add the cover art to m4a and m4b file types.
if [[ ${extension} == "m4a" && $(type -P mp4art) ]]; then if [[ ${extension} == "m4a" && $(type -P mp4art) ]]; then
mp4art --add "${cover_path}" "${full_file_path}" mp4art -q --add "${cover_path}" "${full_file_path}"
log "Added cover art." log "Added cover art to ${title}.${extension}"
fi fi
# ----
# ---- # Detect if we are actuall m4b instead of m4a Then rename the file.
# Detect if we are actuall m4b instead of m4a Then rename the file. if [[ ${extension} == "m4a" && ${container}="m4b" ]]; then
if [[ ${extension} == "m4a" && ${container}="m4b" ]]; then mv "${full_file_path}" "${full_file_path/.m4a/.m4b}"
mv "${full_file_path}" "${full_file_path/.m4a/.m4b}" fi
fi fi
log "Done. ${title}" log "Done ${title}"
# Lastly get rid of any extra stuff. # Lastly get rid of any extra stuff.
rm "${metadata_file}" rm "${metadata_file}"
done done

View File

@ -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 create a method for you to download and store your books just in case
Audible fails for some reason. 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 ## Requirements
* bash 4.3.42 or later tested * bash 4.3.42 or later tested
* ffmpeg version 2.8.3 or later * 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) ## Usage(s)
``` ```
bash AAXtoMP3 [-f|--flac] [-o|--opus] [-a|-aac] [-s|--single] [-e:mp3] [-e:m4a] [-e:m4b] [-A|--authcode <AUTHCODE>] [-t|--target_dir <PATH>] [-d|--debug] [-h|--help] <AAX INPUT_FILES>... bash AAXtoMP3 [-f|--flac] [-o|--opus] [-a|-aac] [-s|--single] [-c|--chaptered] [-e:mp3] [-e:m4a] [-e:m4b] [-A|--authcode <AUTHCODE>] [-t|--target_dir <PATH>] [-d|--debug] [-h|--help] <AAX INPUT_FILES>...
``` ```
* **[AUTHCODE]** **your** Audible auth code (it won't correctly decode otherwise) (required), See below for more information on setting the AUTHCODE.
* **&lt;AAX INPUT_FILES&gt;**... are considered input file(s), useful for batching! * **&lt;AAX INPUT_FILES&gt;**... are considered input file(s), useful for batching!
## Options ## ## Options
* **-f** or **--flac** Flac Encoding and Produces a single file. * **-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 * **-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. * **-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 &lt;AUTHCODE&gt;** for this execution of the command use the provided &lt;AUTHCODE&gt; to decode the AAX file. * **-A** or **--authcode &lt;AUTHCODE&gt;** for this execution of the command use the provided &lt;AUTHCODE&gt; to decode the AAX file.
* **-t** or **--target_dir &lt;PATH&gt;** change the default output location to the named &lt;PATH&gt;. Note the default location is ./Audiobook of the directory to which each AAX file resides. * **-t** or **--target_dir &lt;PATH&gt;** change the default output location to the named &lt;PATH&gt;. Note the default location is ./Audiobook of the directory to which each AAX file resides.
* **-e:mp3** Identical to defaults. * **-e:mp3** Identical to defaults.
* **-e:m4a** Create a m4a audio file. This is identical to --aac * **-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. * **-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 ### MP3 Encoding
* This is the **default** 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 * Produces 1 or more mp3 files for the AAX title.
* 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. * 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 ### Ogg/Opus Encoding
* Can be done by using the **-o** or **--opus** command line switches * 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. * Opus coded files are stored in the ogg container format for better compatibility.
### AAC Encoding ### AAC Encoding
* Can be done by using the **-a** or **--aac** command line switches * 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 * Designed to be the successor of the MP3 format
* Generally achieves better sound quality than MP3 at the same bit rate. * Generally achieves better sound quality than MP3 at the same bit rate.
* This will only produce 1 audio file as output. * This will only produce 1 audio file as output.
### FLAC Encoding ### FLAC Encoding
* Can be done by using the **-f** or **--flac** command line switches * Can be done by using the **-f** or **--flac** command line switches
* The default mode is **single**
* FLAC is an open format with royalty-free licensing * 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. * 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. * M4B is a container that is meant to hold audiobooks and is typically has bitrates of 64k and 32k.
* Both formats are chaptered * Both formats are chaptered
* Both support coverart internall * Both support coverart internall
* The default mode is **single**
### Defaults ### 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. * 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 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 ## 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 use their own encryption key (fetched from Audible servers) to decrypt the
audiobook in the same manner that the official audiobook playing software does. audiobook in the same manner that the official audiobook playing software does.