This is an old revision of the document!


Adding ReplayGain to FLACs in Bulk

#!/usr/bin/env bash
#
###############################################################################
# A routine to go through every folder mentioned in the local
# music database and apply ReplayGain to the FLACs found within them.
# If ReplayGain is determined to already have been computed, the folder is
# skipped without further work being performed.
#
###############################################################################
clear
sqlite3 "$HOME/.local/share/giocoso3/db/test.db" "select dirname from recordings order by composer" > "directories.txt"
tput civis

# File containing the list of directories
DIRFILE="directories.txt"

# Initialize counters
GAINCOUNTER=0
SKIPCOUNTER=0

# Loop through each line of the file
while IFS= read -r dirname || [[ -n "$dirname" ]]; do

    # Change into the directory
    cd "$dirname" || { echo "Failed to cd into $dirname"; continue; }
    
    FLACFILE=$(find "$(pwd)" -name "*.flac" | sort | head -n 1)
    DISPLAYFLAC="${FLACFILE#/*/*/*/*/}"; DISPLAYFLACFILE="$(basename "$DISPLAYFLAC")"; DISPLAYFLACFILE=${DISPLAYFLACFILE:0:98}
    DISPLAYPATH="$(dirname "$DISPLAYFLAC")"; DISPLAYPATH=${DISPLAYPATH:0:98}
    GAIN=$(metaflac --show-tag=REPLAYGAIN_ALBUM_GAIN "$FLACFILE" 2>/dev/null | cut -d= -f2)
    tput cup 2 54; echo -n "ReplayGain Computed: $GAINCOUNTER - Skipped: $SKIPCOUNTER"
    if [[ -z "$GAIN" ]]; then
        tput cup 6 2; printf '%*s' 98 ''; tput cup 6 2; echo -n "$DISPLAYPATH"
        tput cup 7 2; printf '%*s' 98 ''; tput cup 7 2; echo -n "$DISPLAYFLACFILE"
        echo "$FLACFILE" >> "$HOME/Desktop/adjusted.txt"
        metaflac --add-replay-gain *.flac 2>/dev/null 
        ((GAINCOUNTER++))    
    else
        tput cup 6 2; printf '%*s' 98 ''; tput cup 6 2; echo -n "$DISPLAYPATH"
        tput cup 7 2; printf '%*s' 98 ''; tput cup 7 2; echo -n "$DISPLAYFLACFILE"
        ((SKIPCOUNTER++)) 
    fi
    # ----- END ORIGINAL CODE BLOCK -----
done < "$DIRFILE"
tput civvis
  • articles/bulkreplaygain.1769719675.txt.gz
  • Last modified: 2026/01/29 20:47
  • by hjr