-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp3-tidy-file.sh
executable file
·269 lines (230 loc) · 7.98 KB
/
mp3-tidy-file.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/bin/bash
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
# Initialise variables
function showHelp() {
echo "Command line options:"
echo "-r Write changes, pass any argument."
echo "-p The directory path of the mp3 files to be tidied."
echo " By default it will use the location of this script."
echo " Should point to ./Artist/Album/%FILES_TO_TIDY%"
echo "-y The ablum year"
echo "-g Short code genre. For full list see http://axon.cs.byu.edu/~adam/gatheredinfo/organizedtables/musicgenrelist.php."
echo " Recommended genres and codes:"
echo " 7 Hip-Hop"
echo " 8 Jazz"
echo " 9 Metal"
echo " 13 Pop"
echo " 17 Rock"
echo " 32 Classical"
echo " 52 Electronic"
echo " 57 Comedy"
echo "-h Print this help menu"
echo ""
echo "Examples:"
echo " Dry run all mp3s in this directory"
echo " .mp3-tidy-file.sh -p ./Mark\ Ronson/Uptown\ Special"
echo ""
echo " Tidy all mp3s in this directory"
echo " .mp3-tidy-file.sh -p ./Mark\ Ronson/Uptown\ Special -r 1"
echo ""
}
run=0
path="${PWD}"
artist=""
album=""
song=""
year=""
track=""
trackName=""
trackTotal=""
genre=""
oldfilename=""
newfilename=""
genres="Electronica Hip-Hop Rock Metal Classical Comedy Jazz Pop"
while getopts "h?r:p:y:g:" opt; do
case "$opt" in
h|\?)
showHelp
exit 0
;;
r) run=1
;;
p) path="$OPTARG"
;;
y) year="$OPTARG"
;;
g) genre="$OPTARG"
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
# =================================================
# Set up and normalise shared data
# =================================================
function getAbsPath() {
local absPath=$(cd "$(dirname "$path")"; pwd)
local count=0
local ary
IFS='/' read -a ary <<< "${path}";
for i in "${ary[@]}"
do
if [[ "$absPath" != *"$i"* ]] && [[ "$i" != "." ]]; then
absPath=$absPath"/"$i
fi
done
path=$absPath
}
getAbsPath
function setSharedMetaData() {
local ary
local tempGenre=""
IFS='/' read -a ary <<< "${path}";
# Artist
artist=${ary[@]: -2:1}
# Album
album=${ary[@]: -1:1}
# trackTotal
trackTotal=$(find "$path" -name "*.mp3" | wc -l)
# Trim white space on trackTotal
trackTotal=$(echo "$trackTotal" | sed -E "s/^ *//" | sed -E "s/ *$//")
echo "Id3 shared MetaData"
echo "==================="
echo "Artist: "$artist
echo "Album: "$album
echo "Year: "$year
echo "Genre: "$genre
echo "trackTotal: "$trackTotal
echo ""
}
setSharedMetaData
# =================================================
# Format files and folders
# Add new formatting functions as neccessary based on further found idiosyncracies
# =================================================
function cleanUpLeadingDir() {
newfilename="${newfilename/.\//}"
}
function manageHyphensAndNumbering() {
#Random brackets around numbers?!?!
newfilename=$(echo "$newfilename" | sed -E "s/\(([0-9]*)\)/\1/g")
# Convert special character hyphens to standard
newfilename=$(echo "$newfilename" | sed -E "s/–/-/g")
# Hyphens
newfilename=$(echo "$newfilename" | sed -E "s/ - /-/g")
# http://regexpal.com/
# Test data:
# 01 - Uptown’s First Finale (feat. Stevie Wonder & Andrew Wyatt).mp3
# 01 - - Uptown’s First Finale (feat. Stevie Wonder & Andrew Wyatt).mp3
# 01-Uptown’s First Finale (feat. Stevie Wonder & Andrew Wyatt).mp3
# 01 Uptown’s First Finale (feat. Stevie Wonder & Andrew Wyatt).mp3
# - 01 Uptown’s First Finale (feat. Stevie Wonder & Andrew Wyatt).mp3
# - 01 Uptown’s First Finale (feat. Stevie Wonder & Andrew Wyatt).mp3
# Mark Ronson - 01 - Uptown’s First Finale (feat. Stevie Wonder & Andrew Wyatt).mp3
# Mark Ronson-01-Uptown’s First Finale (feat. Stevie Wonder & Andrew Wyatt).mp3
# Mark Ronson- 01 -Uptown’s First Finale (feat. Stevie Wonder & Andrew Wyatt).mp3
newfilename=$(echo "$newfilename" | sed -E "s/[\s]*-*\s*([0-9][0-9]+)\s*-*\s*-*\s*/ - \1 - /")
}
# Remove artist name in case it is already there.
# We will add it later - this prevents duplicates.
function removeArtistName() {
newfilename=$(echo "$newfilename" | sed "s/$artist//g")
}
function addArtistName() {
newfilename="${artist}${newfilename}"
}
function replaceFeaturing() {
# Stupid mac regex can not be set to case insensitive
# Commmon feat usage
newfilename=$(echo "$newfilename" | sed "s/feat/ft/g")
# Sentence case feat
newfilename=$(echo "$newfilename" | sed "s/Feat/ft/g")
# Sentence case feat
newfilename=$(echo "$newfilename" | sed "s/(with/(ft /g")
# Remove trailing period if it exists
newfilename=$(echo "$newfilename" | sed "s/ft./ft/g")
# Add trailing period as it now doesn't exist
newfilename=$(echo "$newfilename" | sed "s/ ft/ ft./g")
# Remove surrounding () parentheses
if [[ $newfilename == *\(ft.*\)* ]]; then
newfilename=$(echo "$newfilename" | sed -E "s/\((ft[^)]*)\)/\1/g")
fi
}
function cleanUpDoubles() {
# double or more hyphens
newfilename=$(echo "$newfilename" | sed -E "s/[-]+/-/g")
# double or more white space
newfilename=$(echo "$newfilename" | sed -E "s/[ ]+/ /g")
#double space & hypens
newfilename=$(echo "$newfilename" | sed -E "s/[ -]+[ -]+/ - /g")
}
function renameFiles() {
while IFS= read -d $'\0' -r file ; do
oldfilename=$(basename "$file")
newfilename="${oldfilename}"
# Run the formatting functions
cleanUpLeadingDir
manageHyphensAndNumbering
removeArtistName
addArtistName
replaceFeaturing
cleanUpDoubles
# Check if in dry run mode
if [[ $run != 1 ]]; then
printf "\"$oldfilename\" to \n\"$newfilename\"\n\n"
# In run mode
else
mv "$file" "$path/$newfilename" && printf "\"$newfilename\" successfully renamed.\n\n"
fi
done < <(find "$path" -iname '*.mp3' -print0)
# Check if in dry run mode
if [[ $run != 1 ]]; then
echo "If the conversion looks good run command with -r switch."
# In run mode
else
echo "Renaming complete."
fi
}
read -p "Do you want to rename files? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
renameFiles
fi
# =================================================
# Format id3 v1 & v2 flags
# =================================================
function getTrackName() {
trackName=$(echo "$1" | sed -E "s/.* - [0-9]* - (.*)\.mp3/\1/g")
}
function formatId3Tags () {
trackNumber=0
while IFS= read -d $'\0' -r file ; do
getTrackName "$(basename "$file")"
let trackNumber=trackNumber+1
# Check if in dry run mode
if [[ $run != 1 ]]; then
printf "Setting id3 v1 & v2 tags of \"$(basename "$file")\"\n"
printf "Artist: $artist\nAlbum: $album\nTrack Name: $trackName\nComment: ""\nDescription: ""\nYear: $year\nTrack Number: $trackNumber\nTrack Total: $trackTotal\nGenre: $genre\n\n"
# echo id3tag -a "\"$artist\"" -A "\"$album\"" -s "\"$trackName\"" -c "\"\"" -C "\"\"" -y "\"$year\"" -t "\"$trackNumber\"" -T "\"$trackTotal\"" -gSHORT "\"$genre\"" "\"$file\""
# In run mode
else
id3tag -a "$artist" -A "$album" -s "$trackName" -c "" -C "" -y "$year" -t "$trackNumber" -T "$trackTotal" -g "$genre" "$file"
fi
done < <(find "$path" -iname '*.mp3' -print0)
# Check if in dry run mode
if [[ $run != 1 ]]; then
echo "If the conversion looks good run command with -r switch."
# In run mode
else
echo "Tagging complete."
fi
}
read -p "Do you want to format id3 tags? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
formatId3Tags
fi
# # End of file