-
Notifications
You must be signed in to change notification settings - Fork 2
/
rename-tracks
executable file
·68 lines (57 loc) · 1.11 KB
/
rename-tracks
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
#!/bin/sh
usage()
{
cat >&2 <<__EOF__
Usage: $(basename $0) [-n]
Arguments:
-n -- Dry run mode. Don't actually modify any files.
__EOF__
exit ${1-1}
}
log()
{
echo "$(basename $0): $1" >&2
}
digits()
{
local i count
i=0
count=$1
while [ $count -gt 0 ]; do
count=$(($count / 10))
i=$((i + 1))
done
echo $i
}
while [ $# -gt 0 ]; do
case $1 in
-n)
dryrun=yes
;;
*)
usage
;;
esac
shift
done
: ${dryrun=no}
if ! which audget >/dev/null 2>&1; then
log "audget is not available"
exit 1
fi
OLDIFS=$IFS
IFS='
'
count=$(digits $(find . -depth 1 -name \*.mp3 -o -name \*.flac | wc -l))
for file in $(find . -depth 1 -name \*.mp3 -o -name \*.flac); do
track=$(audget --track "$file" | awk '{print $NF}')
if [ $? -ne 0 ]; then
log "failed to obtain track number for '$file', skipping"
continue
fi
ext=${file##*.}
new=track$(printf "%0${count}d" $track).cdda.$ext
log "renaming '"$(basename "$file")"' to '$new'"
[ $dryrun = no ] && mv "$file" "$new"
done
IFS=$OLDIFS