#!/bin/sh # this script encodes flac files at level 8 to achieve the highest amount of # lossless compression and reduce file sizes. it obviously requires flac! # it should always be run first before any of the tagging scripts, as flac/metaflac # may error when decoding files that haven't been re-encoded. this is especially # common with flac 1.4 which has much stricter standards compared to earlier versions. # the erroring seems to happen when ID3 tags are present (which flacs should not have!) # or if track lengths are very short (<20s). see these issues for more info: # https://github.com/xiph/flac/issues/487 # https://github.com/xiph/flac/issues/653 # the -e arg can be used to activate exhaustive / precision searching. theoretically # this should result in the best possible compression but I've found that the difference # between this and regular level 8 compression is often little to none, whilst taking far, # far longer to run. I've left it here as an option, but it really isn't necessary to use. while getopts "d:emh" o; do case "${o}" in d) master="$OPTARG";; e) ex=1;; m) mod=1;; h|*) printf "usage: ./%s [OPTION]...\n\noptions: -d specify directory to scan -e use exhaustive searching -m modify files\n" "$(basename "$0")" && exit 1 esac done # check variables and programs. [ ! "$(command -v flac)" ] && echo "flac must be installed to use this script." && exit [ ! -d "$master" ] && echo "invalid directory!" && exit [ -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 # loop through sub directories echo "$dirs" | while read -r dir; do # check if sub directories 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)" "$(basename "$dir")" # encode files if [ -n "$mod" ]; then if [ -n "$ex" ]; then flac -8 -r 8 -f -e -p "$dir"/*.flac else flac -8 -r 8 -f "$dir"/*.flac fi fi num=$((num+1)) fi done printf "\n\033[1mscript completed!\033[0m\n"