2016-07-13 11:25:43 +02:00
|
|
|
#!/usr/bin/env bash
|
2016-12-16 05:25:27 +01:00
|
|
|
|
2017-01-06 00:56:32 +01:00
|
|
|
set -o errexit -o noclobber -o nounset -o pipefail
|
|
|
|
|
|
|
|
codec=libmp3lame
|
|
|
|
extension=mp3
|
|
|
|
|
|
|
|
if [[ "$1" = '--flac' ]]
|
|
|
|
then
|
|
|
|
codec=flac
|
|
|
|
extension=flac
|
|
|
|
shift
|
2016-12-16 05:25:27 +01:00
|
|
|
fi
|
|
|
|
|
2017-01-06 00:56:32 +01:00
|
|
|
auth_code=$1
|
|
|
|
shift
|
2016-03-31 04:48:35 +02:00
|
|
|
|
2017-01-06 00:56:32 +01:00
|
|
|
debug() {
|
|
|
|
echo "$(date "+%F %T%z") ${1}"
|
|
|
|
}
|
2016-07-13 11:58:43 +02:00
|
|
|
|
2017-01-06 00:56:32 +01:00
|
|
|
trap 'rm --recursive --force "${working_directory}"' EXIT
|
|
|
|
working_directory="$(mktemp --directory)"
|
|
|
|
metadata_file="${working_directory}/metadata.txt"
|
2016-07-13 11:58:43 +02:00
|
|
|
|
2017-01-06 00:56:32 +01:00
|
|
|
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 --delimiter=: --fields=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}"
|
|
|
|
title=$(get_metadata_value title)
|
|
|
|
output_directory="$(dirname "${path}")/$(get_metadata_value genre)/$(get_metadata_value artist)/${title}"
|
|
|
|
mkdir -p "${output_directory}"
|
|
|
|
full_file_path="${output_directory}/${title}.${extension}"
|
|
|
|
ffmpeg -loglevel error -stats -activation_bytes "${auth_code}" -i "${path}" -vn -codec:a "${codec}" -ab "$(get_bitrate)k" "${full_file_path}"
|
|
|
|
|
|
|
|
debug "Created ${full_file_path}."
|
|
|
|
|
|
|
|
debug "Extracting chapter files from ${full_file_path}..."
|
|
|
|
|
|
|
|
while read -r -u9 first _ _ start _ end
|
|
|
|
do
|
|
|
|
if [[ "${first}" = "Chapter" ]]
|
|
|
|
then
|
|
|
|
read -r -u9 _
|
|
|
|
read -r -u9 _ _ chapter
|
|
|
|
chapter_file="${output_directory}/${title} - ${chapter}.${extension}"
|
|
|
|
ffmpeg -loglevel error -stats -i "${full_file_path}" -ss "${start%?}" -to "${end}" -codec:a copy "${chapter_file}"
|
|
|
|
fi
|
|
|
|
done 9< "$metadata_file"
|
|
|
|
debug "Done creating chapters. Single file and chaptered files contained in ${output_directory}."
|
|
|
|
|
|
|
|
cover_path="${output_directory}/cover.jpg"
|
|
|
|
debug "Extracting cover into ${cover_path}..."
|
|
|
|
ffmpeg -loglevel error -activation_bytes "${auth_code}" -i "${path}" -an -codec:v copy "${cover_path}"
|
|
|
|
debug "Done."
|
|
|
|
rm "${metadata_file}"
|
2016-03-31 04:48:35 +02:00
|
|
|
done
|