forked from vincetse/shellutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
naud.sh
710 lines (623 loc) · 19.3 KB
/
naud.sh
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#!/bin/bash
###################################################################
# naud - Normalize mp3, ogg and flac files
# (c) Copyright - 2013 Geoff Clements
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
###################################################################
###################################################################
# Two useful web pages about quality settings
# http://lame.cvs.sourceforge.net/viewvc/lame/lame/doc/html/usage.html
# http://www.vorbis.com/faq/#quality
###################################################################
###################################################################
# Quality setting when encoding from flacs
# Feel free to change to your liking
###################################################################
LAME_DEF_QUAL="-V 0"
OGG_DEF_QUAL="-q 6"
###################################################################
# O traveller stray not beyond this point - here be dragons!
###################################################################
shopt -u nullglob
# Set up temporary directory
TMPDIR=${TMP:-/tmp}
WDIRS=() # Cache of all temp dirs, used for cleanup
trap "{ cleanup ; exit 255 ;}" INT TERM
trap "{ cleanup ; exit 0 ;}" EXIT
TARGET=same # keep encoded files the same type as the decoded ones
TARGET_OPTS="same ogg vorbis mp3 flac"
#########################################
# Cleanup functions
#########################################
cleanup () {
for tdir in "${WDIRS[@]}"
do
rm -r "${tdir}" 2>/dev/null
done
job_pool_shutdown
}
die () {
echo "$1"
cleanup
exit 1
}
#########################################
# Find programs which we need
# Can't do without these
#########################################
SOX=$(which sox) || die "Cannot find sox"
SOXI=$(which soxi) || die "Cannot find soxi"
NORM=$(which normalize-audio) || \
NORM=$(which normalize) || die "Cannot find normalize-audio"
LAME=$(which lame) || die "Cannot find lame"
OGGENC=$(which oggenc) || die "Cannot find oggenc"
FLAC=$(which flac) || die "Cannot find flac"
numprocs=$(nproc 2>/dev/null)
if [ ${#numprocs} -eq 0 ]
then
numprocs=$(grep -c "^core id" /proc/cpuinfo)
fi
#########################################
# Job Pool
# Copyright (c) 2012 Vince Tse
# with changes by Geoff Clements
#########################################
# end-of-jobs marker
job_pool_end_of_jobs="JOBPOOL_END_OF_JOBS"
# job queue used to send jobs to the workers
job_pool_job_queue=/tmp/job_pool_job_queue_$$
# where to run results to
job_pool_result_log=/tmp/job_pool_result_log_$$
# toggle command echoing
job_pool_echo_command=0
# number of parallel jobs allowed. also used to determine if job_pool_init
# has been called when jobs are queued.
job_pool_pool_size=-1
# \brief variable to check for number of non-zero exits
job_pool_nerrors=0
################################################################################
# private functions
################################################################################
# \brief debug output
function _job_pool_echo()
{
if [[ "${job_pool_echo_command}" == "1" ]]; then
echo $@
fi
}
# \brief cleans up
function _job_pool_cleanup()
{
rm -f ${job_pool_job_queue} ${job_pool_result_log}
}
# \brief signal handler
function _job_pool_exit_handler()
{
_job_pool_stop_workers
_job_pool_cleanup
}
# \brief print the exit codes for each command
# \param[in] result_log the file where the exit codes are written to
function _job_pool_print_result_log()
{
job_pool_nerrors=$(grep ^ERROR "${job_pool_result_log}" | wc -l)
cat "${job_pool_result_log}" | sed -e 's/^ERROR//'
}
# \brief the worker function that is called when we fork off worker processes
# \param[in] id the worker ID
# \param[in] job_queue the fifo to read jobs from
# \param[in] result_log the temporary log file to write exit codes to
function _job_pool_worker()
{
local id=$1
local job_queue=$2
local result_log=$3
local cmd=
local args=
exec 7<> ${job_queue}
while [[ "${cmd}" != "${job_pool_end_of_jobs}" && -e "${job_queue}" ]]; do
# workers block on the exclusive lock to read the job queue
flock --exclusive 7
IFS=$'\v'
read cmd args <${job_queue}
set -- ${args}
unset IFS
flock --unlock 7
# the worker should exit if it sees the end-of-job marker or run the
# job otherwise and save its exit code to the result log.
if [[ "${cmd}" == "${job_pool_end_of_jobs}" ]]; then
# write it one more time for the next sibling so that everyone
# will know we are exiting.
echo "${cmd}" >&7
else
_job_pool_echo "### _job_pool_worker-${id}: ${cmd}"
# run the job
{ ${cmd} "$@" ; }
# now check the exit code and prepend "ERROR" to the result log entry
# which we will use to count errors and then strip out later.
local result=$?
local status=
if [[ "${result}" != "0" ]]; then
status=ERROR
fi
# now write the error to the log, making sure multiple processes
# don't trample over each other.
exec 8<> ${result_log}
flock --exclusive 8
_job_pool_echo "${status}job_pool: exited ${result}: ${cmd} $@" >> ${result_log}
flock --unlock 8
exec 8>&-
_job_pool_echo "### _job_pool_worker-${id}: exited ${result}: ${cmd} $@"
fi
done
exec 7>&-
}
# \brief sends message to worker processes to stop
function _job_pool_stop_workers()
{
# send message to workers to exit, and wait for them to stop before
# doing cleanup.
echo ${job_pool_end_of_jobs} >> ${job_pool_job_queue}
wait
}
# \brief fork off the workers
# \param[in] job_queue the fifo used to send jobs to the workers
# \param[in] result_log the temporary log file to write exit codes to
function _job_pool_start_workers()
{
local job_queue=$1
local result_log=$2
for ((i=0; i<${job_pool_pool_size}; i++)); do
_job_pool_worker ${i} ${job_queue} ${result_log} &
done
}
################################################################################
# public functions
################################################################################
# \brief initializes the job pool
# \param[in] pool_size number of parallel jobs allowed
# \param[in] echo_command 1 to turn on echo, 0 to turn off
function job_pool_init()
{
local pool_size=$1
local echo_command=$2
# set the global attibutes
job_pool_pool_size=${pool_size:=1}
job_pool_echo_command=${echo_command:=0}
# create the fifo job queue and create the exit code log
rm -rf ${job_pool_job_queue} ${job_pool_result_log}
mkfifo ${job_pool_job_queue}
touch ${job_pool_result_log}
# fork off the workers
_job_pool_start_workers ${job_pool_job_queue} ${job_pool_result_log}
}
# \brief waits for all queued up jobs to complete and shuts down the job pool
function job_pool_shutdown()
{
_job_pool_stop_workers
_job_pool_print_result_log
_job_pool_cleanup
}
# \brief run a job in the job pool
function job_pool_run()
{
if [[ "${job_pool_pool_size}" == "-1" ]]; then
job_pool_init
fi
printf "%s\v" "$@" >> ${job_pool_job_queue}
echo >> ${job_pool_job_queue}
}
# \brief waits for all queued up jobs to complete before starting new jobs
# This function actually fakes a wait by telling the workers to exit
# when done with the jobs and then restarting them.
function job_pool_wait()
{
_job_pool_stop_workers
_job_pool_start_workers ${job_pool_job_queue} ${job_pool_result_log}
}
#########################################
# End of Job Pool
#########################################
identify_file () {
# Return array of audio parameters for the passed file: file_data
# $1 = path to file
#
# The array of file data that is generated looks like this
# 0 = path to input file
# 1 = input file encoding
# 2 = bit rate (bits per second)
# 3 = number of channels
# 4 = title
# 5 = artist
# 6 = album
# 7 = date or year
# 8 = track number
# 9 = genre
# 10 = comment
# First save file name
local file_data=("${1}") #0
# Ignore .m3u files as soxi obligingly decodes them - the b*****d
if echo "${1}" | grep -qv -e ".m3u$"
then
# Then get type, bitrate and channels
if file_data+=($(${SOXI} -t "${1}" 2>/dev/null)) #1
then
file_data+=($(${SOXI} -B "${1}")) #2
file_data+=($(${SOXI} -c "${1}")) #3
# Now get id tags
local tags=$(${SOXI} -a "${1}")
file_data+=("$(echo "${tags}" | grep -i '^title=' | cut -d = -f 2)") #4
file_data+=("$(echo "${tags}" | grep -i '^artist=' | cut -d = -f 2)") #5
file_data+=("$(echo "${tags}" | grep -i '^album=' | cut -d = -f 2)") #6
file_data+=("$(echo "${tags}" | grep -iE '^date=|^year=' | cut -d = -f 2)") #7
file_data+=("$(echo "${tags}" | grep -i '^tracknumber=' | cut -d = -f 2)") #8
file_data+=("$(echo "${tags}" | grep -i '^genre=' | cut -d = -f 2)") #9
file_data+=("$(echo "${tags}" | grep -i '^comment=' | cut -d = -f 2)") #10
fi
fi
# Fix bitrate to be a true number
local val
if [ ${#file_data[2]} -gt 0 ]
then
case ${file_data[2]: -1} in
k)
val=$(echo "${file_data[2]::${#file_data[2]}-1} * 1000" | bc)
file_data[2]=$(printf "%.0f" ${val})
;;
M)
val=$(echo "${file_data[2]::${#file_data[2]}-1} * 1000000" | bc)
file_data[2]=$(printf "%.0f" ${val})
;;
esac
fi
#echo "${file_data[@]}"
echo $(declare -p file_data)
}
make_decode_name () {
# Calculate the name of the decode file
# $1 = full path of audio file
# $2 = full path of temp directory
local fromfile=${1##*/}
echo "${2}/${fromfile%.*}".wav
}
decode_file () {
# Convert the file to a wav, there's nothing more to see
# here, move along now!
# $1 = path of file to decode
echo "Decoding $1 ..."
${SOX} "${1}" -t wav "${2}" 2>/dev/null
return $?
}
change_ext () {
# Change a file's extension. If we think it should be
# vorbis then make it ogg, after all who uses vorbis?
# $1 = path to file
# $2 = new extension to append
local new_ext
if [ "${2}" == "vorbis" ]
then
new_ext="ogg"
else
new_ext="${2}"
fi
echo "${1%.*}.${new_ext}"
}
encode_file () {
# Encode a wav file to either mp3, ogg or flac
# $1 = array holding all file data, see earlier comments
# for contents
local file_data=("$@") quality out_type
# Check to see if we're going to change the file type
# when we encode.
if [ "${file_data[11]}" == "same" ]
then
out_type=${file_data[1]}
else
out_type=${file_data[11]}
fi
# If we are changing the file type then we need to change
# the extension of the output file
if [ "${out_type}" != "${file_data[1]}" ]
then
file_data[0]="$(change_ext "${file_data[0]}" "${file_data[11]}")"
fi
# Choose your weapons gentlemen
case ${out_type} in
"mp3")
echo Encoding $(basename "${file_data[@]: -1}") to mp3 ...
# set the mono switch if needed
[ "${file_data[3]}" -eq 1 ] && local mode="-m m"
# Work out quality
# Beware nasty magic numbers ahead divined by sacrificing virgins,
# playing with the entrails and reading the web pages shown
# near the top of this file
if [ "${file_data[1]}" == "flac" ]
then
quality="${LAME_DEF_QUAL}"
else
# if bitrate is very high use cbr 320
if ((${file_data[2]} > 283000))
then
quality="-b 320"
else
# if bitrate is low use abr 80 or 65
if ((${file_data[2]} < 98000))
then
if [ "${file_data[3]}" -eq 1 ]
then
quality="--abr 56"
else
quality="--abr 80"
fi
else
quality="-V $(echo "scale=5; (${file_data[2]} - 264285.71429) / (-21785.71429 * 1.09) - 1;" | bc)"
fi
fi
fi
${LAME} --quiet ${quality} ${mode} --add-id3v2 \
--tt "${file_data[4]}" \
--ta "${file_data[5]}" \
--tl "${file_data[6]}" \
--ty "${file_data[7]}" \
--tn "${file_data[8]}" \
--tg "${file_data[9]}" \
--tc "${file_data[10]}" \
"${file_data[@]: -1}" "${file_data[0]}"
echo Completed encoding $(basename "${file_data[@]: -1}") to mp3
;;
"vorbis"|"ogg")
echo Encoding $(basename "${file_data[@]: -1}") to ogg ...
# Work out quality
if [ "${file_data[1]}" == "flac" ]
then
quality="${OGG_DEF_QUAL}"
else
quality="-q $(echo "scale=5; l (${file_data[2]} / 53478.12402) * 1.1384 / 0.18859 - 1;" | bc -l)"
fi
${OGGENC} ${quality} --quiet -o "${file_data[0]}" \
-t "${file_data[4]}" \
-a "${file_data[5]}" \
-l "${file_data[6]}" \
-d "${file_data[7]}" \
-N "${file_data[8]}" \
-G "${file_data[9]}" \
-c "${file_data[10]}" \
"${file_data[@]: -1}" 2>/dev/null
echo Completed encoding $(basename "${file_data[@]: -1}") to ogg
;;
"flac")
# what no quality to worry about - ha! this _is_ easy!
echo Encoding $(basename "${file_data[@]: -1}") to flac ...
${FLAC} --totally-silent --force --best -o "${file_data[0]}" \
--tag TITLE="${file_data[4]}" \
--tag ARTIST="${file_data[5]}" \
--tag ALBUM="${file_data[6]}" \
--tag DATE="${file_data[7]}" \
--tag TRACKNUMBER="${file_data[8]}" \
--tag GENRE="${file_data[9]}" \
--tag COMMENT="${file_data[10]}" \
"${file_data[@]: -1}"
echo Completed encoding $(basename "${file_data[@]: -1}") to flac
;;
esac
return $?
}
process_file () {
# Full process route for a single file.
# $1 = input file path
# $2 = temporary directory path
# $3 = user selected target type from command line
# $4 = options to normalize
local norm_op
echo "Processing file $1 ..."
# Retrieve file data into file_data
eval $(identify_file "${1}")
# If file is valid
if [ -n "${file_data[1]}" ] && echo "mp3 vorbis flac" | grep -q "${file_data[1]}"
then
# Add target type and wav file name to file data list
file_data+=("${3}")
file_data+=("$(make_decode_name "${1}" "${2}")")
if decode_file "$1" "${file_data[@]: -1}"
then
echo "Normalizing $1 ..."
if norm_op=$(${NORM} --no-progress ${4} "${file_data[@]: -1}" 2>&1)
then
if ! echo ${norm_op} | grep -q "already normalized"
then
encode_file "${file_data[@]}"
else
echo $1 is already normalized - not encoding\!
fi
fi
fi
fi
return 0
}
process_dir () {
# Full process route for a directory, i.e batch normalize.
# We need to decode every file to a wav before we can do
# the normalization.
# $1 = input file path
# $2 = temporary directory path
# $3 = user selected target type from command line
# $4 = options to normalize
local idx=0 file_list=() p_list=() dec_count norm_op
declare -r rows=13
echo "Batch processing $1 ..."
# For each file
for ip_file in "${1%/}"/*
do
# If we get no file expansion break out now
[ "${ip_file: -1}" == '*' ] && break
# Retrieve file data into file_data
eval $(identify_file "${ip_file}")
# If file is valid
if [ -n "${file_data[1]}" ] && echo "mp3 vorbis flac" | grep -q "${file_data[1]}"
then
# Add target type and wav file name to file data list
file_data+=("${3}")
file_data+=("$(make_decode_name "${ip_file}" "${2}")")
# If we get a wav file then the append all the data onto
# the end of the file list
if decode_file "${ip_file}" "${file_data[@]: -1}"
then
file_list+=("${file_data[@]}")
fi
fi
done
# file_list now holds data for all files in one long list
# Oh how I wish we had multidimensional arrays!
# Each file takes up 13 elements (or rows)
# Do we have any wavs?
dec_count=$(ls -1 "${2}"/*.wav 2>/dev/null | wc -l)
if ((dec_count > 0))
then
# Yes it's a miracle!
echo "Batch normalizing $1 ..."
if norm_op=$(${NORM} --batch --no-progress ${4} "${2}"/*.wav 2>&1)
then
if ! echo ${norm_op} | grep -q "already normalized"
then
# Slice the file_list every ${rows}, i.e. 1 file = 13 rows
while ((idx < ${#file_list[@]}))
do
(encode_file "${file_list[@]:${idx}:${rows}}") &
p_list+=($!)
((idx+=rows))
done
wait ${p_list[*]}
else
echo $1 is already normalized - not encoding\!
fi
fi
fi
return 0
}
process_param () {
# Process the non-option parameters, i.e. the files and directories
# Create the temp working area and decide if were processing a file
# or directory.
# $1 = input file path
# $2 = temporary directory path
# $3 = user selected target type from command line
# $4 = options to normalize
local WORKDIR=$(mktemp --directory --tmpdir="$2" naud-XXXXXXXXXX)
WDIRS[${#WDIRS[@]}]="${WORKDIR}"
if [ -d "$1" ]
then
process_dir "${1}" "${WORKDIR}" "${3}" "${4}"
elif [ -f "$1" -a -r "$1" ]
then
process_file "${1}" "${WORKDIR}" "${3}" "${4}"
fi
rm -r ${WORKDIR}
return 0
}
job_pool_init $((numprocs * 2)) 0
# Command line parameter mangling starts here
# -a AMP \\
# -g ADJ |
# -n |
# -T THR |_ These arguments are passed as arguments to normalize.
# -b | Run "normalize-audio --help" for more info.
# -m |
# -v |
# -q /
declare -r NORM_ARG_LIST="-a -g -n -T -b -m -v -q"
NORM_ARGS=""
while [ -n "$1" ]
do
if [ "${1::1}" == "-" ]
then
# It's an option
if echo "${NORM_ARG_LIST}" | grep -q -e "${1%-}"
then
# pass-thru arg for normalize
case "${1%-}" in
-n|-b|-m|-v|-q)
if [ ${1: -1} == "-" ]
then
NORM_ARGS=$(echo ${NORM_ARGS} | sed "s/${1%-}//g")
else
echo ${NORM_ARGS} | grep -q -e ${1%-} || NORM_ARGS="${NORM_ARGS} ${1%-}"
fi
shift
;;
-a|-g|-T)
if [ ${1: -1} == "-" ]
then
NORM_ARGS=$(echo ${NORM_ARGS} | sed "s/${1%-} \S\+//g")
shift
else
if (($# > 1))
then
if echo ${NORM_ARGS} | grep -q -e ${1%-}
then
NORM_ARGS=$(echo ${NORM_ARGS} | sed "s/${1%-} \S\+/${1%-} ${2}/")
else
NORM_ARGS="${NORM_ARGS} ${1%-} ${2}"
fi
shift 2
else
echo Missing argument to ${1}
exit 1
fi
fi
;;
esac
else
# naud arg
case "${1}" in
-t)
if (($# > 1))
then
shift
TMPDIR="${1}"
else
echo Missing argument to -t
exit 1
fi
;;
-o)
if (($# > 1))
then
shift
TARGET="${1}"
if ! echo ${TARGET_OPTS} | grep -q ${TARGET}
then
echo "Unrecognised target file type, should be one of \"${TARGET_OPTS}\""
exit 1
fi
else
echo Missing argument to -o
exit 1
fi
;;
esac
shift
fi
else
# It's a file / dir
#(process_param "${1}" "${TMPDIR}" "${TARGET}" "${NORM_ARGS}") &
job_pool_run process_param "${1}" "${TMPDIR}" "${TARGET}" "${NORM_ARGS}"
shift
fi
done
job_pool_wait
# Phew! - time for a cup of tea
echo "naud has finished."
exit 0