Add system checks and folder creations. Make the conversion to mp3 more abstract.

This commit is contained in:
Hyperling 2025-03-13 05:57:16 -07:00
parent 92fe141508
commit bbb97b81c7

View File

@ -1,6 +1,6 @@
#!/usr/bin/env bash
# 2025 Hyperling
# Clean up a music library.
# Clean up a music library. Improved version of rename_music_files.sh.
# Steps:
# 0) Ensure an OLD folder exists, it is what will be refactored.
@ -10,20 +10,70 @@
# - Name parent folder as "${YEAR} - ${ALBUM}" if both exist, otherwise just one or the other "${YEAR}${ALBUM}".
# - Name grandparent folder as "${ARTIST} - ${GENRE}" if both exist, otherwise "${ARTIST}${GENRE}".
# TODO List
# TODO list:
# - Organize tracks by Artist + Album folders.
# - Name parent folder as "$YEAR - $ALBUM" if both exist, otherwise just one or the other.
# - Name grandparent folder as ARTIST if it exists, otherwise GENRE.
# - Name grandparent folder as ARTIST if it exists, otherwise GENRE.
# If we are in OLD, go up one level.
## Facts and Variables ##
PROG="$(basename -- "${BASH_SOURCE[0]}")"
FAIL=".$PROG.exit-error"
OLD="`pwd`/OLD"
NEW="`pwd`/NEW"
INT="`pwd`/INTERIM"
## Checks and Setup ##
# System Packages #
echo "- Checking if necessary tools are installed."
function needed_progs {
cat <<- EOF
ffmpeg
exiftool
EOF
}
needed_progs | while read prog; do
if [[ -z "`which $prog`" ]]; then
echo "ERROR: $prog not found, please install."
touch .$PROG.exit-error
fi
done
if [[ -e .$PROG.exit-error ]]; then
rm -rf .$PROG.exit-error
exit 1
fi
echo " - Tools are available."
# Old Directory #
echo "- Checking whether OLD directory exists."
if [[ "`pwd`"== */OLD ]]; then
echo " - We are in the folder, moving up a directory."
cd ..
fi
if [[ ! -d OLD ]]; then
echo "ERROR: Could not find a folder named 'OLD', please create it and add your library there."
echo "ERROR: Could not find a folder named 'OLD'."
echo " Please create it and add your library there."
fi
echo " - Folder exists appropriately."
# Other Directories #
echo "- Ensuring working directories NEW and INTERIM exist."
mkdir -pv $NEW $INT
## Convert Media ##
echo "- Convert all files to mp3."
find "$OLD" ! -name "*".mp3 | while read file; do
ffmpeg -nostdin -hide_banner -loglevel quiet -i "$file" "$file".converted.mp3
done
echo " - Done creating mp3s."
## Transform Media ##
echo "- Moving INTERIM files to NEW."
echo "Convert m4a's to mp3's."
ls *.m4a | while read m4a; do ffmpeg -nostdin -hide_banner -loglevel quiet -i "$m4a" "$m4a".mp3; done
echo "- Copying OLD files to NEW."