Skip to content

Instantly share code, notes, and snippets.

@simonc
Created October 14, 2009 19:06
Show Gist options
  • Save simonc/210324 to your computer and use it in GitHub Desktop.
Save simonc/210324 to your computer and use it in GitHub Desktop.
Functions to add ID3 tags to mp3 files from their name
#!/usr/bin/env bash
tag_tracks ()
{
reg_name="s/^\([0-9]\{2\}\)___\(.*\)\.\(mp3\|ogg\)/\2/"
if [ -f ./Folder.jpg ]; then
img='./Folder.jpg'
else
if [ -f ../Folder.jpg ]; then
img='../Folder.jpg'
fi
fi
for fic in *.{mp3,ogg};
do
if [ -f $fic ]; then
num=`echo $fic | sed "s/\([0-9]\{1,3\}\).*/\1/"`
name=`echo $fic | sed "$reg_name" | tr -s "_" " "`
artist=`pwd | egrep -o "([^/]+)/[^/]+(/CD_[0-9]+)?$" | cut -d'/' -f1 | tr -s '_' ' '`
album=`pwd | egrep -o "[^/]+(/CD_[0-9]+)?$" | cut -d '/' -f1 | tr -s '_' ' '`
album_num=`pwd | egrep -o "CD_[0-9]+$" | cut -d '_' -f2`
if [ -z $album_num ]; then
album_num='1'
fi
echo "File: $fic"
echo "$num $artist $album($album_num) $name"
eyeD3 --remove-all --to-v2.4 -t"$name" -a"$artist" -A"$album" -n$num --add-image=$img:FRONT_COVER --set-text-frame=TPOS:$album_num $fic 1> /dev/null 2> /dev/null
if [ $? -eq 0 ]; then
echo "-----"
else
echo "KO"
fi
fi
done
}
tag_albums ()
{
for d in `ls -1 --color=none | tr -s "\n" " "`;
do
if [ -d $d ]; then
cd $d
line="$d"
if [ -d ./CD_1 ]; then
cd CD_1 && tag_tracks 2>&1 && line="$line [OK] "
cd ../CD_2 && tag_tracks 2>&1 && line="$line[OK]"
echo $line
cd ../..
else
tag_tracks 2>&1 && line="$line [OK]"
echo $line
cd ..
fi
fi
done
}
tag_artists ()
{
for d in `ls -1 --color=none | tr -s "\n" " "`
do
if [ -d $d ]; then
cd $d
artist_line="$d"
tag_albums && artist_line="$artist_line [OK]"
echo $artist_line
cd ..
fi
done
}
if [[ `basename '$0'` == 'tag_music.sh' ]]; then
case $1 in
'tracks')
tag_tracks
;;
'albums')
tag_albums
;;
'artists')
tag_artists
;;
*)
echo $1
;;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment