-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflac-clean-tags.sh.in
executable file
·228 lines (188 loc) · 5.04 KB
/
flac-clean-tags.sh.in
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
#!/usr/bin/env bash
usage() {
local old_xtrace
old_xtrace="$(shopt -po xtrace || :)"
set +o xtrace
{
echo "${script_name} - Clean FLAC metadata tags using standard rules."
echo "Usage: ${script_name} [flags] top-dir"
echo "Option flags:"
echo " -a --and - Apply 'and The' conversions."
echo " -f --file - Apply 'filename' conversions."
echo " -h --help - Show this help and exit."
echo " -v --verbose - Verbose execution."
echo " -g --debug - Extra verbose execution."
echo " -d --dry-run - Dry run, don't modify files."
echo "Info:"
print_project_info
} >&2
eval "${old_xtrace}"
}
process_opts() {
local short_opts="afhvgd"
local long_opts="and,file,help,verbose,debug,dry-run"
local opts
opts=$(getopt --options ${short_opts} --long ${long_opts} -n "${script_name}" -- "$@")
eval set -- "${opts}"
while true ; do
# echo "${FUNCNAME[0]}: (${#}) '${*}'"
case "${1}" in
-a | --and)
clean_and_the=1
shift
;;
-f | --file)
clean_filename=1
shift
;;
-h | --help)
usage=1
shift
;;
-v | --verbose)
verbose=1
shift
;;
-g | --debug)
verbose=1
debug=1
set -x
shift
;;
-d | --dry-run)
dry_run=1
shift
;;
--)
shift
top_dir="${1:-}"
if [[ ${top_dir} ]]; then
shift
fi
extra_args="${*}"
break
;;
*)
echo "${script_name}: ERROR: Internal opts: '${*}'" >&2
exit 1
;;
esac
done
}
clean_tags() {
local file="${1}"
if ! flac_check_file "${file}" ''; then
return
fi
declare -A tags
declare -A new_tags
flac_fill_tag_set "${file}" tags
new_tags[ARTIST]="$(str_clean_tag "${tags[ARTIST]}" str_clean_tag_opts)"
new_tags[ALBUM]="$(str_clean_tag "${tags[ALBUM]}" str_clean_tag_opts)"
new_tags[TITLE]="$(str_clean_tag "${tags[TITLE]}" str_clean_tag_opts)"
new_tags[GENRE]="$(str_clean_genre "${tags[GENRE]}" str_clean_tag_opts)"
new_tags[TRACKNUMBER]="${tags[TRACKNUMBER]}"
new_tags[TRACKTOTAL]="${tags[TRACKTOTAL]}"
new_tags[CDDB]="${tags[CDDB]}"
local need_write=''
if [[ "${tags[ARTIST]}" != "${new_tags[ARTIST]}" ]]; then
need_write=1
if [[ ${verbose} ]]; then
echo -e "clean ARTIST: '${file}': '${tags[ARTIST]}' -> '${new_tags[ARTIST]}'" >&2
else
echo -e "clean ARTIST: '${tags[ARTIST]}' -> '${new_tags[ARTIST]}'" >&2
fi
fi
if [[ "${tags[ALBUM]}" != "${new_tags[ALBUM]}" ]]; then
need_write=1
if [[ ${verbose} ]]; then
echo -e "clean ALBUM: '${file}': '${tags[ALBUM]}' -> '${new_tags[ALBUM]}'" >&2
else
echo -e "clean ALBUM: '${tags[ALBUM]}' -> '${new_tags[ALBUM]}'" >&2
fi
fi
if [[ "${tags[TITLE]}" != "${new_tags[TITLE]}" ]]; then
need_write=1
if [[ ${verbose} ]]; then
echo -e "clean TITLE: '${file}': '${tags[TITLE]}' -> '${new_tags[TITLE]}'" >&2
else
echo -e "clean TITLE: '${tags[TITLE]}' -> '${new_tags[TITLE]}'" >&2
fi
fi
if [[ "${tags[GENRE]}" != "${new_tags[GENRE]}" ]]; then
need_write=1
if [[ ${verbose} ]]; then
echo -e "clean GENRE: '${file}': '${tags[GENRE]}' -> '${new_tags[GENRE]}'" >&2
else
echo -e "clean GENRE: '${tags[GENRE]}' -> '${new_tags[GENRE]}'" >&2
fi
fi
if [[ ${debug} ]]; then
print_tag_set "${FUNCNAME[0]}: IN: " "${file}" tags
if [[ ${need_write} ]]; then
print_tag_set "${FUNCNAME[0]}: OUT: " "${file}" new_tags
fi
fi
if [[ ${need_write} && ! ${dry_run} ]]; then
flac_write_tag_set "${file}" new_tags
if [[ ${verbose} ]]; then
echo -e "${FUNCNAME[0]}: Wrote '${file}'" >&2
fi
fi
}
#===============================================================================
export PS4='\[\e[0;33m\]+ ${BASH_SOURCE##*/}:${LINENO}:(${FUNCNAME[0]:-main}):\[\e[0m\] '
script_name="${0##*/}"
SECONDS=0
start_time="$(date +%Y.%m.%d-%H.%M.%S)"
real_source="$(realpath "${BASH_SOURCE}")"
SCRIPT_TOP="$(realpath "${SCRIPT_TOP:-${real_source%/*}}")"
trap "on_exit 'Failed'" EXIT
trap 'on_err ${FUNCNAME[0]:-main} ${LINENO} ${?}' ERR
trap 'on_err SIGUSR1 ? 3' SIGUSR1
set -eE
set -o pipefail
set -o nounset
source "${SCRIPT_TOP}/audx-lib.sh"
clean_and_the=''
clean_filename=''
usage=''
verbose=''
debug=''
dry_run=''
process_opts "${@}"
if [[ -f "${HOME}/.audx.conf" ]]; then
source "${HOME}/.audx.conf"
fi
if [[ ${usage} ]]; then
usage
trap - EXIT
exit 0
fi
if [[ ${extra_args} ]]; then
set +o xtrace
echo "${script_name}: ERROR: Got extra args: '${extra_args}'" >&2
usage
exit 1
fi
check_top_dir "${top_dir}"
top_dir="$(realpath -e "${top_dir}")"
metaflac="${metaflac:-metaflac}"
check_program "metaflac" "${metaflac}"
declare -A str_clean_tag_opts=()
if [[ ${clean_and_the} ]]; then
str_clean_tag_opts[and_the]='1'
fi
if [[ ${clean_filename} ]]; then
str_clean_tag_opts[filename]='1'
fi
readarray -t file_array < <( find "${top_dir}" -type f -name '*.flac' | sort \
|| { echo "${script_name}: ERROR: files_array find failed, function=${FUNCNAME[0]:-main}, line=${LINENO}, result=${?}" >&2; \
kill -SIGUSR1 $$; } )
echo "${script_name}: INFO: Processing ${#file_array[@]} files." >&2
for (( i = 0; i < ${#file_array[@]}; i++ )); do
# echo "process: '${file_array[i]}'"
clean_tags "${file_array[i]}"
done
trap "on_exit 'Success'" EXIT
exit 0