#!/bin/sh # this script uses metaflac to set the genre tag. # tags will first be set based on language: # japanese -> J-Pop # chinese -> C-Pop # korean/english/etc. -> K-Pop # then files with 'inst' or 'instrumental' in the name will also be given the Instrumental tag. # for the script to work properly, albums must first be sorted into their appropriate language folders like so: # YENA/Lossless/Japanese/YENA [2023.08.07] SMILEY [FLAC-16bit-44.1kHz] check() { [ ! "$(command -v metaflac)" ] && echo "metaflac must be installed to use this script." && exit [ ! -d "$master" ] && echo "invalid directory!" && exit [ ! -d "$(dirname "$log")" ] && mkdir "$(dirname "$log")" } while getopts "d:mh" o; do case "${o}" in d) master="$OPTARG";; m) mod=1;; h|*) printf "usage: ./%s [OPTION]...\n\noptions: -d specify directory to scan -m modify files\n" "$(basename "$0")" && exit 1 esac done # set log file. log="../logs/genre.log" # check variables and programs. check # begin new log. printf "\nscript run - %s\n" "$(date)" >> "$log" [ -z "$mod" ] && printf "\033[1mperforming DRY RUN. use -m to modify files.\033[0m\n\n" # find sub directories in master directory. dirs="$(find "$master" -type d -links 2)" num=1 # main loop echo "$dirs" | while read -r dir; do # check if directory contain flac files. if ls "$dir"/*.flac >/dev/null 2>&1; then printf "\033[1m[%d/%d] %s\033[0m\n" "$num" "$(echo "$dirs" | wc -l)" "$(echo "$dir" | sed 's|.*Lossless/||')" # get language. case "$(dirname "$dir" | sed 's|.*/||')" in Japanese) genre="J-Pop";; Chinese) genre="C-Pop";; *) genre="K-Pop";; esac # check if genre is already set correctly. doing this will leave additional genre tags intact. if [ "$(metaflac --show-tag=GENRE "$(ls "$dir"/*.flac | head -n 1)" | head -n 1 | cut -d '=' -f 2)" != "$genre" ]; then echo "setting genre -> $genre" if [ -n "$mod" ]; then # set genre. metaflac --remove-tag=GENRE --set-tag=GENRE="$genre" "$dir"/*.flac # check if genre was set correctly and no errors occurred. if [ "$(metaflac --show-tag=GENRE "$(ls "$dir"/*.flac | head -n 1)" | cut -d '=' -f 2)" != "$genre" ]; then echo "$dir -> failed setting genre: $genre" >> "$log" fi else echo "$dir -> $genre" >> "$log" fi else [ -z "$mod" ] && echo "genre already set correctly" fi # set instrumental tag. ls "$dir"/*.flac | while read -r file; do # check if file name contains 'inst' or 'instrumental'. if echo "${file%.*}" | grep -qE '(\([Ii]nst\)|[Ii]nst\.|[Ii]nstrumental)'; then # check if instrumental is already set correctly. if ! metaflac --show-tag=GENRE "$file" | grep -q Instrumental; then echo "$(basename "$file") setting genre -> Instrumental" if [ -n "$mod" ]; then metaflac --set-tag=GENRE=Instrumental "$file" # check if instrumental was set correctly and no errors occurred. if ! metaflac --show-tag=GENRE "$file" | grep -q Insrumental; then echo "$file -> failed setting Instrumental" >> "$log" fi else echo "$file -> Instrumental" >> "$log" fi else [ -z "$mod" ] && echo "$(basename "$file") -> Instrumental already set correctly" fi fi done num=$((num+1)) fi done printf "\n\033[1mscript completed! remember to check log for errors.\033[0m\n"