Merge pull request #171 from fabh2o/aaxc

AAXC support, audible-cli integration
This commit is contained in:
KrumpetPirate 2021-09-03 10:08:27 -04:00 committed by GitHub
commit 596108d56e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 192 additions and 29 deletions

2
.gitignore vendored
View File

@ -1,4 +1,6 @@
ACTIVATION
.authcode
*aax
*jpg
*json
Audiobook/*

169
AAXtoMP3
View File

@ -9,7 +9,7 @@ usage=$'\nUsage: AAXtoMP3 [--flac] [--aac] [--opus ] [--single] [--level <COMPRE
[--chaptered] [-e:mp3] [-e:m4a] [-e:m4b] [--authcode <AUTHCODE>] [--no-clobber]
[--target_dir <PATH>] [--complete_dir <PATH>] [--validate] [--loglevel <LOGLEVEL>]
[--keep-author <N>] [--author <AUTHOR>] [--{dir,file,chapter}-naming-scheme <STRING>]
[--continue <CHAPTERNUMBER>] {FILES}\n'
[--use-audible-cli-data] [--continue <CHAPTERNUMBER>] {FILES}\n'
codec=libmp3lame # Default encoder.
extension=mp3 # Default encoder extension.
level=-1 # Compression level. Can be given for mp3, flac and opus. -1 = default/not specified.
@ -31,6 +31,9 @@ continue=0 # Default off, If set Transcoding is skipped and cha
continueAt=1 # Optional chapter to continue splitting the chapters.
keepArtist=-1 # Default off, if set change author metadata to use the passed argument as field
authorOverride= # Override the author, ignoring the metadata
audibleCli=0 # Default off, Use additional data gathered from mkb79/audible-cli
aaxc_key= # Initialize variables, in case we need them in debug_vars
aaxc_iv= # Initialize variables, in case we need them in debug_vars
# -----
# Code tip Do not have any script above this point that calls a function or a binary. If you do
@ -77,6 +80,8 @@ while true; do
-V | --validate ) VALIDATE=1; shift ;;
# continue splitting chapters at chapter continueAt
--continue ) continueAt="$2"; continue=1; shift 2 ;;
# Use additional data got with mkb79/audible-cli
--use-audible-cli-data ) audibleCli=1; shift ;;
# Compression level
--level ) level="$2"; shift 2 ;;
# Keep author number n
@ -192,7 +197,7 @@ progressbar() {
echo -ne "Chapter splitting: |$progressbar| $print_percentage% ($part/$total chapters)\r"
}
# Print out what we have already after command line processing.
debug_vars "Command line options as set" codec extension mode container targetdir completedir auth_code keepArtist authorOverride
debug_vars "Command line options as set" codec extension mode container targetdir completedir auth_code keepArtist authorOverride audibleCli
# ========================================================================
# Variable validation
@ -290,12 +295,6 @@ if [ -z $auth_code ]; then
auth_code=`head -1 ~/.authcode`
fi
fi
# No point going on if no authcode found.
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
@ -384,8 +383,8 @@ validate_aax() {
# 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)"
# Take a look at the aax file and see if it is valid. If the source file is aaxc, we give ffprobe additional flags
output="$(ffprobe -loglevel warning ${decrypt_param} -i "${media_file}" 2>&1)"
# If invalid then say something.
if [[ $? != "0" ]] ; then
@ -398,7 +397,7 @@ validate_aax() {
# This is a big test only performed when the --validate switch is passed.
if [[ "${VALIDATE}" == "1" ]]; then
output="$(ffmpeg -hide_banner -activation_bytes ${auth_code} -i "${media_file}" -vn -f null - 2>&1)"
output="$(ffmpeg -hide_banner ${decrypt_param} -i "${media_file}" -vn -f null - 2>&1)"
if [[ $? != "0" ]] ; then
log "ERROR: Invalid File: ${media_file}"
else
@ -413,6 +412,56 @@ validate_aax() {
set -e errexit
}
validate_extra_files() {
local extra_media_file extra_find_command
extra_media_file="$1"
# Bash trick to delete, non greedy, from the end up until the first '-'
extra_title="${extra_media_file%-*}"
# Using this is not ideal, because if the naming scheme is changed then
# this part of the script will break
# AAX file: BookTitle-LC_128_44100_stereo.aax
# Cover file: BookTitle_(1215).jpg
# Chapter file: BookTitle-chapters.json
# Chapter
extra_chapter_file="${extra_title}-chapters.json"
# Cover
extra_dirname="$(dirname "${extra_media_file}")"
extra_find_command='find "${extra_dirname}" -maxdepth 1 -regex ".*/${extra_title##*/}_([0-9]+)\.jpg"'
# We want the output of the find command, we will turn errexit on later
set +e errexit
extra_cover_file="$(eval ${extra_find_command})"
extra_eval_comm="$(eval echo ${extra_find_command})"
set -e errexit
if [[ "${aaxc}" == "1" ]]; then
# bash trick to get file w\o extention (delete from end to the first '.')
extra_voucher="${extra_media_file%.*}.voucher"
if [[ ! -r "${extra_voucher}" ]] ; then
log "ERROR File NOT Found: ${extra_voucher}"
return 1
fi
aaxc_key=$(jq -r '.content_license.license_response.key' "${extra_voucher}")
aaxc_iv=$(jq -r '.content_license.license_response.iv' "${extra_voucher}")
fi
debug_vars "Audible-cli variables" extra_media_file extra_title extra_chapter_file extra_cover_file extra_find_command extra_eval_comm extra_dirname extra_voucher aaxc_key aaxc_iv
# Test for chapter file existence
if [[ ! -r "${extra_chapter_file}" ]] ; then
log "ERROR File NOT Found: ${extra_chapter_file}"
return 1
fi
if [[ "x${extra_cover_file}" == "x" ]] ; then
log "ERROR Cover File NOT Found"
return 1
fi
debug "All expected audible-cli related file are here"
}
# -----
# Inspect the AAX and extract the metadata associated with the file.
save_metadata() {
@ -432,6 +481,18 @@ save_metadata() {
echo "pub :" "$(mediainfo --Inform="General;%pub%" "$media_file")" >> "$metadata_file"
echo "Mediainfo data END" >> "$metadata_file"
fi
if [[ "${audibleCli}" == "1" ]]; then
# If we use data we got with audible-cli, we delete conflicting chapter infos
$SED -i '/^ Chapter #/d' "${metadata_file}"
# Some magic: we parse the .json generated by audible-cli.
# to get the output structure like the one generated by ffprobe,
# we use some characters (#) as placeholder, add some new lines,
# put a ',' after the start value, we calculate the end of each chapter
# as start+length, and we convert (divide) the time stamps from ms to s.
# Then we delete all ':' since they make a filename invalid.
jq -r '.content_metadata.chapter_info.chapters[] | "Chapter # start: \(.start_offset_ms/1000), end: \((.start_offset_ms+.length_ms)/1000) \n#\n# Title: \(.title)"' "${extra_chapter_file}" \
| tr -d ':' >> "$metadata_file"
fi
debug "Metadata file $metadata_file"
debug_file "$metadata_file"
}
@ -453,15 +514,59 @@ get_bitrate() {
get_metadata_value bitrate | $GREP --only-matching '[0-9]\+'
}
# Save the original value, since in the for loop we overwrite
# $audibleCli in case the file is aaxc. If the file is the
# old aax, reset the variable to be the one passed by the user
originalAudibleCliVar=$audibleCli
# ========================================================================
# Main Transcode Loop
for aax_file
do
# If the file is in aaxc format, set the proper variables
if [[ ${aax_file##*.} == "aaxc" ]]; then
# File is the new .aaxc
aaxc=1
audibleCli=1
else
# File is the old .aax
aaxc=0
# If some previous file in the loop are aaxc, the $audibleCli variable has been overwritten, so we reset it to the original one
audibleCli=$originalAudibleCliVar
fi
debug_vars "Variables set based on file extention" aaxc originalAudibleCliVar audibleCli
# No point going on if no authcode found and the file is aax.
# If we use aaxc as input, we do not need it
# if the string $auth_code is null and the format is not aaxc; quit. We need the authcode
if [ -z $auth_code ] && [ "${aaxc}" = "0" ]; then
echo "ERROR Missing authcode, can't decode $aax_file"
echo "$usage"
exit 1
fi
# 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 validate is not set and we proceed with the script any errors will
# case the script to stop.
# If the input file is aaxc, we need to first get the audible_key and audible_iv
# We get them in the function validate_extra_files
if [[ ${audibleCli} == "1" ]] ; then
# If we have additional files (obtained via audible-cli), be sure that they
# exists and they are in the correct location.
validate_extra_files "${aax_file}"
fi
# Set the needed params to decrypt the file. Needed in all command that require ffprobe or ffmpeg
# After validate_extra_files, since the -audible_key and -audible_iv are read in that function
if [[ ${aaxc} == "1" ]] ; then
decrypt_param="-audible_key ${aaxc_key} -audible_iv ${aaxc_iv}"
else
decrypt_param="-activation_bytes ${auth_code}"
fi
validate_aax "${aax_file}"
if [[ ${VALIDATE} == "1" ]] ; then
# Don't bother doing anything else with this file.
@ -488,8 +593,8 @@ do
album_artist="$(get_metadata_value album_artist)"
fi
fi
title=$(get_metadata_value title | $SED 's/'\:'/'-'/g' | $SED 's/- /-/g' | xargs -0)
title=${title:0:100}
title=$(get_metadata_value title)
title=${title:0:128}
bitrate="$(get_bitrate)k"
album="$(get_metadata_value album)"
album_date="$(get_metadata_value date)"
@ -546,11 +651,12 @@ do
# Big long DEBUG output. Fully describes the settings used for transcoding.
# Note 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_vars "Book and Variable values" title auth_code mode aax_file container codec bitrate artist album_artist album album_date genre copyright narrator description publisher currentDirNameScheme output_directory currentFileNameScheme output_file metadata_file working_directory
debug_vars "Book and Variable values" title auth_code aaxc aaxc_key aaxc_iv mode aax_file container codec bitrate artist album_artist album album_date genre copyright narrator description publisher currentDirNameScheme output_directory currentFileNameScheme output_file metadata_file working_directory
# Display the total length of the audiobook in format hh:mm:ss
# 10#$var force base-10 interpretation. By default it's base-8, so values like 08 or 09 are not octal numbers
total_length="$(ffprobe -v error -activation_bytes "${auth_code}" -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${aax_file}" | cut -d . -f 1)"
total_length="$(ffprobe -v error ${decrypt_param} -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${aax_file}" | cut -d . -f 1)"
hours="$((total_length/3600))"
if [ "$((hours<10))" = "1" ]; then hours="0$hours"; fi
minutes="$((total_length/60-60*10#$hours))"
@ -569,10 +675,10 @@ do
if [ "${continue}" == "0" ]; then
# 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}"'
debug 'ffmpeg -loglevel error -stats ${decrypt_param} -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}"'
</dev/null ffmpeg -loglevel error \
-stats \
-activation_bytes "${auth_code}" \
${decrypt_param} \
-i "${aax_file}" \
-vn \
-codec:a "${codec}" \
@ -599,11 +705,27 @@ do
fi
# Grab the cover art if available.
cover_file="${output_directory}/cover.jpg"
extra_crop_cover=''
if [ "${continue}" == "0" ]; then
if [ "$((${loglevel} > 1))" == "1" ]; then
log "Extracting cover into ${cover_file}..."
if [ "${audibleCli}" == "1" ]; then
# We have a better quality cover file, copy it.
if [ "$((${loglevel} > 1))" == "1" ]; then
log "Copy cover file to ${cover_file}..."
fi
cp "${extra_cover_file}" "${cover_file}"
# We now set a variable, ${extra_crop_cover}, which contains an additional
# ffmpeg flag. It crops the cover so the width and the height is divisible by two.
# Since the standard (in the aax file) image resolution is 512, we set the flag
# only if we use a custom cover art.
extra_crop_cover='-vf crop=trunc(iw/2)*2:trunc(ih/2)*2'
else
# Audible-cli not used, extract the cover from the aax file
if [ "$((${loglevel} > 1))" == "1" ]; then
log "Extracting cover into ${cover_file}..."
fi
</dev/null ffmpeg -loglevel error -activation_bytes "${auth_code}" -i "${aax_file}" -an -codec:v copy "${cover_file}"
fi
</dev/null ffmpeg -loglevel error -activation_bytes "${auth_code}" -i "${aax_file}" -an -codec:v copy "${cover_file}"
fi
# -----
@ -690,7 +812,7 @@ do
fi
# Big Long chapter debug
debug_vars "Chapter Variables:" cover_file chapter_start chapter_end chapternum chapterNameScheme chapter_title chapter_file
debug_vars "Chapter Variables:" cover_file chapter_start chapter_end chapternum chapter chapterNameScheme chapter_title chapter_file
if [ "$((${continueAt} > ${chapternum}))" = "0" ]; then
# Extract chapter by time stamps start and finish of chapter.
# This extracts based on time stamps start and end.
@ -702,6 +824,7 @@ do
${split_input} \
-i "${output_file}" \
-i "${cover_file}" \
${extra_crop_cover} \
${split_output} \
-map 0:0 \
-map 1:0 \
@ -709,8 +832,8 @@ do
-metadata:s:v title="Album cover" \
-metadata:s:v comment="Cover (Front)" \
-metadata track="${chapternum}" \
-metadata title="${chapter_title}" \
-metadata:s:a title="${chapter_title}" \
-metadata title="${chapter}" \
-metadata:s:a title="${chapter}" \
-metadata:s:a track="${chapternum}" \
-map_chapters -1 \
-f ${container} \

View File

@ -1,5 +1,5 @@
# AAXtoMP3
The purpose of this software is to convert AAX files to common MP3, M4A, M4B, flac and ogg formats
The purpose of this software is to convert AAX (or AAXC) files to common MP3, M4A, M4B, flac and ogg formats
through a basic bash script frontend to FFMPEG.
Audible uses this file format to maintain DRM restrictions on their audio
@ -14,10 +14,11 @@ Audible fails for some reason.
## Requirements
* bash 4.3.42 or later tested
* ffmpeg version 2.8.3 or later
* ffmpeg version 2.8.3 or later (4.4 or later if the input file is `.aaxc`)
* libmp3lame (came from lame package on Arch, not sure where else this is stored)
* grep Some OS distributions do not have it installed.
* sed Some OS versions will need to install gnu sed.
* jq Command-line JSON processor
* mp4art used to add cover art to m4a and m4b files. Optional
* mediainfo used to add additional media tags like narrator. Optional
@ -43,7 +44,7 @@ bash interactiveAAXtoMP3 [-a|--advanced] [-h|--help]
* **-f** or **--flac** Flac Encoding and as default produces a single file.
* **-o** or **--opus** Ogg/Opus Encoding defaults to multiple file output by chapter. The extension is .ogg
* **-a** or **--aac** AAC Encoding and produce a m4a single files output.
* **-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. Not needed if the source file is .aaxc.
* **-n** or **--no-clobber** If set and the target directory already exists, AAXtoMP3 will exit without overwriting anything.
* **-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.
* **-C** or **--complete_dir &lt;PATH&gt;** 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.
@ -61,6 +62,7 @@ bash interactiveAAXtoMP3 [-a|--advanced] [-h|--help]
* **--dir-naming-scheme &lt;STRING&gt;** or **-D** Use a custom directory naming scheme, with variables. See [below](#custom-naming-scheme) for more info.
* **--file-naming-scheme &lt;STRING&gt;** or **-F** Use a custom file naming scheme, with variables. See [below](#custom-naming-scheme) for more info.
* **--chapter-naming-scheme &lt;STRING&gt;** Use a custom chapter naming scheme, with variables. See [below](#custom-naming-scheme) for more info.
* **--use-audible-cli-data** Use additional data got with mkb79/audible-cli. See [below](#audible-cli-integration) for more infos. Needed for the files in the `aaxc` format.
## Options for interactiveAAXtoMP3
* **-a** or **--advanced** Get more options to choose. Not used right now.
@ -69,20 +71,20 @@ This script presents you the options you chose last time as default.
When you get asked for the aax-file you may just drag'n'drop it to the terminal.
### [AUTHCODE]
**Your** Audible auth code (it won't correctly decode otherwise) (required).
**Your** Audible auth code (it won't correctly decode otherwise) (not required to decode the `aaxc` format).
#### 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).
[audible-activator](https://github.com/inAudible-NG/audible-activator) or like [audible-cli](https://github.com/mkb79/audible-cli).
#### Specifying the AUTHCODE.
In order of __precidence__.
1. __--authcode [AUTHCODE]__ The command line option. With the highest precedence.
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 occur.
__Note:__ At least one of the above must be exist if converting `aax` files. 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 occur.
### MP3 Encoding
* This is the **default** encoding
@ -156,6 +158,7 @@ So you can use `--dir-naming-scheme '$(date +%Y)/$artist'`, but using `--file-na
* If you don't want to have the books separated by author, use `--dir-naming-scheme '$genre/$title'`
### Installing Dependencies.
In general, take a look at [command-not-found.com](https://command-not-found.com/)
#### FFMPEG,FFPROBE
__Ubuntu, Linux Mint, Debian__
```
@ -233,7 +236,42 @@ __MacOS__
```
brew install mediainfo
```
## AAXC files
The AAXC format is a new Audible encryption format, meant to replace the old AAX.
The encryption has been updated, and now to decrypt the file the authcode
is not sufficient, we need two "keys" which are unique for each audiobook.
Since getting those keys is not simple, for now the method used to get them
is handled by the package audible-cli, that stores
them in a file when downloading the aaxc file. This means that in order to
decrypt the aaxc files, they must be downloaded with audible-cli.
## Audible-cli integration
Some information are not present in the AAX file. For example the chapters's
title, additional chapters division (Opening and End credits, Copyright and
more). Those information are avaiable via a non-public audible API. This
[repo](https://github.com/mkb79/Audible) provides a python API wrapper, and the
[audible-cli](https://github.com/mkb79/audible-cli) packege makes easy to get
more info. In particular the flags **--cover --cover-size 1215 --chapter**
downloads a better-quality cover (.jpg) and detailed chapter infos (.json).
More info are avaiable on the package page.
Some books might not be avaiable in the old `aax` format, but only in the newer
`aaxc` format. In that case, you can use [audible-cli](https://github.com/mkb79/audible-cli)
to download them. For example, to download all the books in your library in the newer `aaxc` format, as well as
chapters's title and an HQ cover: `audible download --all --aaxc --cover --cover-size 1215 --chapter`.
To make AAXtoMP3 use the additional data, specify the **--use-audible-cli-data**
flag: it expects the cover and the chapter files (and the voucher, if converting
an aaxc file) to be in the same location of the AAX file. The naming of these
files must be the one set by audible-cli. When converting aaxc files, the variable
is automatically set, so be sure to follow the instructions in this paragraph.
For more information on how to use the `audible-cli` package, check out the git page [audible-cli](https://github.com/mkb79/audible-cli).
Please note that right now audible-cli is in dev stage, so keep in mind that the
naming scheme of the additional files, the flags syntax and other things can
change without warning.
## Anti-Piracy Notice
Note that this project **does NOT crack** the DRM. It simply allows the user to