From b8f3397afeb1a89643241266088b4329ed909bb9 Mon Sep 17 00:00:00 2001 From: phg Date: Sun, 7 Sep 2025 12:04:37 +0200 Subject: [PATCH] initial commit --- .gitignore | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ to-m4b.sh | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 .gitignore create mode 100644 to-m4b.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9178f67 --- /dev/null +++ b/.gitignore @@ -0,0 +1,82 @@ +out/ +src/ + +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# Metadata left by Dolphin file manager, which comes with KDE Plasma +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +# Log files created by default by the nohup command +nohup.out + +# General +.DS_Store +__MACOSX/ +.AppleDouble +.LSOverride +Icon[ +] + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets +!*.code-workspace + +# Built Visual Studio Code Extensions +*.vsix diff --git a/to-m4b.sh b/to-m4b.sh new file mode 100644 index 0000000..f71e395 --- /dev/null +++ b/to-m4b.sh @@ -0,0 +1,73 @@ +#!/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}///Book - - {}/ + # Standalone structure: ${SRC}// - {}/ + # Return the 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 "$@" \ No newline at end of file