aboutsummaryrefslogtreecommitdiffstats
path: root/unix/id3tag.sh
blob: 93cd099e8e93e5af51b1cb2bddb53aa34a533888 (plain)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/bin/sh
#
# MP3 id3tag functions
#
# Copyright (C) 2005 Robert, Zhang Le
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


# INTRO:
#	 This file contains several functions related to mp3 file's operations.
#	 I wrote them in order to ease the process of transforming the encoding 
#	 of mp3 file's id3tag from gbk to utf8. YOU NEED id3v2 to make these 
#	 functions work.
#    !!! USE THESE FUNCTIONS IN UTF-8 LOCALE !!!

# TODO: 
#	1. make all the functions work both on a directory and a file
#	2. add testing feature to all functions
#      * rename_delete_ending_artist()
#      * check_artist()
#	3. set_artist() maybe need to be improved
#   4. if functions are called without params, echo "usage"

# USAGE:
#	1. source this file before use


# get_title_1() can be used to get v1 title from an mp3 file's id3tag
# get_title_1_convernted() convert its encoding from gbk to utf8

get_title_1(){
	id3v2 -l "$1" | grep ^Title \
	| sed -e 's/^Title  : //g' -e 's/[[:space:]]*Artist: [[:print:]]*//g'
}
get_title_1_converted(){
	id3v2 -l "$1" | grep ^Title \
	| sed -e 's/^Title  : //g' -e 's/[[:space:]]*Artist: [[:print:]]*//g' \
	| iconv -f gbk -t utf8 
}

# ATTENTION: if title encoding is gbk, the following function does not
#            function ideally, because "." can't match invisible/unmappd char?
get_artist_1(){
	id3v2 -l "$1" | grep ^Title \
	| sed -e  's/^Title.*Artist: //g' 
}

get_title_2(){
	id3v2 -l "$1" | grep ^TIT2 | tail -n 1 | sed -e 's/^.*: //g'
}
get_title_2_converted(){
	id3v2 -l "$1" | grep ^TIT2 | tail -n 1 | sed -e 's/^.*: //g' \
	| iconv -f gbk -t utf8 

}
get_artist_2(){
	id3v2 -l "$1" | grep ^TPE1 | tail -n 1 | sed -e 's/^.*: //g'
}


