-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtranscode
executable file
·205 lines (174 loc) · 4.46 KB
/
transcode
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
#!/bin/bash -efu
#
# transcode - transcode radio programs for portable use
#
# Written by Alexey Tourbin.
# This file is distributed as Public Domain.
verbose=
tmpdir=${TMPDIR:-/tmp}
av0=$(readlink -ev "$0")
av0dir=$(dirname "$av0")
# autodetect mono
mono=
# use deafult aac priming
priming=
# use dynamic range compression when appropriate
no_drc=
# compress towards this dynamic range
drc_range=10dB
. $av0dir/calc.sh
. $av0dir/dualmono.sh
# seek and duration support
. $av0dir/ffseek.sh
# predefined album gain
gain=
# quality control
VBR= TVBR=
MonoStuff()
{
case $mono in
no) ismono=$(Calc "${ismono:?} >= 2 ? $ismono : 0") ;;
yes) ismono=9 ;;
esac
if [ $ismono -ge 2 ]; then
vbr=4
else
ismono=0
monoparts=$($av0dir/monoparts --gain=$g1db ${ff_ss:+-ss $ff_ss} <$tmpdir/gain$$.log)
if [ "$monoparts" = 'all-mono' ]; then
ismono=9 vbr=4
elif [ -n "$monoparts" ]; then
local dura durap
dura=$(tc2ms ${g0dura:?})
durap=$(Calc "-(${monoparts//,/+})*100")
if Cond "$durap > 0.75 * $dura"; then
vbr=4
fi
fi
fi
if [ $ismono -gt 0 ]; then
if [ ${g0ch:?} -gt 1 ]; then
AF=${AF:+$AF,}pan='1c|c0=0.5*c0+0.5*c1'
fi
else
if [ -n "$monoparts" ]; then
AF=${AF:+$AF,}monoparts=${monoparts//,/|}
fi
fi
}
Transcode()
{
ffseek
local g0brate
. $av0dir/mono.sh
# -af chain
local AF=${downmix:+$downmix,}
local vbr=4 tvbr=82
[ ${g0brate:-320} -lt 180 ] || vbr=3 tvbr=91
if [ "$mono" = NO ]; then
[ ${g0ch:?} -gt 1 ] ||
echo >&2 "warning: cannot force stereo on mono input"
else
MonoStuff "$1"
fi
if [[ $VBR$vbr = 3 && $2 = *.[Mm][Pp]3 ]]; then
vbr=3$(zenity --title VBR \
--text $'Select VBR for\n'"$2" \
--column VBR --list V4 V3) ||:
vbr=${vbr#3V}
fi
local drc=
if [ -z "$no_drc" ] && [ ${g1range%.*} -gt ${drc_range%dB} ]; then
vars=$(perl -le '
use v5.12;
my $range = shift;
my $rlow = shift;
my $target = shift;
my $ratio = 0 + sprintf "%.2f", $range / ($target + ($range - $target) / 3);
my $thresh = 0 + sprintf "%.0f", $rlow;
my $knee = 0 + sprintf "%.0f", 2 * $range;
say "ratio=$ratio thresh=$thresh knee=$knee";
' -- $g1range $g1rlow $drc_range)
local $vars
qadrc="$(($thresh+2)):$ratio:$knee"
mydrc="$(($thresh-2)):$ratio:$knee"
drc="asplit[i1][i2];[i1]qadrc=$qadrc[o1];[i2]mydrc=$mydrc[o2];[o1][o2]amix"
AF=${AF:+$AF,}$drc
ffmpeg $ff_decode_pre ${ff_ss:+-ss $ff_ss} -i "$1" ${ff_t:+-t $ff_t} -vn \
-af "$AF",replaygain,ebur128=framelog=verbose \
-f null - >$tmpdir/gain$$.log 2>&1 || { grep -i error $tmpdir/gain$$.log; false; }
vars=$($av0dir/aacgain15pp <$tmpdir/gain$$.log)
local $vars
if [ ${g0ch:?} = 1 ]; then
DualMono rg1
DualMono eb1
fi
g1db=$(Calc2f "(${rg1gain:?} + ${eb1gain:?}) / 2")
g1peak=$rg1peak g1range=$eb1range g1rlow=$eb1rlow
fi
if [ ${g1db:?} != 0 ]; then
AF=${AF:+$AF,}volume=${g1db}dB
fi
local limiter
limiter=$(perl -le "print 1 if ${g1peak:?} + $g1db > -0.999")
if [ -n "$limiter" ]; then
AF=${AF:+$AF,}qalimiter
fi
if [[ $2 = *.[Mm][Pp]3 ]]; then
Conv1()
{
[ -z "$verbose" ] || set -x
ffmpeg -v error ${verbose:+-stats} \
$ff_decode_pre -i "$1" ${ff_ss:+-ss $ff_ss} ${ff_t:+-t $ff_t} -vn \
${AF:+-af "$AF"} -aq ${VBR:-$vbr} -y "$2"
}
if [ -z "$verbose" ]; then
Conv1 "$@"
else
(Conv1 "$@")
[ $? = 0 ]
fi
else
adts=
[[ $2 != *.[Aa][Aa][Cc] ]] || adts='--adts'
priming=${priming:+--num-priming=$priming}
Conv2()
{
set -o pipefail
[ -z "$verbose" ] || set -x
ffmpeg -v error \
$ff_decode_pre ${ff_ss:+-ss $ff_ss} -i "$1" ${ff_t:+-t $ff_t} -vn \
${AF:+-af "$AF"} -acodec pcm_f32le -f wav - |
qaac ${verbose:--s} -o "$2" $priming $adts --tvbr=${TVBR:-$tvbr} -
}
if [ -z "$verbose" ]; then
Conv2 "$@"
set +o pipefail
else
(Conv2 "$@")
[ $? = 0 ]
fi
fi
rm $tmpdir/gain$$.log
}
argv=$(getopt -n "${0##*/}" -o vt:V: -al verbose,mono,stereo,force-stereo,no-drc,drc-range:,priming:,ss:,to:,tvbr: -- "$@")
eval set -- "$argv"
while :; do
case "$1" in
-v|--verbose) verbose=' '; shift ;;
--mono) mono=yes; shift ;;
--stereo) mono=no; shift ;;
--force-stereo) mono=NO; shift ;;
--no-drc) no_drc=1; shift ;;
--drc-range) drc_range=${2:?}; shift 2;;
--priming) priming=${2:?}; shift 2 ;;
--ss) ff_ss=${2:?}; shift 2 ;;
--to) ff_to=${2:?}; shift 2 ;;
-t) ff_t=${2:?}; shift 2 ;;
-V) VBR=${2:?}; shift 2 ;;
--tvbr) TVBR=${2:?}; shift 2 ;;
--) shift; break ;;
*) echo >&2 "unrecognized option: $1"; false ;;
esac
done
Transcode "$1" "${2:-${1%.*}.mp3}"