[BASH] Add replaygain and rename musicfiles based on tags.

Come up with a useful post-processing script? Share it here!
Post Reply
User avatar
Mar2zz
Jr. Member
Jr. Member
Posts: 85
Joined: February 4th, 2011, 8:30 am
Contact:

[BASH] Add replaygain and rename musicfiles based on tags.

Post by Mar2zz »

This script will add Replaygain to your files (mp3, flac or ogg) then renames all files based on tags in the files self (the safe way, if tags are missing it will do not so) and move them to the folder you set in .arename.rc. As a bonus is deletes garbage from the sourcedir.

If you want to do a Musicbrainz check first for those tags then use my albumidentifyscript (see my other topic). (those scripts can be linked together if you want to process both ways).

Code: Select all

#!/usr/bin/env bash

# Author:  Mar2zz
# blogs: mar2zz.tweakblogs.net
# License: GPL v3
#
# Add replaygain to your files, then rename and move them inside your musiclibrary. 
#
# Depends on arename, mp3gain, vorbisgain and flac (sudo apt-get install arename mp3gain vorbisgain flac)
#
# Create a configfile for arename to set your default musicdirectory (~/.arenamerc)
# http://ft.bewatermyfriend.org/comp/arename/arename.html
#
#
##################### MINIMAL .ARENAMERC : ##########################################
#------------------------------------------------------------------------------------
#  # switch on verbosity
#  #verbose

#  # canonicalize file names before working with them
#  canonicalize

#  # template will be used when albumartist is the same as all trackartists
#  # comp_template will be used when albumartist is not the same as trackartists
#  template        &artist/(&year) &album/&tracknumber. &artist - &tracktitle
#  comp_template   Various Artists/&album (&year)/&tracknumber. &artist - &tracktitle
#  
#  #set default values for tags if empty (uncomment if needed)
#  #default_artist no_artist
#  #default_album no_album
#  #default_compilation Various Artists
#  #default_genre
#  #default_tracknumber 00
#  #default_tracktitle no_title
#  default_year 0000
#  
#  # activate the 'music' profile below /mnt/audio/music/.
#  #profile music /mnt/audio/music/

#  # force files from /foo/bar/ to stay below that directory
#  prefix /home/user/Music
# ---------------------------------------------------------------------------------------------
#
#
#
# -----------------------------------------
# Works only for mp3, ogg and flacfiles
# -----------------------------------------


DIR=$1;
JOBNAME=$3;
FAILDIR=$HOME/Untagged
GARBAGE=".nfo .srr .sfv .nzb .m3u .exe"          #### Add or remove extensions here, files with extensions listed will be deleted



Cleanup () { #### Remove unwanted files
for junk in $GARBAGE
do
find "$DIR" -name *$junk -type f -exec rm -f {} \;
done
}

apply_RG () {
if find "$DIR" -name "*.mp3" -type f 
    then 
    RG="mp3gain -T -a -r -c -w"
    find "$DIR" -name "*.mp3" -type f -exec $RG {} \;
elif find "$DIR" -name "*.ogg" -type f
    then
    RG="vorbisgain -f -n -r -s"
    find "$DIR" -name "*.ogg" -type f -exec $RG {} \;
elif find "$DIR" -name "*.flac" -type f
    then 
    RG="metaflac --add-replay-gain"
    find "$DIR" -name "*.flac" -type f -exec $RG {} \;
else
    echo "No valid files found"
    exit
fi
}

do_Renaming () {
echo
echo "--------------------------"
echo $(date)
echo "Start renaming $JOBNAME"
echo
ARENAME="arename -c --rc /home/marsjaco/.arenamerc" #### remove -c if you want to delete sourcefiles

find "$DIR" \( -name "*.mp3" -o -name "*.ogg" -o -name "*.flac" \) -type f -exec $ARENAME {} '+' | grep -Ev "\-\!\-" > /tmp/arename.log

if find "$DIR" \( -name "*.mp3" -o -name "*.ogg" -o -name "*.flac" \) -type f | grep -i 'mp3\|ogg\|flac'    
    then
    echo "Not all files renamed, moving;"
    echo '-------------------------------------------------'
    ls -R "$DIR"
    echo '-------------------------------------------------'
    echo "to $FAILDIR/$JOBNAME"
    mkdir $FAILDIR ||
    mv "$DIR" $FAILDIR/$JOBNAME
else
    echo "Renaming succes, results:"
    echo '-------------------------------------------------'
    grep 'mp3\|ogg\|flac' /tmp/arename.log
    echo '-------------------------------------------------'
    rm -f /tmp/arename.log
    #rm -Rf "$DIR"    #uncomment if you want to delete the sourcefolder after succesfull processing.
fi
}


#### CALL FUNCTIONS ####
Cleanup            # comment if you do not want to cleanup
apply_RG        # comment line if you do not want to Replaygain your files
do_Renaming      # comment if you do not want to rename your files
}
Last edited by Mar2zz on February 6th, 2011, 2:51 am, edited 1 time in total.
Post Reply