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

View File

@ -41,8 +41,9 @@ fi
# Variable validation
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")
if ! [[ $(type -P "$GREP") ]]; then
echo "$GREP (GNU grep) is not in your PATH"
echo "Without it, this script will break."
@ -98,10 +99,13 @@ log() {
echo "$(date "+%F %T%z") ${1}"
}
# Clean up if someone hits ^c
trap 'rm -r -f "${working_directory}"' EXIT
working_directory=`mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir'`
metadata_file="${working_directory}/metadata.txt"
# -----
# Inspect the AAX and extract the metadata associated with the file.
save_metadata() {
local media_file
media_file="$1"
@ -110,16 +114,23 @@ save_metadata() {
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() {
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:]' ' ')"
}
# -----
# specific varient of get_metadata_value bitrate is important for transcoding.
get_bitrate() {
get_metadata_value bitrate | $GREP --only-matching '[0-9]\+'
}
# -----
# simple function to turn tabs and multiple spaces into a single space.
normalize_whitespace() {
echo $*
}
@ -136,6 +147,8 @@ do
exit 1
fi
# -----
# Make sure everything is a variable. Simplifying CMD interpretation
save_metadata "${path}"
genre=$(get_metadata_value genre)
artist=$(get_metadata_value artist)
@ -156,6 +169,8 @@ do
# 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}" )"
# -----
# 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}"
@ -167,6 +182,8 @@ do
log "Extracting cover into ${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.
# If we want multiple file we take the big mp3 and split it by chapter.
if [ "${mode}" == "chaptered" ]; then
# Playlist m3u support
@ -180,8 +197,7 @@ do
chapternum=1
while read -r -u9 first _ _ start _ end
do
if [[ "${first}" = "Chapter" ]]
then
if [[ "${first}" = "Chapter" ]]; then
read -r -u9 _
read -r -u9 _ _ chapter
chapter_title="${title} - $(printf %0${#chaptercount}d $chapternum) ${chapter}"
@ -203,6 +219,7 @@ do
-metadata:s:v title="Album cover" -metadata:s:v comment="Cover (Front)" -metadata track="${chapternum}" -metadata title="${chapter_title}" \
"${chapter_file}"
# OK lets get what need for the next chapter in the Playlist m3u file.
duration=$(echo "${end} - ${start%?}" | bc)
echo "#EXTINF:${duration%.*},${title} - ${chapter}" >> "${playlist_file}"
echo "${chapter_title}.${extension}" >> "${playlist_file}"