# if only one param, it must be a dirname, and at the same time an artist's name
# if two param, the first artist name, the second dirname
set_artist(){ 
	local artist 

	if [[ $# -eq 0 ]]; then
		echo "set_artist artist [dir]"
		return;
	else
		artist="$1";
	fi

	if [[ $# -eq 2 ]]; then
		if [[ -d $2 ]]; then
			shift;
		else
			echo "the 2nd param must be a dir"
			return;
		fi
	elif [[ $# -eq 1 ]]; then
		if [[ ! -d $1 ]]; then
			echo "the sole param must be also a dir"
			return;
		fi
	fi

	find "$1" -name "*.mp3" -print0 | xargs -0 id3v2 -a "$artist" ;

}

id3_convert_set_title(){
	local target
	local title
	local testing 
	local tmp
	local i

	if [[ $# -eq 0 ]]; then
		echo "needs parameters"
		return;
	fi

	if [[ $1 == "notest" ]]; then
		testing=0;
		shift;
	else
		testing=1;
	fi


	while [[ $# -gt 0 ]]; do
		if [[ -d "$1" ]]; then
			for i in "$1"/*.mp3; 
			do 
				title=`get_title_1_converted "$i"`

				# if the last command, ie. iconv fails
				# then probably the file's id3tag has already been converted
				if [[ $? -ne 0 ]]; then
					continue;
				fi

				tmp=`echo -n "$title" | sed -e 's/Track//g' -e 's/[[:space:][:digit:]]*//g'`

				if [[ -z $tmp ]]; then
					title=`get_title_2_converted "$i"`

					if [[ $? -ne 0 ]]; then
						continue;
					fi

					tmp=`echo -n "$title" | sed -e 's/Track//g' -e 's/[[:space:][:digit:]]*//g'`

					if [[ -z $tmp ]]; then
						echo "title is "$tmp" nothing need to be done"
						continue;
					fi
				fi

				if [[ -n $title ]]; then
					if [[ $testing -eq 1 ]]; then
						echo $title;
					else
						echo -n $title;
						id3v2 -1 -t "$title" "$i"; 
						id3v2 -2 -t "$title" "$i"; 
						echo " done";
					fi
				fi
			done
		elif [[ -f "$1" ]]; then
			title=`get_title_1_converted "$1"`
			if [[ $? -ne 0 ]]; then
				shift;
				continue;
			fi

			tmp=`echo -n "$title" | sed -e 's/Track//g' -e 's/[[:space:][:digit:]]*//g'`

			if [[ -z $tmp ]]; then
				title=`get_title_2_converted "$1"`

				if [[ $? -ne 0 ]]; then
					continue;
				fi

				tmp=`echo -n "$title" | sed -e 's/Track//g' -e 's/[[:space:][:digit:]]*//g'`

				if [[ -z $tmp ]]; then
					echo "title is "$tmp" nothing need to be done"
					shift;
					continue;
				fi
			fi

			if [[ -n $title ]]; then
				if [[ $testing -eq 1 ]]; then
					echo $title;
				else
					echo -n $title;
					id3v2 -1 -t "$title" "$1"; 
					id3v2 -2 -t "$title" "$1"; 
					echo " done";
				fi
			fi
		fi
		shift;
	done

	if [[ $testing -eq 1 ]]; then
		echo "testing finished, to convert use \"notest\" as the fist param"
	fi
}

# make sure file name ends with ".mp3"
id3_set_title_as_filename(){
	local title 
	local testing
	local i

	if [[ $# -eq 0 ]]; then
		echo "needs parameters";
		return;
	fi

	if [[ $1 == "notest" ]]; then
		testing=0;
		shift;
	else
		testing=1;
	fi

	while [[ $# -gt 0 ]]; do
		if [[ -d "$1" ]]; then
			for i in "$1"/*.mp3; do 
				title=`basename "$i" | sed -e 's/\.mp3//g'`	
				if [[ $testing -eq 1 ]]; then
					echo $title;
				else
					echo -n $title;
					id3v2 -1  -t "${title}" "$i";
					id3v2 -2  -t "${title}" "$i";
					echo " done";
				fi
			done 
		elif [[ -f "$1" ]]; then
			title=`basename "$1" | sed -e 's/\.mp3//g'`
			if [[ $testing -eq 1 ]]; then
				echo $title;
			else
				echo -n $title;
				id3v2 -1 -t "$title" "$1"; 
				id3v2 -2 -t "$title" "$1"; 
				echo " done";
			fi
		fi
		shift;
	done

	if [[ $testing -eq 1 ]]; then
		echo "testing finished, to convert use \"notest\" as the fist param"
	fi
}

id3_rename_MP3_to_mp3(){
	local target
	local i

	if [[ $# -eq 0 ]]; then
		echo "needs parameters";
		return;
	fi

	while [[ $# -gt 0 ]]; do
		if [[ -d "$1" ]]; then
			for i in "$1"/*.MP3; do
				target="$1"/`basename "$i" | sed -e 's/MP3$/mp3/g'`
				if [[ "$i" != "$target" ]]; then
					mv "$i" "$target"; 
					echo "$i -> $target: done"
				fi
			done
		elif [[ -f "$1" ]]; then
			target=`dirname "$1"`/`basename "$1" | sed -e 's/MP3$/mp3/g'`
			if [[ "$1" != "$target" ]]; then
				mv "$1" "$target"; 
				echo "$1 -> $target: done"
			fi
		fi
		shift;
	done
}

# use this function to remove leading garbages in the file name
# Sometimes, the file name in the form of "artist -(1). title.mp3"
# we just need title.mp3
# the delim is the adjacent char before "title"

id3_rename_delete_leading_garbage(){
	local target
	local testing
	local orig="^0[0-9]\."
	#local orig="^[[:alnum:]]*[-.()[:digit:][:space:]]*"
	local delim="[[:space:]]"
	# ATTENTION: if delimeter is  ".",  delim should be  "\."
	#            when passing on command line, it should be "\\\."
	local new=""
	local i

	if [[ $# -eq 0 ]]; then
		echo "needs parameters";
		return;
	fi

	if [[ $1 == "notest" ]]; then
		testing=0;
		shift;
	else
		testing=1;
	fi

	if [[ "${1/=*/}" == "delim" ]]; then
		delim="${1/*=/}";
		echo $delim;
		shift;
	fi

	while [[ $# -gt 0 ]]; do
		if [[ -d "$1" ]]; then
			echo $1
			for i in "$1"/*.mp3; do
				target="$1"/`basename "$i" | sed -e "s/$orig$delim/$new/g"`	
				if [[ "$i" != "$target" ]]; then
					echo -n "$target";
					if [[ $testing -eq 0 ]]; then
						mv "$i" "$target"; 
					fi
					echo " done"
				else
					echo "$target";
				fi
			done
		elif [[ -f "$1" ]]; then
			target=`dirname "$1"`/`basename "$1" | sed -e "s/$orig$delim/$new/g"`	
			if [[ "$1" != "$target" ]]; then
				echo -n "$target";
				if [[ $testing -eq 0 ]]; then
					mv "$1" "$target"; 
				fi
				echo " done";
			else
				echo $target;
			fi
		fi
		shift;
	done

	if [[ $testing -eq 1 ]]; then
		echo "testing finished, to convert use \"notest\" as the fist param"
	fi
}

# use this function after conversion
id3_rename_as_title(){
	local target
	local title
	local testing
	local i

	if [[ $# -eq 0 ]]; then
		echo "needs parameters";
		return;
	fi

	if [[ $1 == "notest" ]]; then
		testing=0;
		shift;
	else
		testing=1;
	fi

	while [[ $# -gt 0 ]]; do
		if [[ -d "$1" ]]; then
			for i in "$1"/*.mp3; do
				title=`get_title_1 "$(basename $i)"`

				# if the last command, ie. iconv fails
				# then probably the file's id3tag has already been converted
				if [[ $? -ne 0 ]]; then
					continue;
				fi

				title=`echo -n "$title" | sed -e 's/[[:space:][:punct:]]*//g'`

				if [[ -z $title ]]; then
					title=`get_title_2_converted "$(basename $i)"`
					if [[ $? -ne 0 ]]; then
						continue;
					fi
				fi

				target="$1"/`get_title_2 "$i"`.mp3

				if [[  -a "$target" ]]; then
					echo "$target exists, skiping"
					continue;
				fi

				if [[ "$i" != "$target" ]] ; then
					if [[ $testing -eq 0 ]]; then
						mv "$i" "$target"; 
					fi
					echo "$i -> $target: done";
				fi
			done
		elif [[ -f "$1" ]]; then
			target=`dirname "$1"`/`get_title_2 "$1"`.mp3

			if [[  -a "$target" ]]; then
				echo "$target exists, skiping"
				shift;
				continue;
			fi

			if [[ "$1" != "$target" ]]; then
				if [[ $testing -eq 0 ]]; then
					mv "$1" "$target"; 
				fi
				echo "$1 -> $target: done";
			fi
		fi
		shift;
	done

	if [[ $testing -eq 1 ]]; then
		echo "testing finished, to convert use \"notest\" as the fist param"
	fi
}

id3_check_title(){
	local target
	local i
	local title1
	local title2

	if [[ $# -eq 0 ]]; then
		echo "needs parameters";
		return;
	fi

	while [[ $# -gt 0 ]]; do
		if [[ -d "$1" ]]; then
			echo $1
			for i in "$1"/*.mp3
			do
				title1=`get_title_1 "$i"`;
				title2=`get_title_2 "$i"`;
				printf "%s   \tv1: %s   \tv2: %s\n" "$i" "$title1" "$title2";
			done
		elif [[ -f "$1" ]]; then
			title1=`get_title_1 "$1"`;
			title2=`get_title_2 "$1"`;
			printf "%s   \tv1: %s   \tv2: %s\n" "$1" "$title1" "$title2";

		fi
		shift;
	done
}

id3_check_title_converted(){
	local target
	local i
	local title1
	local title2

	if [[ $# -eq 0 ]]; then
		echo "needs parameters";
		return;
	fi

	while [[ $# -gt 0 ]]; do
		if [[ -d "$1" ]]; then
			echo $1
			for i in "$1"/*.mp3
			do
				title1=`get_title_1_converted "$i"`;
				title2=`get_title_2_converted "$i"`;
				printf "%s   \tv1: %s   \tv2: %s\n" "$i" "$title1" "$title2";
			done
		elif [[ -f "$1" ]]; then
			title1=`get_title_1_converted "$1"`;
			title2=`get_title_2_converted "$1"`;
			printf "%s   \tv1: %s   \tv2: %s\n" "$1" "$title1" "$title2";

		fi
		shift;
	done
}

id3_check_artist(){
	for i in *.mp3;
	do
		artist1=`get_artist_1 "$i"`;
		artist2=`get_artist_2 "$i"`;
		printf "%s   \tv1: %s   \tv2: %s\n" "$i" "$artist1" "$artist2";
	done
}
# if filename ends with "_artist", use this one to remove the suffix
id3_rename_delete_ending_artist(){

	if [[ $# -eq 0 ]]; then
		echo "needs parameters";
		return;
	fi

	for i in "$1"/*.mp3; do
		mv "$i" "${i/_*./.}";
	done
}