Inserting code comments for the next guy.

This commit is contained in:
upuv 2018-05-20 00:50:48 +10:00
parent f2c78da132
commit ac656f9723

195
AAXtoMP3
View File

@ -41,13 +41,14 @@ fi
# Variable validation # Variable validation
set -o errexit -o noclobber -o nounset -o pipefail set -o errexit -o noclobber -o nounset -o pipefail
# -----
# Detect which annoying version fo grep we have
GREP=$(grep --version | grep -q GNU && echo "grep" || echo "ggrep") GREP=$(grep --version | grep -q GNU && echo "grep" || echo "ggrep")
if ! [[ $(type -P "$GREP") ]]; then if ! [[ $(type -P "$GREP") ]]; then
echo "$GREP (GNU grep) is not in your PATH" echo "$GREP (GNU grep) is not in your PATH"
echo "Without it, this script will break." echo "Without it, this script will break."
echo "On macOS, you may want to try: brew install grep" echo "On macOS, you may want to try: brew install grep"
exit 1 exit 1
fi fi
# ----- # -----
@ -95,124 +96,140 @@ debug_file() {
# log # log
log() { log() {
echo "$(date "+%F %T%z") ${1}" echo "$(date "+%F %T%z") ${1}"
} }
# 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'`
metadata_file="${working_directory}/metadata.txt" metadata_file="${working_directory}/metadata.txt"
# -----
# Inspect the AAX and extract the metadata associated with the file.
save_metadata() { save_metadata() {
local media_file local media_file
media_file="$1" media_file="$1"
ffprobe -i "$media_file" 2> "$metadata_file" ffprobe -i "$media_file" 2> "$metadata_file"
debug "Metadata file $metadata_file" debug "Metadata file $metadata_file"
debug_file "$metadata_file" debug_file "$metadata_file"
} }
# -----
# Reach into the meta data and extract a specific value.
# Note the white space clean up could be well cleaner.
get_metadata_value() { get_metadata_value() {
local key local key
key="$1" 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:]' ' ')" normalize_whitespace "$($GREP --max-count=1 --only-matching "${key} *: .*" "$metadata_file" | cut -d : -f 2- | sed -e 's#/##g;s/ (Unabridged)//' | tr -s '[:blank:]' ' ')"
} }
# -----
# specific varient of get_metadata_value bitrate is important for transcoding.
get_bitrate() { get_bitrate() {
get_metadata_value bitrate | $GREP --only-matching '[0-9]\+' get_metadata_value bitrate | $GREP --only-matching '[0-9]\+'
} }
# -----
# simple function to turn tabs and multiple spaces into a single space.
normalize_whitespace() { normalize_whitespace() {
echo $* echo $*
} }
# ======================================================================== # ========================================================================
# Main Transcode Loop # Main Transcode Loop
for path 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
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
save_metadata "${path}" # -----
genre=$(get_metadata_value genre) # Make sure everything is a variable. Simplifying CMD interpretation
artist=$(get_metadata_value artist) save_metadata "${path}"
title=$(get_metadata_value title | sed 's/'\:'/'-\ '/g' | xargs -0) genre=$(get_metadata_value genre)
if [ ! -z targetdir ] ; then artist=$(get_metadata_value artist)
output_directory="${targetdir}/${genre}/${artist}/${title}" title=$(get_metadata_value title | sed 's/'\:'/'-\ '/g' | xargs -0)
else if [ ! -z targetdir ] ; then
output_directory="$(dirname "${path}")/${genre}/${artist}/${title}" output_directory="${targetdir}/${genre}/${artist}/${title}"
fi else
mkdir -p "${output_directory}" output_directory="$(dirname "${path}")/${genre}/${artist}/${title}"
full_file_path="${output_directory}/${title}.${extension}" fi
bitrate="$(get_bitrate)k" mkdir -p "${output_directory}"
album_artist="$(get_metadata_value album_artist)" full_file_path="${output_directory}/${title}.${extension}"
album="$(get_metadata_value album)" bitrate="$(get_bitrate)k"
album_date="$(get_metadata_value date)" album_artist="$(get_metadata_value album_artist)"
copyright="$(get_metadata_value copyright)" album="$(get_metadata_value album)"
album_date="$(get_metadata_value date)"
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.
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}" )"
# This is the primary transcode. All the heavy lifting is here. # -----
</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}" # This is the main work horse command. This is the primary transcoder.
# This is the primary transcode. All the heavy lifting is here.
</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}"
# If we want multiple file we take the big mp3 and split it by chapter. # -----
if [ "${mode}" == "chaptered" ]; then # OK now spit the file if that's what you want.
# Playlist m3u support # If we want multiple file we take the big mp3 and split it by chapter.
playlist_file="${output_directory}/${title}.m3u" if [ "${mode}" == "chaptered" ]; then
log "Creating PlayList ${title}.m3u" # Playlist m3u support
echo '#EXTM3U' > "${playlist_file}" playlist_file="${output_directory}/${title}.m3u"
log "Creating PlayList ${title}.m3u"
echo '#EXTM3U' > "${playlist_file}"
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}..."
chapternum=1 chapternum=1
while read -r -u9 first _ _ start _ end while read -r -u9 first _ _ start _ end
do do
if [[ "${first}" = "Chapter" ]] if [[ "${first}" = "Chapter" ]]; then
then read -r -u9 _
read -r -u9 _ read -r -u9 _ _ chapter
read -r -u9 _ _ chapter 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}"
# the ID3 tags must only be specified for *.mp3 files, # the ID3 tags must only be specified for *.mp3 files,
# the other container formats come with their own # the other container formats come with their own
# tagging mechanisms. # tagging mechanisms.
id3_version_param="" id3_version_param=""
if test "${extension}" = "mp3"; then if test "${extension}" = "mp3"; then
id3_version_param="-id3v2_version 3" id3_version_param="-id3v2_version 3"
fi fi
# Big Long chapter debug I could probably do this better. # Big Long chapter debug I could probably do this better.
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.
</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}"
duration=$(echo "${end} - ${start%?}" | bc) # OK lets get what need for the next chapter in the Playlist m3u file.
echo "#EXTINF:${duration%.*},${title} - ${chapter}" >> "${playlist_file}" duration=$(echo "${end} - ${start%?}" | bc)
echo "${chapter_title}.${extension}" >> "${playlist_file}" echo "#EXTINF:${duration%.*},${title} - ${chapter}" >> "${playlist_file}"
chapternum=$((chapternum + 1 )) echo "${chapter_title}.${extension}" >> "${playlist_file}"
fi chapternum=$((chapternum + 1 ))
done 9< "$metadata_file" fi
rm "${full_file_path}" done 9< "$metadata_file"
log "Done creating chapters. Chaptered files contained in ${output_directory}." rm "${full_file_path}"
fi log "Done creating chapters. Chaptered files contained in ${output_directory}."
fi
log "Done. ${title}" log "Done. ${title}"
rm "${metadata_file}" rm "${metadata_file}"
done done