rss logo

GNU/Linux script to automatically set mp3 tags

Here is a Shell script which can automatically set mp3 tags to multiple folders in a GNU/Linux environment.

The script will set the following tags :

Configuration

Instructions

Mp3 albums need to be structured like this : DIRECTORY_TO_WORK/BANDS/YEAR - ALBUM_NAME/01 - first_track.mp3

the doors strange days mp3 in a linux file explorer

Code

#! /bin/sh
# auto-mp3tags.sh version 1.0
# 2016.10.30  shebangthedolphins.net first version
#---------------------------------------------------
# this script automatically set mp3/id3v2 tags
# needs id3v2 to work
# you need to respect this path : DIRECTORY_TO_WORK/BANDS/YEAR - ALBUM_NAME/01 - first_track.mp3
#---------------------------------------------------

## If no arguments found
option_found=0

usage()
{
        echo "usage: ./auto-mp3tags.sh -d [DIRECTORY_TO_WORK]"
        echo "ex : ./auto-mp3tags.sh -d /mnt/USB/"
        echo ""
        exit 3
}

while getopts d: OPTNAME; do
        case "$OPTNAME" in
        d)
                ROOT="$OPTARG"
                option_found=1
                ;;

        *)
                usage
                ;;
        esac
done

if [ "$option_found" -eq "0" ] || [ -d "ROOT" ] ; then
        usage
fi

FOLDER=$(echo "$ROOT" | sed 's/\/$//')

for ARTIST in "$FOLDER"/*
do
        ARTIST=$(expr "$ARTIST" : '.*/\(.*\)')
        cd "$FOLDER/$ARTIST"
        for DATE_ALBUM in *
        do
                if [ -d "$DATE_ALBUM" ]; then
                        YEAR=$(expr "$DATE_ALBUM" : '\([0-9]\{4\}\).*')
                        ALBUM=$(expr "$DATE_ALBUM" : '[0-9]\{4\}\s-\s\(.*\)')
                        cd "$DATE_ALBUM"
                        for MP3 in *mp3
                        do
                                TITLE=$(expr "$MP3" : '^[0-9]\{2\} - \(.*\)\.mp3')
                                NUM=$(expr "$MP3" : '^\([0-9]\{2\}\) - .*')
                                echo "id3v2 --artist \""$ARTIST"\" --year "$YEAR" --album \""$YEAR - $ALBUM"\" --track "$NUM" --song \""$TITLE"\" \""$MP3"\""
                                echo "$PWD"
				id3v2 --artist "$ARTIST" --year "$YEAR" --album "$YEAR - $ALBUM" --track "$NUM" --song "$TITLE" "$MP3"
                        done
			sync	#Synchronize cached writes to persistent storage
                        cd "$FOLDER/$ARTIST"
                fi
        done
done

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address