-
Notifications
You must be signed in to change notification settings - Fork 1
/
mp3tag
executable file
·68 lines (53 loc) · 1.75 KB
/
mp3tag
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
#!/bin/bash
# Copy filename in the form of "artist - title.mp3" to mp3 tags,
# or vice versa.
#
# mp3tag {-t | -f} filename [filename ...]
EYED3=$(which eyed3 2>/dev/null)$(which eyeD3 2>/dev/null)
function usage {
echo "mp3tag {-t | -f} [-a album] filename [filename ...]"
}
while getopts "tfa:" OPT; do
case $OPT in
t ) TO_TAG=1 ;;
f ) TO_FN=1 ;;
a ) ALBUM=$OPTARG ;;
esac
done
[[ !($TO_TAG || $TO_FN) || ($TO_TAG && $TO_FN) ]] && {
usage;
echo "Please specify one of -t (write tags), -f (write filename)." >&2
exit 1;
}
[[ -n "$ALBUM" ]] && ALBUMOPTS="-A \"$ALBUM\""
while [[ $1 ]]; do
FN=$(echo $1 | egrep '\.mp3$'); shift
[[ ! "$FN" ]] && continue
# Write tags based on filename
if [ $TO_TAG ]; then
[ -w "$FN" ] || {
echo "$FN is not writable!" >&2
continue;
}
ARTIST=$(echo "$FN" | perl -nle '/^(.*?)-/; $a = $1; $a =~ s/(^\s+|\s+$)//g; print $a;')
TITLE=$(echo "$FN" | perl -nle '/^.*?-(.*?)\.mp3$/; $a = $1; $a =~ s/(^\s+|\s+$)//g; print $a;')
if [[ "$TITLE" && "$ARTIST" ]]; then
CMD="$EYED3 --no-color -a \"$ARTIST\" -t \"$TITLE\""
[[ -n "$ALBUMOPTS" ]] && CMD="$CMD $ALBUMOPTS"
eval "$CMD \"$FN\"" >/dev/null
else
echo "Cannot write tag for $FN!" >&2
fi
# Write filename based on tags
elif [ $TO_FN ]; then
TA=$($EYED3 --no-color "$FN" 2>/dev/null | egrep 'title\:.*artist\:')
TITLE=$(echo "$TA" | perl -nle '/title\: (.*?)artist\:/; $a = $1; $a =~ s/(^\s+|\s+$)//g; print $a;')
ARTIST=$(echo "$TA" | perl -nle '/title\: .*?artist\:(.*)$/; $a = $1; $a =~ s/(^\s+|\s+$)//g; print $a;')
if [[ "$TITLE" && "$ARTIST" ]]; then
mv "$FN" "$ARTIST - $TITLE.mp3"
echo "Renamed \"$FN\" to \"$ARTIST - $TITLE.mp3\"" >&2
else
echo "Cannot rename $FN!" >&2
fi
fi
done