-
Notifications
You must be signed in to change notification settings - Fork 0
/
enc.audio
executable file
·567 lines (445 loc) · 13 KB
/
enc.audio
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
#!/bin/sh
set -e -u
error()
{
case $1 in
"
"*) ;;
*) printf '\n' >&2
esac
printf '%s' "$1" >&2
case $1 in
*"
") ;;
*) printf '\n' >&2
esac
exit 1
}
command -v ffmpeg >/dev/null || error "ffmpeg not found in \$PATH!"
badargs()
{
bin="$(basename "$0")"
syntax="
Syntax:
$bin [options] <profile> <input_file> <output_file>
$bin [options] <profile> <input_file> ... <output_directory>
The container format is guessed by the file extension of the output file.
E.g. \".mp4\" generates an MPEG 4 container file, whereas \".mp3\" would
generate a MP3 container, and \".aac\" a raw AAC data stream.
If output is a directory, the same file name as input will be used, except for
the file extension which will be the default extension for the default
container format of the chosen profile, unless overridden by the \"extension\"
option.
NOTE:
Not all codecs can be used in all container formats!
Options:
NOTE:
Options can be shortened. E.g. \"vol\" and \"volume\" are the same option.
vol[ume]=<p%|max>
Volume can either be in percent (\"90%\"), allowing increasing and
decreasing volume by any amount desired (up to the point where the
output clips), or it can be \"max\" in which case the maximum
amplication is calculated that is possible without causing any
clipping.
ext[ension]=<ext>
Overrides output file extension in case output is a directory,
e.g. \"ext=mp3\". Has no effect if output is a file name.
Valid profile names are:
mp3poor - Variable bitrate MP3, poor quality.
mp3low - Variable bitrate MP3, low quality.
mp3std - Variable bitrate MP3, standard quality.
mp3high - Variable bitrate MP3, high quality.
aacpoor - Variable bitrate AAC, poor quality.
aaclow - Variable bitrate AAC, low quality.
aacstd - Variable bitrate AAC, standard quality.
aachigh - Variable bitrate AAC, high quality.
archive - FLAC lossless compression, suitable for archiving music.
webcast - Webcast with low bitrate (Vorbis mono).
webcasthigh - Webcast with high bitrate (Vorbis mono).
webcasthq - Webcast with low bitrate, better quality (Opus mono).
webcasthqhigh - Webcast with high bitrate, better quality (Opus mono).
transform - Copy audio data unmodified, just rewrite container.
fixaac - Fix broken AAC streams with bad timing or bad average bitrate
that transform cannot fix. All meta information will be lost!
"
error "$syntax"
}
volume=
outfileExt=
profile=
output=
inCount=0
while [ -z "$output" ]
do
if [ $# -lt 3 ]
then
printf "%s\n" "Too little arguments!" >&2
badargs
fi
case $1 in
"vol="*)
volume=${1#"vol="}
shift
;;
"volume="*)
volume=${1#"volume="}
shift
;;
"ext="*)
outfileExt=${1#"ext="}
shift
;;
"extension="*)
outfileExt=${1#"extension="}
shift
;;
*)
profile=$1
shift
inCount=$(( $# - 1 ))
eval "output=\${$#}"
;;
esac
done
if [ $inCount -gt 1 ] && [ ! -d "$output" ]
then
error "Multiple input files are only allowed if output is a directory!"
fi
volarg=
case "$volume" in
"")
# No volume set - fine
;;
"max")
# We'll take care of that later
volume="max"
;;
*"%")
volarg=${volume%"%"}
volarg="volume=$volarg/100"
;;
*)
error "Bad volume value \"$volume\"!"
;;
esac
hascoder()
{
encfound=$(ffmpeg -encoders 2>/dev/null | grep -E "^ [^ ]+ $1 ")
if [ -n "$encfound" ]
then
return 0
fi
return 1
}
# $1 - bitrate in kbit/s (must be 16 kbit/s or higher)
# ($2) - "stereo" or "mono"; default is "stereo"
# ($3) - force HE/HEv2: "yes" or "no"; default is "no"
activate_aac()
{
if [ "$1" -lt 16 ]
then
echo "AAC bitrate too low!" >&2
exit 1
fi
if [ "${2:-"stereo"}" = "mono" ]
then
encargs="$encargs -ac 1" # Convert audio to mono
else
encargs="$encargs -ac 2" # Convert audio to stereo
fi
encargs="$encargs -b:a ${1}k" # Set bitrate in kbit/s
if [ "$1" -lt 80 ] || [ "${3:-"no"}" = "yes" ]
then
if [ "${2:-"stereo"}" = "mono" ]
then
if hascoder "aac_at"
then
encargs="$encargs -c:a aac_at" # AAC audio
encargs="$encargs -profile:a 28" # Use HEv2 encoding
return
fi
if hascoder "libfdk_aac"
then
encargs="$encargs -c:a libfdk_aac" # AAC audio
encargs="$encargs -profile:a aac_he_v2" # Use HEv2 encoding
return
fi
fi
if hascoder "aac_at"
then
encargs="$encargs -c:a aac_at" # AAC audio
encargs="$encargs -profile:a 4" # Use HE encoding
return
fi
if hascoder "libfdk_aac"
then
encargs="$encargs -c:a libfdk_aac" # AAC audio
encargs="$encargs -profile:a aac_he" # Use HE encoding
return
fi
if [ "${3:-"no"}" = "yes" ]
then
echo "No encoder with HE/HEv2 support found!" >&2
exit 1
fi
# Fallback to non HE/HEv2 if allowed
if hascoder "aac"
then
encargs="$encargs -c:a aac" # AAC audio
encargs="$encargs -aac_coder twoloop" # Tread CPU time for quality
return
fi
fi
if hascoder "aac_at"
then
encargs="$encargs -c:a aac_at" # AAC audio
encargs="$encargs -aac_at_mode abr" # Set ABR mode
return
fi
if hascoder "libfdk_aac"
then
encargs="$encargs -c:a libfdk_aac" # AAC audio
return
fi
if hascoder "aac"
then
encargs="$encargs -c:a aac" # AAC audio
encargs="$encargs -aac_coder twoloop" # Tread CPU time for quality
return
fi
echo "No AAC encoder found!" >&2
exit 1
}
# $1 - vbr quality - 2 to 5, 5 being best
activate_aacvbr()
{
if hascoder "aac_at"
then
encargs="$encargs -c:a aac_at" # AAC audio
encargs="$encargs -aac_at_mode vbr" # Set VBR mode
case "$1" in
"5")
encargs="$encargs -qscale:a 3" # Approx. libfdk level 5
;;
"4")
encargs="$encargs -qscale:a 6" # Approx. libfdk level 4
;;
"3")
encargs="$encargs -qscale:a 8" # Approx. libfdk level 3
;;
"2")
encargs="$encargs -qscale:a 10" # Approx. libfdk level 2
;;
esac
return
fi
if hascoder "libfdk_aac"
then
encargs="$encargs -c:a libfdk_aac" # AAC audio
encargs="$encargs -vbr $1" # Set VBR quality
return
fi
if hascoder "aac"
then
encargs="$encargs -c:a aac" # AAC audio
encargs="$encargs -aac_coder twoloop" # Treat speed for quality
# The VBR abilities of the aac codec suck horribly, that's probably
# why this is still an experimental feature as of 2022. Despite
# producing larger files than a lower CBR rate, they still sound much
# worse, so with that enocder, only CBR makes sense at the moment.
# We use slightly higher bitrates than VBR would use, just in case.
case "$1" in
"5")
encargs="$encargs -b:a 224k" # Approx. libfdk level 5 quality
;;
"4")
encargs="$encargs -b:a 144k" # Approx. libfdk level 4 quality
;;
"3")
encargs="$encargs -b:a 112k" # Approx. libfdk level 3 quality
;;
"2")
encargs="$encargs -b:a 80k" # Approx. libfdk level 2 quality
esac
return
fi
echo "No AAC encoder found!" >&2
exit 1
}
# Never decode or encode any video
encargs="-vn"
decargs="-vn"
case "$profile" in
"mp3poor")
encargs="$encargs -codec:a libmp3lame" # Set liblame codec
encargs="$encargs -qscale:a 6" # Set VBR quality
encargs="$encargs -compression_level 0" # Highst compression quality
[ -z "$outfileExt" ] && outfileExt="mp3"
;;
"mp3low")
encargs="$encargs -codec:a libmp3lame" # Set liblame codec
encargs="$encargs -qscale:a 4" # Set VBR quality
encargs="$encargs -compression_level 0" # Highst compression quality
[ -z "$outfileExt" ] && outfileExt="mp3"
;;
"mp3std")
encargs="$encargs -codec:a libmp3lame" # Set liblame codec
encargs="$encargs -qscale:a 2" # Set VBR quality
encargs="$encargs -compression_level 0" # Highst compression quality
[ -z "$outfileExt" ] && outfileExt="mp3"
;;
"mp3high")
encargs="$encargs -codec:a libmp3lame" # Set liblame codec
encargs="$encargs -qscale:a 0" # Set VBR quality
encargs="$encargs -compression_level 0" # Highst compression quality
[ -z "$outfileExt" ] && outfileExt="mp3"
;;
"aacpoor")
activate_aacvbr 2
[ -z "$outfileExt" ] && outfileExt="m4a"
;;
"aaclow")
activate_aacvbr 3
[ -z "$outfileExt" ] && outfileExt="m4a"
;;
"aacstd")
activate_aacvbr 4
[ -z "$outfileExt" ] && outfileExt="m4a"
;;
"aachigh")
activate_aacvbr 5
[ -z "$outfileExt" ] && outfileExt="m4a"
;;
"archive")
encargs="$encargs -codec:a flac" # Set FLAC codec
encargs="$encargs -compression_level 12" # Use best compression
[ -z "$outfileExt" ] && outfileExt="flac"
;;
"webcast")
encargs="$encargs -c:a libvorbis" # Vorbis audio
encargs="$encargs -b:a 24k" # Set bitrate
encargs="$encargs -ar 22050" # Lower sampling rate to 22050 Hz
encargs="$encargs -ac 1" # Convert audio to mono
[ -z "$outfileExt" ] && outfileExt="ogg"
;;
"webcasthigh")
encargs="$encargs -c:a libvorbis" # Vorbis audio
encargs="$encargs -b:a 32k" # Set bitrate
encargs="$encargs -ac 1" # Convert audio to mono
[ -z "$outfileExt" ] && outfileExt="ogg"
;;
"webcasthq")
encargs="$encargs -c:a libopus" # Opus audio
encargs="$encargs -b:a 24k" # Set bitrate
encargs="$encargs -ar 24000" # Lower sampling rate to 24000 Hz
encargs="$encargs -ac 1" # Convert audio to mono
[ -z "$outfileExt" ] && outfileExt="opus"
;;
"webcasthqhigh")
encargs="$encargs -c:a libopus" # Opus audio
encargs="$encargs -b:a 32k" # Set bitrate
encargs="$encargs -ac 1" # Convert audio to mono
[ -z "$outfileExt" ] && outfileExt="opus"
;;
"transform")
encargs="$encargs -c:a copy" # Copy audio data
if [ -z "$outfileExt" ]
then
error "Profile \"transform\" requires \"extension\" option!"
fi
if [ -n "$volarg" ]
then
error "Profile \"transform\" does not support \"volume\" option!"
fi
;;
"fixaac")
# This is a special case which is not even using $decargs and $encargs.
[ -z "$outfileExt" ] && outfileExt="m4a"
;;
*)
error "Unknown profile \"$profile\"!"
;;
esac
abspath()
(
path=$1
if [ ! -e "$path" ]; then
return 1
fi
file=
dir="$path"
if [ ! -d "$dir" ]; then
file=$( basename "$dir" )
dir=$( dirname "$dir" )
fi
case "$dir" in
/*) ;;
*) dir="$( pwd )/$dir"
esac
result=$( cd "$dir" && pwd )
if [ -n "$file" ]; then
case "$result" in
*/) ;;
*) result="$result/"
esac
result="$result$file"
fi
printf "%s\n" "$result"
)
basenameWithoutExt()
{
printf '%s' "$( basename "$1" | cut -f1 -d'.' )"
}
calcMaxVolume()
{
printf "%s\n" "==> Analyzing audio, please wait..."
volarg=$(
ffmpeg -i "$1" -filter:a volumedetect \
-f null /dev/null 2>&1 \
| grep max_volume \
| sed 's/.*max_volume: -\{0,1\}\([0-9.]\{1,\}\) dB$/\1/'
)
if [ "$volarg" = "0.0" ]
then
printf "%s\n" "==> Volume already at maximum."
volarg=
else
printf "%s\n" "==> Emphasizing volume by $volarg dB."
volarg="volume=${volarg}dB"
fi
}
while [ $inCount -gt 0 ]
do
printf "\n%s\n" "====> Next File: $1"
[ "$volume" = "max" ] && calcMaxVolume "$1"
if [ -d "$output" ]
then
outfile="$output/$( basenameWithoutExt "$1" ).$outfileExt"
if [ "$(abspath "$1")" = "$(abspath "$outfile")" ]
then
echo "Input file cannot override itself!" >&2
exit 1
fi
else
outfile="$output"
fi
case "$profile" in
"fixaac")
# Sometimes "transform" will already fix the stream when
# transforming from an AAC container to a container of the same
# kind but in some cases this doesn't work and then this trick
# will work:
ffmpeg -vn -i "$1" -c:a copy -f adts - \
| ffmpeg -f aac -i - -c:a copy "$outfile"
;;
*)
filterarg=
[ -n "$volarg" ] && filterarg="-filter:a $volarg"
eval ffmpeg "$decargs" -i "\"$1\"" \
"$encargs $filterarg" "\"$outfile"\"
;;
esac
shift
inCount=$(( inCount - 1 ))
printf "\n\n"
done