-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathset-replaygain
executable file
·415 lines (362 loc) · 11.4 KB
/
set-replaygain
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
#!/bin/bash
# SPDX-FileCopyrightText: 2020 - 2024 sudorook <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# 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 3 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, see <https://www.gnu.org/licenses/>.
set -Eeuo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
#
# Check inputs and requirements
#
function compute_bs1770gain {
! check_command bs1770gain && exit 3
local item
if [ "${#}" -eq 1 ]; then
bs1770gain "${@}" -o "${TMPDIR}"
if [[ "${KEEP}" = true ]]; then
mkdir -p "${BACKUPDIR}"
mv "${1}" "${BACKUPDIR}"
mv "${TMPDIR}" "${1}"
else
mv "${1}" "${1}.bak"
mv "${TMPDIR}" "${1}"
rm -r "${1}.bak"
fi
else
for item in "${@}"; do
mkdir -p "${TMPDIR}/$(basename "${item}")"
bs1770gain "${item}" -o "${TMPDIR}/$(basename "${item}")"
if [[ "${KEEP}" = true ]]; then
mkdir -p "${BACKUPDIR}/$(basename "${item}")"
mv "${item}" "${BACKUPDIR}/$(basename "${item}")"
mv "${TMPDIR}/$(basename "${item}")" "${item}"
else
mv "${item}" "${item}.bak"
mv "${TMPDIR}/$(basename "${item}")" "${item}"
rm -r "${item}.bak"
fi
done
rmdir "${TMPDIR}"
fi
sync
}
function compute_r128gain {
! check_command r128gain && exit 3
local item
if [ "${#}" -eq 1 ]; then
if [[ "${KEEP}" = true ]]; then
cp -r "${1}" "${BACKUPDIR}"
r128gain -a -r "${1}"
else
r128gain -a -r "${1}"
fi
else
for item in "${@}"; do
if [[ "${KEEP}" = true ]]; then
mkdir -p "${BACKUPDIR}/"
cp -r "${item}" "${BACKUPDIR}/$(basename "${item}")"
r128gain -a -r "${item}"
else
r128gain -a -r "${item}"
fi
done
fi
sync
}
function scan_track_loudnorm {
local data
local peak
local i
data="$(ffmpeg -v info -nostdin -nostats -hide_banner -i file:"${1}" -af loudnorm=print_format=json -f null - 2>&1 |
sed -n '/^{/,/}$/p')"
peak="$(echo "${data}" | jq -r ".input_tp")"
i="$(echo "${data}" | jq -r ".input_i")"
echo "${peak}|${i}"
}
export -f scan_track_loudnorm
function scan_album_loudnorm {
local data
local peak
local i
local file
local cmd
local count=0
cmd="ffmpeg -v info -nostdin -nostats -hide_banner"
while read -r file; do
cmd="${cmd} -i file:${file@Q}"
count=$((count + 1))
done <<< "$(find "${@}" -type f \( -iname "*.mp3" -o -iname "*.flac" -o -iname "*.opus" -o -iname "*.aac" \))"
cmd="${cmd} -filter_complex 'concat=n=${count}:v=0:a=1,loudnorm=print_format=json' -f null - 2>&1 | sed -n '/^{/,/}$/p'"
data="$(eval "${cmd}")"
peak="$(echo "${data}" | jq -r ".input_tp")"
i="$(echo "${data}" | jq -r ".input_i")"
echo "${peak}|${i}"
}
export -f scan_album_loudnorm
function tag_track_loudnorm {
local file
local replaygain_album_gain
local replaygain_album_peak
local replaygain_track_gain
local replaygain_track_peak
local track_data
local codec
local tmp
tmp="$(mktemp)"
file="${1}"
replaygain_album_gain="${2}"
replaygain_album_peak="${3}"
track_data="$(scan_track_loudnorm "${file}")"
codec="$(ffprobe -v error -select_streams a -show_entries stream=codec_name -of compact=nokey=1 file:"${file}")"
codec="$(echo "${codec}" | cut -d"|" -f2)"
replaygain_track_peak="$(echo "${track_data}" | cut -d"|" -f1)"
replaygain_track_gain="$(echo "${track_data}" | cut -d"|" -f2)"
replaygain_track_peak="$(echo "scale=6; e(l(10)*${replaygain_track_peak}/20)" | bc -l | sed 's/^\./0./')"
replaygain_track_gain="$(echo "scale=2; -18-(${replaygain_track_gain}/1)" | bc -l)"
show_info "Tagging ${file@Q}..." >&2
ffmpeg -v error -nostdin -nostats -hide_banner -i file:"${file}" -map 0 -map_metadata 0 \
-metadata REPLAYGAIN_TRACK_GAIN="${replaygain_track_gain} dB" \
-metadata REPLAYGAIN_ALBUM_GAIN="${replaygain_album_gain} dB" \
-metadata REPLAYGAIN_TRACK_PEAK="${replaygain_track_peak}" \
-metadata REPLAYGAIN_ALBUM_PEAK="${replaygain_album_peak}" \
-c copy -f "${codec}" -y "${tmp}"
mv "${tmp}" "${file}"
}
export -f tag_track_loudnorm
function compute_loudnormgain {
! check_command ffmpeg ffprobe find sed jq bc && exit 3
local item
if [[ "${KEEP}" = true ]]; then
if [ "${#}" -eq 1 ]; then
cp -ra "${1}" "${BACKUPDIR}"
else
for item in "${@}"; do
mkdir -p "${BACKUPDIR}/"
cp -ra "${item}" "${BACKUPDIR}/$(basename "${item}")"
done
fi
fi
local album_data
local replaygain_album_gain
local replaygain_album_peak
show_info "Calculating album gain..." >&2
album_data="$(scan_album_loudnorm "${@}")"
replaygain_album_peak="$(echo "${album_data}" | cut -d"|" -f1)"
replaygain_album_gain="$(echo "${album_data}" | cut -d"|" -f2)"
replaygain_album_peak="$(echo "scale=6; e(l(10)*${replaygain_album_peak}/20)" | bc -l | sed 's/^\./0./')"
replaygain_album_gain="$(echo "scale=2; -18-(${replaygain_album_gain}/1)" | bc -l)"
find "${@}" -type f \
\( -iname "*.aac" -o \
-iname "*.alac" -o \
-iname "*.flac" -o \
-iname "*.mka" -o \
-iname "*.mp3" -o \
-iname "*.opus" -o \
-iname "*.wav" \) -print0 |
xargs -0 -P "$(nproc)" -I{} bash -c 'tag_track_loudnorm "{}" '"${replaygain_album_gain} ${replaygain_album_peak}"
sync
}
function scan_track_ebur128 {
local data
local peak
local i
data="$(ffmpeg -v info -nostdin -nostats -hide_banner -i file:"${1}" -af ebur128=peak=true -f null - 2>&1)"
peak="$(echo "${data}" |
tail -n 14 |
sed -n '/^\s\+True peak:/{N;s/.*\s\+Peak:\s\+\([+0-9\.-]\+\) dBFS/\1/p}')"
i="$(echo "${data}" |
tail -n 14 |
sed -n '/^\s\+Integrated loudness:/{N;s/.*\s\+I:\s\+\([+0-9\.-]\+\) LUFS/\1/p}')"
echo "${peak}|${i}"
}
export -f scan_track_ebur128
function scan_album_ebur128 {
local data
local peak
local i
local file
local cmd
local count=0
cmd="ffmpeg -v info -nostdin -nostats -hide_banner"
while read -r file; do
cmd="${cmd} -i file:${file@Q}"
count=$((count + 1))
done <<< "$(find "${@}" -type f \( -iname "*.mp3" -o -iname "*.flac" -o -iname "*.opus" -o -iname "*.aac" \))"
cmd="${cmd} -filter_complex 'concat=n=${count}:v=0:a=1,ebur128=peak=true' -f null - 2>&1"
data="$(eval "${cmd}")"
peak="$(echo "${data}" |
tail -n 14 |
sed -n '/^\s\+True peak:/{N;s/.*\s\+Peak:\s\+\([+0-9\.-]\+\) dBFS/\1/p}')"
i="$(echo "${data}" |
tail -n 14 |
sed -n '/^\s\+Integrated loudness:/{N;s/.*\s\+I:\s\+\([+0-9\.-]\+\) LUFS/\1/p}')"
echo "${peak}|${i}"
}
export -f scan_album_ebur128
function tag_track_ebur128 {
local file
local replaygain_album_gain
local replaygain_album_peak
local replaygain_track_gain
local replaygain_track_peak
local track_data
local codec
local tmp
tmp="$(mktemp)"
file="${1}"
replaygain_album_gain="${2}"
replaygain_album_peak="${3}"
track_data="$(scan_track_ebur128 "${file}")"
codec="$(ffprobe -v error -select_streams a -show_entries stream=codec_name -of compact=nokey=1 file:"${file}")"
codec="$(echo "${codec}" | cut -d"|" -f2)"
replaygain_track_peak="$(echo "${track_data}" | cut -d"|" -f1)"
replaygain_track_gain="$(echo "${track_data}" | cut -d"|" -f2)"
replaygain_track_peak="$(echo "scale=6; e(l(10)*${replaygain_track_peak}/20)" | bc -l | sed 's/^\./0./')"
replaygain_track_gain="$(echo "scale=2; -18-(${replaygain_track_gain}/1)" | bc -l)"
show_info "Tagging ${file@Q}..." >&2
ffmpeg -v error -nostdin -nostats -hide_banner -i file:"${file}" -map 0 -map_metadata 0 \
-metadata REPLAYGAIN_TRACK_GAIN="${replaygain_track_gain} dB" \
-metadata REPLAYGAIN_ALBUM_GAIN="${replaygain_album_gain} dB" \
-metadata REPLAYGAIN_TRACK_PEAK="${replaygain_track_peak}" \
-metadata REPLAYGAIN_ALBUM_PEAK="${replaygain_album_peak}" \
-c copy -f "${codec}" -y "${tmp}"
mv "${tmp}" "${file}"
}
export -f tag_track_ebur128
function compute_ebur128gain {
! check_command bc ffmpeg ffprobe find sed && exit 3
local item
if [[ "${KEEP}" = true ]]; then
if [ "${#}" -eq 1 ]; then
cp -ra "${1}" "${BACKUPDIR}"
else
for item in "${@}"; do
mkdir -p "${BACKUPDIR}/"
cp -ra "${item}" "${BACKUPDIR}/$(basename "${item}")"
done
fi
fi
local album_data
local replaygain_album_gain
local replaygain_album_peak
show_info "Calculating album gain..." >&2
album_data="$(scan_album_ebur128 "${@}")"
replaygain_album_peak="$(echo "${album_data}" | cut -d"|" -f1)"
replaygain_album_gain="$(echo "${album_data}" | cut -d"|" -f2)"
replaygain_album_peak="$(echo "scale=6; e(l(10)*${replaygain_album_peak}/20)" | bc -l | sed 's/^\./0./')"
replaygain_album_gain="$(echo "scale=2; -18-(${replaygain_album_gain}/1)" | bc -l)"
find "${@}" -type f \
\( -iname "*.aac" -o \
-iname "*.alac" -o \
-iname "*.flac" -o \
-iname "*.mka" -o \
-iname "*.mp3" -o \
-iname "*.opus" -o \
-iname "*.wav" \) -print0 |
xargs -0 -P "$(nproc)" -I{} bash -c 'tag_track_ebur128 "{}" '"${replaygain_album_gain} ${replaygain_album_peak}"
sync
}
function check_inputs {
local input
if ! [ "${#}" -ge 0 ]; then
show_error "ERROR: no inputs given. Exiting."
fi
for input in "${@}"; do
if ! [ -e "${input}" ]; then
show_error "ERROR: ${input@Q} not found. Exiting."
exit 3
fi
done
}
function print_usage {
show_header "Usage: set-replaygain <file(s) or director(ies)>"
show_listitem "\
-k|--keep flag to keep original files/directories (default: false)
-g|--gain loudness scanner (bs1770gain, r128gain, loudnorm, or *ebur128*)
-h|--help print (this) help message"
}
#
# Main
#
KEEP=false
GAIN=ebur128
OPTIONS=khg:
LONGOPTIONS=keep,help,gain:
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ "${#}" -ge 1 ]; do
case "${1}" in
-k | --keep)
KEEP=true
shift 1
;;
-g | --gain)
GAIN="${2}"
shift 2
;;
-h | --help)
print_usage
exit
;;
--)
shift
break
;;
*)
show_error "ERROR: invalid flag."
exit 3
;;
esac
done
TMPDIR="$(mktemp -d)"
trap 'rm -rf "${TMPDIR}"; exit' INT TERM ERR
# Check that input files/directories exist before computing replay gain.
check_inputs "${@}"
# Generate name for backup directory.
if [[ "${KEEP}" = true ]]; then
if [ "${#}" -eq 1 ]; then
if [ -d "${1}" ]; then
if [[ "${1}" = "." ]]; then
BACKUPDIR="../$(basename "${PWD}")_$(date +%Y%m%d-%H%M%S)"
else
BACKUPDIR="${1}_$(date +%Y%m%d-%H%M%S)"
fi
else
show_warning "Why compute album replay gain for one file? Stopping..."
exit
fi
else
BACKUPDIR="backup_$(date +%Y%m%d-%H%M%S)"
fi
fi
case "${GAIN}" in
bs1770gain)
compute_bs1770gain "${@}"
;;
r128gain)
compute_r128gain "${@}"
;;
ebur128gain | ebur128)
compute_ebur128gain "${@}"
;;
loudnormgain | loudnorm)
compute_loudnormgain "${@}"
;;
*)
show_error "ERROR: ${GAIN@Q} not supported. Exiting."
exit 3
;;
esac