Batch Processing ReplayGain

@mjw - Thanks :slight_smile:

I added some additional info here if somebody is using DietPi

Have a nice WE

Torben

1 Like

Just a note to say that globbing is more efficient and simpler than using exec or xargs. It’s safer and less complex, too.

For example, echo test.log | xargs touch does what touch test.log does, and can be problemtic at times (escaping is needed and long paths with double byte characters could break things.)

Every time you use these, you spawn a new process because its reading stdout into a variable, and processsing that instead.

Adding this here rather than your DietPi thread.

1 Like

Thanks @mjw - I am NOT an expert in these things :slight_smile:

How would this than look like, based on you comments:

#!/bin/bash

MUSIC="/mnt/dietpi_userdata/Music"
find "$MUSIC" -type f -iname "*.flac" -print0 | \
xargs -0 -n1 -P$(nproc) bash -c '
    for file; do
        if ! metaflac --list "$file" | grep -q "REPLAYGAIN_TRACK_GAIN"; then
            echo "Adding ReplayGain: $file"
            metaflac --add-replay-gain "$file"
        else
            echo "Skipping: $file"
        fi
    done
' _

Well, it’s horrible code to read, and a bit pointless, too. Just add ReplayGain as it’s more efficient than listing tags, grepping for ReplayGain, testing the condition, and optionally adding.

Add will simply replace what is there. If you must, test with show tag, i.e., if tag exists don’t write the tag. That’s a one-liner, and simple to understand.

One important point: preserve the file modification time or add IMPORTDATE to files so your library isn’t messed up.

When (if?) I ever finish Tagaroo I’ll share it here.

THX. So you would prefer:

#!/bin/bash
MUSIC="/mnt/dietpi_userdata/Music"
find $MUSIC -type f -name "*.flac" -exec metaflac --add-replay-gain {} \;

Torben

Yes, that would be better. But, I’d use this.

#!/bin/bash
MUSIC=/path/to/media/files
shopt -s globstar
for filepath in $MUSIC/**/*.flac; do metaflac --add-replay-gain "$filepath"; done

You can provide information as it loops through files and folders, too, e.g.:

folder="${filepath%/*.flac}"
# and so on
1 Like