1
|
#!/bin/bash
|
2
|
#
|
3
|
# Fix all ARTIST tags with "The bla-de-bla" to "bla-de-bla, The"
|
4
|
#
|
5
|
# Thanks to http://blog.tordeu.com/?p=184 for snippets
|
6
|
#
|
7
|
if [ $1 ]; then
|
8
|
do_artist=$1
|
9
|
else
|
10
|
do_artist=""
|
11
|
fi
|
12
|
base_dir=/home/music
|
13
|
high_dir="flac"
|
14
|
tmpy=/tmp/flac_un_the
|
15
|
# Some stat counters
|
16
|
let n_artists=0
|
17
|
let n_albums=0
|
18
|
let n_tracks=0
|
19
|
let n_artists_r=0
|
20
|
let n_albums_r=0
|
21
|
let n_tracks_r=0
|
22
|
let n_artists_c=0
|
23
|
let n_albums_c=0
|
24
|
let n_tracks_c=0
|
25
|
last_artist=""
|
26
|
last_album=""
|
27
|
# Create a tmp directory for dumps
|
28
|
if [ ! -d "${tmpy}" ]; then
|
29
|
mkdir ${tmpy}
|
30
|
fi
|
31
|
# Scan through all of the high quality directories
|
32
|
for this_high_dir in ${high_dir}
|
33
|
do
|
34
|
# Scan for artists
|
35
|
echo "Scanning ${this_high_dir} directory ..."
|
36
|
ls -1 "${base_dir}/${this_high_dir}" > ${tmpy}/${this_high_dir}
|
37
|
# Scan for albums
|
38
|
while read artist
|
39
|
do
|
40
|
if [ "${do_artist}" = "" ] || [ "${do_artist}" = "${artist}" ]; then
|
41
|
let n_artists=$n_artists+1
|
42
|
# Scan for tracks
|
43
|
echo " Found ${artist} - scanning for albums ..."
|
44
|
artist_clean=`echo ${artist//[ \[\]()&;\'\"\,]/_} | tr -s _ _ `
|
45
|
if [ "${artist}" != "${artist_clean}" ]; then
|
46
|
echo " Renaming ${artist} to ${artist_clean}"
|
47
|
mv "${base_dir}/${this_high_dir}/${artist}" "${base_dir}/${this_high_dir}/${artist_clean}"
|
48
|
artist=${artist_clean}
|
49
|
let n_artists_r=$n_artists_r+1
|
50
|
fi
|
51
|
ls -1 "${base_dir}/${this_high_dir}/${artist}" > ${tmpy}/${this_high_dir}-${artist}
|
52
|
while read album
|
53
|
do
|
54
|
let n_albums=$n_albums+1
|
55
|
echo " Found ${album} - scanning for tracks ..."
|
56
|
album_clean=`echo ${album//[ \[\]()&;\'\"\,]/_} | tr -s _ _ `
|
57
|
if [ "${album}" != "${album_clean}" ]; then
|
58
|
echo " Renaming ${album} to ${album_clean}"
|
59
|
mv "${base_dir}/${this_high_dir}/${artist}/${album}" "${base_dir}/${this_high_dir}/${artist}/${album_clean}"
|
60
|
album=${album_clean}
|
61
|
let n_albums_r=$n_albums_r+1
|
62
|
fi
|
63
|
ls -1 "${base_dir}/${this_high_dir}/${artist}/${album}" > ${tmpy}/${this_high_dir}-${artist}-${album}
|
64
|
# Now the fun bit ... let's start re-encoding the track we've just found
|
65
|
while read track
|
66
|
do
|
67
|
track_the=`echo ${track} | awk -F "-" '{print $2}' | awk -F "_" '{print $1}'`
|
68
|
if [ "${track_the}" = "the" ]; then
|
69
|
track_id=`echo ${track} | awk -F "-" '{print $1}'`
|
70
|
track_artist=`echo ${track} | awk -F "-" '{print $2}'`
|
71
|
track_the_rest=`echo ${track} | awk -F "-" '{$1=$2=""}1' | tr -d ' ' | tr ' ' '_'`
|
72
|
track_artist_rest=`echo ${track_artist} | awk -F "_" '{$1=""}1' | sed 's/^ *//g' | tr ' ' '_'`
|
73
|
track_clean="${track_id}-${track_artist_rest}_the-${track_the_rest}"
|
74
|
echo " Renaming ${track}"
|
75
|
echo " to ${track_clean}"
|
76
|
mv "${base_dir}/${this_high_dir}/${artist}/${album}/${track}" "${base_dir}/${this_high_dir}/${artist}/${album}/${track_clean}"
|
77
|
track=${track_clean}
|
78
|
let n_tracks_r=$n_tracks_r+1
|
79
|
fi
|
80
|
this_source="${base_dir}/${this_high_dir}/${artist}/${album}/${track}"
|
81
|
this_dest=`echo ${base_dir}/${out_dir}/${artist}/${album}/${track} | sed 's/.flac/.mp3/g'`
|
82
|
# Check that it's a FLAC file
|
83
|
if [ `file "${this_source}" | grep -c "FLAC audio bitstream data"` = "1" ]; then
|
84
|
let n_tracks=$n_tracks+1
|
85
|
# Get tag data from source.
|
86
|
# Artist
|
87
|
s_artist=`metaflac --show-tag=ARTIST "${this_source}" | awk -F "=" '{print $2}'`
|
88
|
is_the_the=`echo ${s_artist} | awk '{print $1}'`
|
89
|
if [ "${is_the_the}" = "The" ] || [ "${is_the_the}" = "the" ] || [ "${is_the_the}" = "THE" ]; then
|
90
|
#the_rest=`echo ${s_artist} | awk '{for(i=2;i<=NF;i++)printf "%s", $i}' | tr '\n' ' '`
|
91
|
the_rest=`echo ${s_artist} | awk '{$1=""}1' | sed 's/^ *//g'`
|
92
|
new_artist="${the_rest}, The"
|
93
|
echo " Re-tagging \"${s_artist}\" to \"${new_artist}\""
|
94
|
echo " in ${track}"
|
95
|
metaflac --remove-tag=ARTIST ${this_source}
|
96
|
metaflac --set-tag="ARTIST=${new_artist}" ${this_source}
|
97
|
let n_tracks_c=$n_tracks_c+1
|
98
|
if [ "${artist}" != "${last_artist}" ]; then
|
99
|
let n_artists_c=$n_artists_c+1
|
100
|
fi
|
101
|
if [ "${album}" != "${last_album}" ]; then
|
102
|
let n_albums_c=$n_albums_c+1
|
103
|
fi
|
104
|
last_artist=${artist}
|
105
|
last_album=${album}
|
106
|
##########################
|
107
|
# Wasn't that simple?!?! #
|
108
|
##########################
|
109
|
fi
|
110
|
fi
|
111
|
done <${tmpy}/${this_high_dir}-${artist}-${album}
|
112
|
done <${tmpy}/${this_high_dir}-${artist}
|
113
|
fi
|
114
|
done <${tmpy}/${this_high_dir}
|
115
|
done
|
116
|
echo ""
|
117
|
echo "Found ${n_tracks} tracks in ${n_albums} albums from ${n_artists} artists"
|
118
|
echo "Renamed ${n_tracks_r} tracks, ${n_albums_r} albums and ${n_artists_r} artists"
|
119
|
echo "Re-tagged ${n_tracks_c} tracks in ${n_albums_c} albums from ${n_artists_c} artists"
|
120
|
echo ""
|
121
|
# Now clean up the tmp area
|
122
|
""rm -f -R ${tmpy}
|