You are not logged in.

#1 2011-01-07 04:05:19

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Convert directory structure to ID3 tags

Here's a winky little script I wrote to convert a directory structure and filename to ID3 tags.

All of my music is arranged like this: ~/Music/Genre/Album/Artist - Title.mp3

The script can be run from anywhere. "Label" your MP3 file by putting it into the proper directories and giving it a proper filename, then run "music-add-tags *.mp3". It can handle one or more MP3 files at once.

The script depends on the packages "id3" and "id3v2".

music-add-tags:

#!/bin/bash

music_directory="$1"

format=mp3

for file in "$@"
do
  
  if [ -f "$file" ]
  then
    
    filename="$(readlink -f "$file")"

    # Genre/Album/Artist - Title
    
    parent_directories='.*/\([^/]*\)'
    after_hyphen='.*\ \-\ \([^\ \-\ ]*\)'
    before_hyphen='\([^\ \-\ ]*\)\ \-\ .*'
    
    song=$(echo "$filename" | sed "s%$parent_directories\."$format"$%\1%")
    
    title=$(echo "$song" | sed "s%$after_hyphen%\1%")
    artist=$(echo "$song" | sed "s%$before_hyphen%\1%")
    album=$(echo "$filename" | sed "s%$parent_directories/$song\."$format"$%\1%")
    genre=$(echo "$filename" | sed "s%$parent_directories/$album/$song\."$format"$%\1%")
    
    echo Tagging \"$song\"...

    #echo $title
    #echo $artist
    #echo $album
    #echo $genre
    
    id3v2 -D "$file" > /dev/null
    id3 -2 -1 -t "$title" -a "$artist" -l "$album" -g "$genre" "$file"
    
  fi
  
done

I had big dreams of making this a super nice script with options and stuff but, whetever, here. tongue

Offline

Board footer

Powered by FluxBB