mirror of
https://github.com/KrumpetPirate/AAXtoMP3.git
synced 2024-11-18 03:08:57 +01:00
175 lines
6.3 KiB
Bash
Executable File
175 lines
6.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
|
|
# ========================================================================
|
|
# Command Line Options
|
|
usage=$'\nUsage: AAXtoMP3.sh [--flac] [--aac] [--opus ] [--single] [--authcode <AUTHCODE>]\n[--output_dir <PATH>] {FILES}\n'
|
|
codec=libmp3lame
|
|
extension=mp3
|
|
mode=chaptered
|
|
auth_code=
|
|
targetdir=
|
|
|
|
while true; do
|
|
case "$1" in
|
|
-f | --flac ) codec=flac; extension=flac; shift ;;
|
|
-a | --aac ) codec=copy; extension=m4a; mode=single ;shift ;;
|
|
-o | --opus ) codec=libopus; extension=ogg; shift ;;
|
|
-s | --single ) mode=single; shift ;;
|
|
-t | --target_dir ) targetdir="$2"; shift 2 ;;
|
|
-A | --authcode ) auth_code="$2"; shift 2 ;;
|
|
-d | --debug ) DEBUG=1; shift ;; # Not so secret flag for debug output. to use in code [ $DEBUG == 1 ] && sample=2
|
|
-h | --help ) printf "$usage" $0 ; exit ;;
|
|
-- ) shift; break ;;
|
|
* ) break ;;
|
|
esac
|
|
done
|
|
|
|
if [ "$#" -eq 0 ]; then
|
|
printf "$usage" $0
|
|
exit 1
|
|
fi
|
|
|
|
# ========================================================================
|
|
# Variable validation
|
|
set -o errexit -o noclobber -o nounset -o pipefail
|
|
|
|
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
|
|
|
|
# Obtain the authcode from either the command line, local directory or home directory.
|
|
if [ -z $auth_code ]; then
|
|
if [ -r .authcode ]; then
|
|
auth_code=`head -1 .authcode`
|
|
elif [ -r ~/.authcode ]; then
|
|
auth_code=`head -1 ~/.authcode`
|
|
fi
|
|
fi
|
|
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
|
|
if [[ ! -w "${targetdir}" || ! -d "${targetdir}" ]] ; then
|
|
echo "ERROR Target Directory is not writable: \"$targetdir\""
|
|
echo "$usage"
|
|
exit 1
|
|
fi
|
|
|
|
# ========================================================================
|
|
# Utility Functions
|
|
|
|
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 $*
|
|
}
|
|
|
|
# ========================================================================
|
|
# Main Transcode Loop
|
|
for path
|
|
do
|
|
debug "Decoding ${path} with auth code ${auth_code}..."
|
|
|
|
# Check for Presense of Audiobook
|
|
[ ! -r "${path}" ] && echo "ERROR: Input file $path missing" ; exit 1
|
|
|
|
save_metadata "${path}"
|
|
genre=$(get_metadata_value genre)
|
|
artist=$(get_metadata_value artist)
|
|
title=$(get_metadata_value title | sed 's/'\:'/'-\ '/g' | xargs -0)
|
|
if [ ! -z targetdir ] ; then
|
|
output_directory="${targetdir}/${genre}/${artist}/${title}"
|
|
else
|
|
output_directory="$(dirname "${path}")/${genre}/${artist}/${title}"
|
|
fi
|
|
mkdir -p "${output_directory}"
|
|
full_file_path="${output_directory}/${title}.${extension}"
|
|
|
|
# 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 "$(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}."
|
|
|
|
# Grab the cover art if available.
|
|
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 we want multiple file we take the big mp3 and split it by chapter.
|
|
if [ "${mode}" == "chaptered" ]; then
|
|
# Playlist m3u support
|
|
playlist_file="${output_directory}/${title}.m3u"
|
|
debug "Creating PlayList ${title}.m3u"
|
|
echo '#EXTM3U' > "${playlist_file}"
|
|
|
|
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}"
|
|
|
|
# the ID3 tags must only be specified for *.mp3 files,
|
|
# the other container formats come with their own
|
|
# tagging mechanisms.
|
|
id3_version_param=""
|
|
if test "${extension}" = "mp3"; then
|
|
id3_version_param="-id3v2_version 3"
|
|
fi
|
|
|
|
</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}" \
|
|
"${chapter_file}"
|
|
|
|
duration=$(echo "${end} - ${start%?}" | bc)
|
|
echo "#EXTINF:${duration%.*},${title} - ${chapter}" >> "${playlist_file}"
|
|
echo "${chapter_title}.${extension}" >> "${playlist_file}"
|
|
chapternum=$((chapternum + 1 ))
|
|
fi
|
|
done 9< "$metadata_file"
|
|
rm "${full_file_path}"
|
|
debug "Done creating chapters. Chaptered files contained in ${output_directory}."
|
|
fi
|
|
|
|
debug "Done."
|
|
rm "${metadata_file}"
|
|
done
|