73 lines
No EOL
2.1 KiB
Bash
73 lines
No EOL
2.1 KiB
Bash
#!/usr/bin/env bash
|
|
set -x
|
|
set -eufo pipefail
|
|
|
|
# CONSTANTS
|
|
declare -r m4b-tool="nix run github:sandreas/m4b-tool#m4b-tool-libfdk -- "
|
|
declare -r script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
# VARS
|
|
LEADING_ZEROES=2
|
|
SRC="${SRC:${script_dir}/src}"
|
|
OUT="${OUT:${script_dir}/out}"
|
|
AUTHOR="${AUTHOR:-}"
|
|
NARRATOR="${NARRATOR:-}"
|
|
TITLE="${TITLE:-}"
|
|
YEAR="${YEAR:-}"
|
|
SERIES="${SERIES:-}"
|
|
SERIES_INDEX="${SERIES_INDEX:-}"
|
|
|
|
# FUNCTIONS
|
|
parse-vars() {
|
|
# TODO implement parsing the directory structure to fill in missing vars and return them
|
|
# Series structure: ${SRC}/<author>/<series>/Book <part> - <year> - <book> {<narrator>}/
|
|
# Standalone structure: ${SRC}/<author>/<year> - <book> {<narrator>}/
|
|
# Return the <part> wihh leading zeros based on the LEADING_ZEROES variable
|
|
}
|
|
|
|
get-book-directories() {
|
|
# TODO implemet getting the book directoris based on the ${SRC} path
|
|
# find the directories that contains the books files and return them as an array
|
|
# Example book directory:
|
|
# /path/to/src/Author/Series/Book Part - Year - Book {Narrator}/
|
|
# /path/to/src/Author/Year - Book {Narrator}/
|
|
}
|
|
|
|
m4b-merge() {
|
|
local output_file="${1}"
|
|
local source_dir="${2}"
|
|
local author="${3}"
|
|
local narrator="${4}"
|
|
local title="${5}"
|
|
local year="${6}"
|
|
local series="${7}"
|
|
local series_index=${8}
|
|
|
|
mkdir -p "$(dirname "${output_file}")"
|
|
"${m4b-tool}" \
|
|
merge \
|
|
-v \
|
|
--jobs=6 \
|
|
--audio-samplerate=44100 \
|
|
--audio-quality=100 \
|
|
--writer="${author}" \
|
|
--artist="${narrator}" \
|
|
--title="${title}" \
|
|
--year="${year}" \
|
|
--album="${series}" \
|
|
--album-sort="${series_index}" \
|
|
--output-file="${output_file}/" \
|
|
-- "${source_dir}"
|
|
}
|
|
|
|
main() {
|
|
parse-vars
|
|
local book_dirs
|
|
IFS=$'\n' read -r -d '' -a book_dirs < <(get-book-directories && printf '\0')
|
|
for book_dir in "${book_dirs[@]}"; do
|
|
local book_output_file="${OUT}/${AUTHOR}/${(SERIES:+${SERIES}/)}${(SERIES:+Book ${SERIES_INDEX} - )}${YEAR} - ${TITLE}.m4b"
|
|
m4b-merge "${LEADING_ZEROES}" "${book_output_file}" "${book_dir}" "${AUTHOR}" "${NARRATOR}" "${TITLE}" "${YEAR}" "${SERIES}" "${SERIES_INDEX}"
|
|
done
|
|
}
|
|
|
|
main "$@" |