-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudiolist
executable file
·113 lines (93 loc) · 2.54 KB
/
audiolist
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
#!/bin/bash
#
# audiolist
# - Outputs a sorted list of all artists, genres, albums or bitrates for mp3 files.
# - Depends on Bash 4, id3info, id3v2.
#
# Process options.
while getopts ":f" opt; do
case $opt in
\? ) echo 'Usage: audiolist directory tag'
exit 1
esac
done
shift $((OPTIND - 1))
# Script definitions.
shopt -s globstar
IFS=$'\n'
input=${1:?'Read directory not specified. Usage: audiolist directory tag'}
tag=${2:?'Tag not specified. Usage: audiolist directory tag'}
case $tag in
"artist" ) id3tag='TPE1' ;;
"genre" ) id3tag='TCON' ;;
"album" ) id3tag='TALB' ;;
"bitrate" ) id3tag='Bitrate' ;;
* ) id3tag='TPE1' ;;
esac
# Check dependencies.
if [ ! -x '/usr/local/bin/id3info' ] || [ ! -x '/usr/local/bin/id3v2' ]; then
echo '> Error: Dependencies not met. id3lib and id3v2 are required.'
exit 2
fi
# Check to see that supplied directory exists.
if [ ! -d "$input" ]; then
echo '> Error: Could not read from directory.'
exit 3
fi
# Check if output directory exists.
if [ ! -d $OUTPUT ]; then
echo '> Error: Output directory is inacessible.'
exit 4
fi
# Verify Bash version dependency.
version=$(echo $BASH_VERSION | head -c 1)
if [[ $version < 4 ]]; then
echo '> Error: Dependencies not met. BASH Shell version must be greater than 4.0.'
exit 5
fi
# Strip quotes and brackets.
function strip {
echo $1 | tr -d "'\"[]"
}
declare -A results
files="$input"**/*.mp3
for file in $files
do
# Verify we actually found some files.
if [[ "$file" == "$files" ]]; then
echo "> Error: No accessible files in supplied directory."
exit 6
fi
id3data=$(id3info "$file")
declare -A id3array
# Generate an array of ID3 tag data.
for f in $id3data; do
regex="[={3}]*[ {1}]*([0-9A-Za-z]+)[0-9A-Za-z/ \(\)]*: (.*)"
[[ $f =~ $regex ]]
if [ ${BASH_REMATCH[1]} ]; then
id3array[${BASH_REMATCH[1]}]=${BASH_REMATCH[2]}
fi
done
value=$(strip ${id3array[$id3tag]})
# Push matches into array (reducing duplicates).
if [[ ${#value} != 0 ]]; then
results[$value]=$((results[$value] + 1))
fi
# If sorting by artist, include remix and featured artists.
if [[ $id3tag == 'TPE1' ]]; then
remix=$(strip $(echo ${id3array[TIT2]} | sed -n 's/^.* (\(.*\) Remix)/\1/p'))
feature=$(strip $(echo ${id3array[TIT2]} | sed -n 's/^.* (ft. \(.*\))/\1/p'))
if [[ ${#remix} != 0 ]]; then
results[$remix]=$((results[$remix] + 1))
fi
if [[ ${#feature} != 0 ]]; then
results[$feature]=$((results[$feature] + 1))
fi
fi
unset id3array
done
# Output results.
for result in "${!results[@]}"
do
echo -e "$result (${results[$result]})"
done | sort