Added in support for m4b, Removed old m4b script.

This commit is contained in:
upuv 2018-05-23 23:18:23 +10:00
parent 6f343c9191
commit 67d4d05b52
3 changed files with 84 additions and 141 deletions

112
AAXtoM4B
View File

@ -1,112 +0,0 @@
#!/usr/bin/env bash
set -o errexit -o noclobber -o nounset -o pipefail
codec=copy
extension=m4a
mode=chaptered
GREP=$(grep --version | grep -q GNU && echo "grep" || echo "ggrep")
if ! [[ $(type -P "$GREP") ]]; then
echo "$GREP (GNU grep) is not in your PATH"
echo "Without it, this script will break."
echo "On macOS, you may want to try: brew install grep"
exit 1
fi
if [ "$#" -eq 0 ]; then
echo "Usage: bash AAXtoM4B [--single] AUTHCODE {FILES}"
exit 1
fi
if [[ "$1" == '--single' ]]
then
mode=single
shift
fi
if [ ! -f .authcode ]; then
auth_code=$1
shift
else
auth_code=`head -1 .authcode`
fi
debug() {
echo "$(date "+%F %T%z") ${1}"
}
trap 'rm -r -f "${working_directory}"' EXIT
working_directory=`mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir'`
metadata_file="${working_directory}/metadata.txt"
save_metadata() {
local media_file
media_file="$1"
ffprobe -i "$media_file" 2> "$metadata_file"
}
get_metadata_value() {
local key
key="$1"
normalize_whitespace "$($GREP --max-count=1 --only-matching "${key} *: .*" "$metadata_file" | cut -d : -f 2- | sed -e 's#/##g;s/ (Unabridged)//' | tr -s '[:blank:]' ' ')"
}
get_bitrate() {
get_metadata_value bitrate | $GREP --only-matching '[0-9]\+'
}
normalize_whitespace() {
echo $*
}
for path
do
debug "Decoding ${path} with auth code ${auth_code}..."
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}"
mkdir -p "${output_directory}"
full_file_path="${output_directory}/${title}.${extension}"
</dev/null ffmpeg -loglevel error -stats -activation_bytes "${auth_code}" -i "${path}" -vn -codec:a "${codec}" -ab "$(get_bitrate)k" -map_metadata -1 -metadata title="${title}" -metadata artist="${artist}" -metadata album_artist="$(get_metadata_value album_artist)" -metadata album="$(get_metadata_value album)" -metadata date="$(get_metadata_value date)" -metadata track="1/1" -metadata genre="${genre}" -metadata copyright="$(get_metadata_value copyright)" "${full_file_path}"
debug "Created ${full_file_path}."
cover_path="${output_directory}/cover.jpg"
debug "Extracting cover into ${cover_path}..."
</dev/null ffmpeg -loglevel error -activation_bytes "${auth_code}" -i "${path}" -an -codec:v copy "${cover_path}"
if [ "${mode}" == "chaptered" ]; then
chaptercount=$($GREP -Pc "Chapter.*start.*end" $metadata_file)
debug "Extracting ${chaptercount} chapter files from ${full_file_path}..."
chapternum=1
while read -r -u9 first _ _ start _ end
do
if [[ "${first}" = "Chapter" ]]
then
read -r -u9 _
read -r -u9 _ _ chapter
chapter_title="${title} - $(printf %0${#chaptercount}d $chapternum) ${chapter}"
chapter_file="${output_directory}/${chapter_title}.${extension}"
</dev/null ffmpeg -loglevel error -stats -i "${full_file_path}" -i "${cover_path}" -ss "${start%?}" -to "${end}" -vn -codec:a copy -map 0:0 -map 1:0 -id3v2_version 3 \
-metadata:s:v title="Album cover" -metadata:s:v comment="Cover (Front)" -metadata track="${chapternum}" -metadata title="${chapter_title}" \
"${chapter_file}"
mv "${chapter_file}" "${output_directory}/${chapter_title}.m4b"
chapternum=$((chapternum + 1 ))
fi
done 9< "$metadata_file"
rm "${full_file_path}"
debug "Done creating chapters. Chaptered files contained in ${output_directory}."
fi
debug "Adding Artwork to files"
mp4art --add "${cover_path}" "${output_directory}"/*.m4b
debug "Done."
rm "${metadata_file}"
done

View File

@ -3,13 +3,16 @@
# ======================================================================== # ========================================================================
# Command Line Options # Command Line Options
usage=$'\nUsage: AAXtoMP3.sh [--flac] [--aac] [--opus ] [--single] [--authcode <AUTHCODE>]\n[--output_dir <PATH>] {FILES}\n'
codec=libmp3lame # Usage Synopsis.
extension=mp3 usage=$'\nUsage: AAXtoMP3.sh [--flac] [--aac] [--opus ] [--single] [-e:m4a] [-e:m4b]\n[--authcode <AUTHCODE>] [--output_dir <PATH>] {FILES}\n'
mode=chaptered codec=libmp3lame # Default encoder.
auth_code= extension=mp3 # Default encoder extention.
targetdir= mode=chaptered # Multi file output
DEBUG=0 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
# ----- # -----
# 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
@ -18,20 +21,37 @@ DEBUG=0
# Process the command line options. This allows for un-ordered options. Sorta like a getops style # Process the command line options. This allows for un-ordered options. Sorta like a getops style
while true; do while true; do
case "$1" in case "$1" in
# Flac encoding
-f | --flac ) codec=flac; extension=flac; mode=single ; shift ;; -f | --flac ) codec=flac; extension=flac; mode=single ; shift ;;
# Apple m4a music format.
-a | --aac ) codec=copy; extension=m4a; mode=single ; shift ;; -a | --aac ) codec=copy; extension=m4a; mode=single ; shift ;;
# Ogg Format
-o | --opus ) codec=libopus; extension=ogg; shift ;; -o | --opus ) codec=libopus; extension=ogg; shift ;;
# If appropriate use only a single file output.
-s | --single ) mode=single; shift ;; -s | --single ) mode=single; shift ;;
# This is the same as --single option.
-e:mp3 ) codec=libmp3lame; extension=mp3; mode=single; shift ;;
# Identical to --acc option.
-e:m4a ) codec=copy; extension=m4a; mode=single; 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.
-t | --target_dir ) targetdir="$2"; shift 2 ;; -t | --target_dir ) targetdir="$2"; shift 2 ;;
# Authorization code associate with the AAX file(s)
-A | --authcode ) auth_code="$2"; shift 2 ;; -A | --authcode ) auth_code="$2"; shift 2 ;;
# Extremely verbose output.
-d | --debug ) DEBUG=1; shift ;; -d | --debug ) DEBUG=1; shift ;;
# Command synopsis.
-h | --help ) printf "$usage" $0 ; exit ;; -h | --help ) printf "$usage" $0 ; exit ;;
# Standard flag signifying the end of command line processing.
-- ) shift; break ;; -- ) shift; break ;;
# Anything else stops command line processing.
* ) break ;; * ) break ;;
esac esac
done done
# -----
# Empty argv means we have nothing to do so lets bark some help. # Empty argv means we have nothing to do so lets bark some help.
if [ "$#" -eq 0 ]; then if [ "$#" -eq 0 ]; then
printf "$usage" $0 printf "$usage" $0
@ -54,6 +74,8 @@ fi
# ----- # -----
# Obtain the authcode from either the command line, local directory or home directory. # Obtain the authcode from either the command line, local directory or home directory.
# See Readme.md for details on how to aquire your personal authcode for your personal
# audible AAX files.
if [ -z $auth_code ]; then if [ -z $auth_code ]; then
if [ -r .authcode ]; then if [ -r .authcode ]; then
auth_code=`head -1 .authcode` auth_code=`head -1 .authcode`
@ -61,6 +83,7 @@ if [ -z $auth_code ]; then
auth_code=`head -1 ~/.authcode` auth_code=`head -1 ~/.authcode`
fi fi
fi fi
# No point going on if no authcode found.
if [ -z $auth_code ]; then if [ -z $auth_code ]; then
echo "ERROR Missing authcode" echo "ERROR Missing authcode"
echo "$usage" echo "$usage"
@ -78,14 +101,15 @@ fi
# ======================================================================== # ========================================================================
# Utility Functions # Utility Functions
# -----
# debug # debug
debug() { debug() {
if [ $DEBUG == 1 ] ; then if [ $DEBUG == 1 ] ; then
echo "$(date "+%F %T%z") DEBUG ${1}" echo "$(date "+%F %T%z") DEBUG ${1}"
fi fi
} }
# -----
# debug dump contents of a file to STDOUT # debug dump contents of a file to STDOUT
debug_file() { debug_file() {
if [ $DEBUG == 1 ] ; then if [ $DEBUG == 1 ] ; then
@ -96,11 +120,13 @@ debug_file() {
fi fi
} }
# -----
# log # log
log() { log() {
echo "$(date "+%F %T%z") ${1}" echo "$(date "+%F %T%z") ${1}"
} }
# -----
# Clean up if someone hits ^c # Clean up if someone hits ^c
trap 'rm -r -f "${working_directory}"' EXIT trap 'rm -r -f "${working_directory}"' EXIT
working_directory=`mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir'` working_directory=`mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir'`
@ -143,18 +169,19 @@ for path
do do
log "Decoding ${path} with auth code ${auth_code}..." log "Decoding ${path} with auth code ${auth_code}..."
# Check for Presense of Audiobook # 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 "${path}" ]] ; then if [[ ! -r "${path}" ]] ; then
echo "ERROR: Input Audiobook file $path missing" echo "ERROR: Input Audiobook file $path missing"
exit 1 exit 1
fi fi
# ----- # -----
# Make sure everything is a variable. Simplifying CMD interpretation # Make sure everything is a variable. Simplifying Command interpretation
save_metadata "${path}" save_metadata "${path}"
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' | xargs -0) title=$(get_metadata_value title | sed 's/'\:'/'-'/g' | sed 's/ / /g' | sed 's/- /-/g' | xargs -0)
if [ ! -z targetdir ] ; then if [ ! -z targetdir ] ; then
output_directory="${targetdir}/${genre}/${artist}/${title}" output_directory="${targetdir}/${genre}/${artist}/${title}"
else else
@ -169,6 +196,8 @@ do
copyright="$(get_metadata_value copyright)" copyright="$(get_metadata_value copyright)"
# 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
# and coders wanting to extend the script.
debug "$(printf '\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %sn%-18s: %s' title "${title}" auth_code "${auth_code}" mode "${mode}" path "${path}" codec "${codec}" bitrate "${bitrate}" artist "${artist}" album_artist "${album_artist}" album "${album}" album_date "${album_date}" genre "${genre}" copyright "${copyright}" full_file_path "${full_file_path}" metadata_file "${metadata_file}" working_directory "${working_directory}" )" debug "$(printf '\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %sn%-18s: %s' title "${title}" auth_code "${auth_code}" mode "${mode}" path "${path}" codec "${codec}" bitrate "${bitrate}" artist "${artist}" album_artist "${album_artist}" album "${album}" album_date "${album_date}" genre "${genre}" copyright "${copyright}" full_file_path "${full_file_path}" metadata_file "${metadata_file}" working_directory "${working_directory}" )"
# ----- # -----
@ -177,22 +206,25 @@ do
</dev/null ffmpeg -loglevel error -stats -activation_bytes "${auth_code}" -i "${path}" -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}" "${full_file_path}" </dev/null ffmpeg -loglevel error -stats -activation_bytes "${auth_code}" -i "${path}" -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}" "${full_file_path}"
log "Created ${full_file_path}." log "Created ${full_file_path}."
# -----
# Grab the cover art if available. # Grab the cover art if available.
cover_path="${output_directory}/cover.jpg" cover_path="${output_directory}/cover.jpg"
log "Extracting cover into ${cover_path}..." log "Extracting cover into ${cover_path}..."
</dev/null ffmpeg -loglevel error -activation_bytes "${auth_code}" -i "${path}" -an -codec:v copy "${cover_path}" </dev/null ffmpeg -loglevel error -activation_bytes "${auth_code}" -i "${path}" -an -codec:v copy "${cover_path}"
# ----- # -----
# OK now spit the file if that's what you want. # OK now spit the file if that's what you want.
# If we want multiple file we take the big mp3 and split it by chapter. # If we want multiple file we take the big mp3 and split it by chapter.
# Not all audio encodings make sense with multiple chapter outputs. See options section
# for more detail
if [ "${mode}" == "chaptered" ]; then if [ "${mode}" == "chaptered" ]; then
# Playlist m3u support # Playlist m3u support
playlist_file="${output_directory}/${title}.m3u" playlist_file="${output_directory}/${title}.m3u"
log "Creating PlayList ${title}.m3u" log "Creating PlayList ${title}.m3u"
echo '#EXTM3U' > "${playlist_file}" echo '#EXTM3U' > "${playlist_file}"
# Determine the number of chapters.
chaptercount=$($GREP -Pc "Chapter.*start.*end" $metadata_file) chaptercount=$($GREP -Pc "Chapter.*start.*end" $metadata_file)
log "Extracting ${chaptercount} chapter files from ${full_file_path}..." log "Extracting ${chaptercount} chapter files from ${full_file_path}..."
@ -202,6 +234,9 @@ do
if [[ "${first}" = "Chapter" ]]; then if [[ "${first}" = "Chapter" ]]; then
read -r -u9 _ read -r -u9 _
read -r -u9 _ _ chapter read -r -u9 _ _ chapter
# 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}" chapter_file="${output_directory}/${chapter_title}.${extension}"
@ -218,21 +253,33 @@ do
debug "$(printf '\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s' cover_path "${cover_path}" start "${start%?}" end "${end}" id3_version_param "${id3_version_param}" chapternum "${chapternum}" chapter_title "${chapter_title}" chapter_file "${chapter_file}" )" debug "$(printf '\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s\n%-18s: %s' cover_path "${cover_path}" start "${start%?}" end "${end}" id3_version_param "${id3_version_param}" chapternum "${chapternum}" chapter_title "${chapter_title}" chapter_file "${chapter_file}" )"
# 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.
</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} \ </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} \
-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}"
# -----
# OK lets get what need for the next chapter in the Playlist m3u file. # OK lets get what need for the next chapter in the Playlist m3u file.
# 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}.${extension}" >> "${playlist_file}"
chapternum=$((chapternum + 1 )) chapternum=$((chapternum + 1 ))
fi fi
done 9< "$metadata_file" done 9< "$metadata_file"
# 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. Chaptered files contained in ${output_directory}."
fi fi
# Detect if we are actuall m4b instead of m4a
if [[ ${extension} == "m4a" && ${container}="m4b" ]]; then
mv "${full_file_path}" "${full_file_path/.m4a/.m4b}"
fi
log "Done. ${title}" log "Done. ${title}"
# Lastly get rid of any extra stuff.
rm "${metadata_file}" rm "${metadata_file}"
done done

View File

@ -31,13 +31,24 @@ 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] [-A|--authcode <AUTHCODE>] [-o|--output_dir <PATH>] [-d|--debug] [-h|--help] <AAX INPUT_FILES>... 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 AAXtoM4B [AUTHCODE] <AAX INPUT_FILES>... bash AAXtoM4B [AUTHCODE] <AAX INPUT_FILES>...
``` ```
* **[AUTHCODE]** **your** Audible auth code (it won't correctly decode otherwise) (required), See below for more information on setting the AUTHCODE. * **[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 ##
* -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.
### 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 desire a single file use the **--single** option
@ -58,9 +69,6 @@ bash AAXtoM4B [AUTHCODE] <AAX INPUT_FILES>...
* Can be done by using the **-f** or **--flac** command line switches * Can be done by using the **-f** or **--flac** command line switches
* FLAC is an open format with royalty-free licensing * FLAC is an open format with royalty-free licensing
### M4B Encoding
### Defaults ### Defaults
* Specifying the AUTHCODE. * Specifying the AUTHCODE.
In order of __precidence__. In order of __precidence__.