-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffwrapper
executable file
·440 lines (414 loc) · 11.1 KB
/
ffwrapper
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
#!/bin/bash
do_usage () {
cat >&2 <<EOF
Usage: $0 -i input-stream -o rtmp-output-url -v src-video-stream-id
-a src-audio-stream-id[:audio-channel] [OPTIONS]
FFmpeg wrapper script for live transcoding and RTMP streaming
Mandatory arguments to long options are mandatory for short options too.
Required parameters:
-i, --input=input-stream input stream
-o, --output=rtmp-output-url RTMP output URL
-v, --videosrc=src=stream-id Source video stream selector
-a, --audiosrc=src-stream-id[:audio-channel]
Source audio stream and channel selector
If specified, the selected channel is duplicated to stereo output
using a pan audio filter.
Optional parameters:
-c, --crop=pix-x:pix-y:offset-x,offset-y
Image crop filter dimensions and offsets
-s, --scale=x:y Image scale dimensions (output SAR hardcoded to 1:1)
--deint[=filter] Deinterlace video, optionally using specified filter
-k, --keyint=seconds Imposed (hard) keyframe interval (Default: 4)
-f, --ffopts-file=file Read FFmpeg encoding parameters from this file
(FFVENC, FFAENC bash arrays)
-j, --snap-file=file Generate image snapshot every 5 seconds and store it in
the specified file
-b, --audiobargraph=i[nput]/o[utput]/r[aw][:tcp-port:silence-threshold:report-volume:ip-address]
Pipe to VLC with Audio Bar Graph filter for audio monitoring
i/o/r: output audio: same as input (copy), same as output or raw
tcp-port: direct ABG output to this port (default: 6514)
silence-threshold: float (default: 0.01)
report-volume: integer (default: 0)
ip-address: Direct ABG output to this (IPv4) address (default:
127.0.0.1)
-p, --pidfile Write FFmpeg PID to this file
-l, --syslog[=facility.level] Redirect FFmpeg standard error to syslog, optionally using specified
facility and level (default: daemon.notice)
-d, --daemon Run FFmpeg as a daemon
EOF
}
daemonize=false
declare -A opts;
test_getopt="$(getopt -T)"
if [ $? -eq 4 -a -z "$test_getopt" ]; then
getopt_flavor=1
else
SCRIPTDIR="$(cd "${0%/*}" 2>/dev/null && pwd -P)"
if [ $? -ne 0 ]; then
echo "could not cd to ${0%/*}, SCRIPTDIR may be wrong" >&2;
SCRIPTDIR="${0/*}"
fi
getopt_src=$(which getopts_long.sh || echo "${SCRIPTDIR}/getopts_long.sh")
if [ -f "${getopt_src}" -a -r "${getopt_src}" ]; then
. "${getopt_src}"
if declare -f getopts_long >/dev/null; then
getopt_flavor=2
fi
fi
fi
if [ -z "$getopt_flavor" ]; then
echo "either getopt(1) from util-linux or getopts_long.sh is required, but none was found" >&2
exit 1
fi
if [ $getopt_flavor -eq 1 ]; then
params="$(getopt -o i:o:c:s:k:f:a:v:l::j:b:p:dh \
-l input:,output:,crop:,scale:,deint::,keyint: \
-l ffopts-file:,audiosrc:,videosrc: \
-l snap-file:,audiobargraph:,pidfile: \
-l syslog::,daemon,help -n "$0" -- "$@")"
#declare -p params
eval set -- "$params"
while true; do
case "$1" in
-h|--help)
do_usage
exit 0
;;
-i|--input)
opts[input]="$2"
shift 2
;;
-o|--output)
opts[output]="$2"
shift 2
;;
-c|--crop)
opts[crop]="$2"
shift 2
;;
-s|--scale)
opts[scale]="$2"
shift 2
;;
--deint)
opts[deinterlace]="${2:-yadif=0:-1:0}"
shift 2
;;
-k|--keyint)
opts[keyint]="$2"
shift 2
;;
-f|--ffopts-file)
opts[ffparams]="$2"
shift 2
;;
-a|--audiosrc)
opts[audiosrc]="$2"
shift 2
;;
-v|--videosrc)
opts[videosrc]="$2"
shift 2
;;
-j|--snap-file)
opts[snapfile]="$2"
shift 2
;;
-b|--audiobargraph)
opts[audiobargraph]="$2"
shift 2
;;
-p|--pidfile)
opts[pidfile]="$2"
shift 2
;;
-l|--syslog)
opts[syslog]="${2:-daemon.notice}"
shift 2
;;
-d|--daemon)
daemonize=true
shift
;;
--)
shift
break
;;
*)
do_usage
exit 1
;;
esac
done
elif [ $getopt_flavor -eq 2 ]; then
OPTLIND=1
while getopts_long i:o:c:s:k:f:a:v:l::j:b:p:dh opt \
input 1 output 1 crop 1 scale 1 deint 2 keyint 1 \
ffopts-file 1 audiosrc 1 videosrc 1 \
snap-file 1 audiobargraph 1 pidfile 1 \
syslog 2 daemon 0 help 0 \
"" "$@"
do
case "$opt" in
h|help)
do_usage
exit 0
;;
i|input)
opts[input]="$OPTLARG"
;;
o|output)
opts[output]="$OPTLARG"
;;
c|crop)
opts[crop]="$OPTLARG"
;;
s|scale)
opts[scale]="$OPTLARG"
;;
deint)
opts[deinterlace]="${OPTLARG:-yadif=0:-1:0}"
;;
k|keyint)
opts[keyint]="$OPTLARG"
;;
f|ffopts-file)
opts[ffparams]="$OPTLARG"
;;
a|audiosrc)
opts[audiosrc]="$OPTLARG"
;;
v|videosrc)
opts[videosrc]="$OPTLARG"
;;
j|snap-file)
opts[snapfile]="$OPTLARG"
;;
b|audiobargraph)
opts[audiobargraph]="$OPTLARG"
;;
p|pidfile)
opts[pidfile]="$OPTLARG"
;;
l|syslog)
opts[syslog]="${OPTLARG:-daemon.notice}"
;;
d|daemon)
daemonize=true
;;
?)
do_usage
exit 1
;;
esac
done
fi
ffinput="${opts[input]}"
test -n "${ffinput}" || { do_usage; exit 1; }
ffoutput="${opts[output]}"
test -n "${ffoutput}" || { do_usage; exit 1; }
#ffsnapfile="${opts[snapfile]:=/tmp/${ffoutput##*/}.jpg}"
ffsnapfile="${opts[snapfile]}"
ffsyslog="${opts[syslog]}"
ffpidfile="${opts[pidfile]:=/tmp/ffmpeg.${ffoutput##*/}.pid}"
vlcpidpath="${ffpidfile%/*}"
vlcpidfile="${ffpidfile##*/}"
vlcpidfile="${vlcpidpath}/vlc.${vlcpidfile#ffmpeg.}"
if [[ "$ffinput" == *srt?(+http?(s))://* ]]; then
STRANSMIT=$(which srt-live-transmit)
if [ ! -x "$STRANSMIT" ]; then
exit 1
fi
SRTOPTS=()
if [[ "$ffinput" == *srt+http?(s)://* ]]; then
ffinput=($ffinput)
for ((p=0, plen=${#ffinput[@]}; $p < $plen; p++)); do
if [[ "${ffinput[$p]}" == srt+http?(s)://* ]]; then
ffinput[$p]="${ffinput[$p]#srt+}"
break
else
SRTOPTS+=("${ffinput[$p]}")
unset "ffinput[${p}]"
fi
done
ffinput=$(curl -s "${ffinput[@]}")
if [ $? -ne 0 -o -z "$ffinput" ] || \
[[ "$ffinput" != srt://* ]]; then
exit 1
fi
fi
SRTOPTS+=($ffinput "file://con")
if [ -n "${ffsyslog}" -o "$daemonize" = true ]; then
exec {srtfifo}< <(SRTPPID=$BASHPID;
"$STRANSMIT" -a:no "${SRTOPTS[@]}" \
2> >(SRTPID=$(pgrep -f -P "$SRTPPID" "$STRANSMIT");
STRANSMIT=$(basename "$STRANSMIT");
/usr/bin/logger -p "${ffsyslog:-daemon.notice}" \
-t "${STRANSMIT}[${SRTPID}]") )
else
exec {srtfifo}< <("$STRANSMIT" -a:no "${SRTOPTS[@]}")
fi
ffinput="pipe:${srtfifo}"
fi
OLDIFS="$IFS"
IFS=:
ffvideosrc=(${opts[videosrc]})
IFS="$OLDIFS"
unset OLDIFS
if [ ${#ffvideosrc[@]} -eq 1 ]; then
ffmap_video="0:${ffvideosrc[0]}"
else
do_usage
exit 1
fi
OLDIFS="$IFS"
IFS=:
ffaudiosrc=(${opts[audiosrc]})
IFS="$OLDIFS"
unset OLDIFS
if [ ${#ffaudiosrc[@]} -eq 2 ]; then
fffilter_audio="[0:${ffaudiosrc[0]}]pan=stereo:c0=c${ffaudiosrc[1]}:c1=c${ffaudiosrc[1]}[pa]"
ffmap_audio="[pa]"
elif [ ${#ffaudiosrc[@]} -eq 1 ]; then
ffmap_audio="0:${ffaudiosrc[0]}"
else
do_usage
exit 1
fi
#fffilter_video="crop=${opts[crop]:=704:576:8:0},scale=${opts[scale]:=640:360},setsar=1/1"
fffilter_video_deint="${opts[deinterlace]}${opts[deinterlace]:+,}"
fffilter_video="${opts[crop]:+crop=}${opts[crop]}${opts[crop]:+,}"
fffilter_video+="${opts[scale]:+scale=}${opts[scale]}${opts[scale]:+,}"
# hardcoded SAR 1:1
fffilter_video+="setsar=1/1"
ffkeyframes="expr:gte(t,n_forced*${opts[keyint]:=4})"
declare -a FFVENC FFAENC
if [ -n "${opts[ffparams]}" -a -f "${opts[ffparams]}" -a -r "${opts[ffparams]}" ]; then
. "${opts[ffparams]}"
fi
OLDIFS="$IFS"
IFS=:
ffabg=(${opts[audiobargraph]})
IFS="$OLDIFS"
unset OLDIFS
if [ ${#ffabg[@]} -ge 1 ]; then
if [ "${ffabg[0]}" = i ]; then
ffabg_what=(-f mpegts -c:a copy)
elif [ "${ffabg[0]}" = o ]; then
ffabg_what=(-f mpegts)
ffabg_what+=("${FFAENC[@]}")
elif [ "${ffabg[0]}" = r ]; then
ffabg_what=(-f nut -c:a pcm_s16le)
# force vlc input format
VLCOPTS+=(--ffmpeg-format=nut)
else
do_usage
exit 1
fi
# need to split audio if using pan filter
if [ -n "$fffilter_audio" -a ${#ffaudiosrc[@]} -eq 2 ]; then
fffilter_audio="${fffilter_audio%\[pa\]},asplit=2[pa][pa2]"
ffabg_what+=(-map "[pa2]" pipe:4)
else
ffabg_what+=(-map "${ffmap_audio}" pipe:4)
fi
fi
if [ ${#ffabg[@]} -eq 2 ]; then
ffabg_port="${ffabg[1]}"
else
ffabg_port=6514
fi
if [ ${#ffabg[@]} -ge 3 ]; then
ffabg_threshold="${ffabg[2]}"
else
ffabg_threshold=0.01
fi
if [ ${#ffabg[@]} -ge 4 ]; then
ffabg_vol="${ffabg[3]}"
else
ffabg_vol=0
fi
if [ ${#ffabg[@]} -ge 5 ]; then
ffabg_addr="${ffabg[4]}"
else
ffabg_addr=localhost
fi
if [ ${#ffabg[@]} -gt 5 ]; then
do_usage
exit 1
fi
vlc_audiobargraph () {
vlc_abgfifo="$1"
vlc -I dummy --ignore-config --daemon --pidfile="${vlcpidfile}" \
--play-and-exit \
--no-video --aout dummy \
--audio-filter="audiobargraph_a" \
--audiobargraph_a-address="${ffabg_addr}" \
--audiobargraph_a-port="${ffabg_port}" \
--audiobargraph_a-connection_reset=1 \
--audiobargraph_a-bargraph="${ffabg_vol}" \
--audiobargraph_a-bargraph_repetition=22 \
--audiobargraph_a-silence=1 \
--audiobargraph_a-repetition_time=1000 \
--audiobargraph_a-time_window=10000 \
--audiobargraph_a-alarm_threshold="${ffabg_threshold}" \
"${VLCOPTS[@]}" \
"${vlc_abgfifo}"
}
FFOPTS=(-nostats -nostdin -y -re -i "${ffinput}" -filter:"${ffmap_video}" "${fffilter_video_deint}${fffilter_video}" "${FFVENC[@]}" -force_key_frames "${ffkeyframes}" ${fffilter_audio:+-filter_complex} "${fffilter_audio}" "${FFAENC[@]}" -map "${ffmap_video}" -map "${ffmap_audio}" -f flv "${ffoutput}")
if [[ "$ffinput" != file:* ]]; then
unset "FFOPTS[3]"
fi
if [ -n "${ffsnapfile}" ]; then
FFOPTS+=(-f image2 -updatefirst 1 -filter:"${ffmap_video}" "fps=fps=1/5,${fffilter_video}" -map "${ffmap_video}" "${ffsnapfile}")
fi
if [ ${#ffabg[@]} -ne 0 ]; then
FFOPTS+=("${ffabg_what[@]}")
if [ "${daemonize}" = true -o -n "${ffsyslog}" ]; then
vlcsyslogfacility="${ffsyslog%.*}"
VLCOPTS+=(--syslog --syslog-facility="${vlcsyslogfacility:-daemon}")
fi
ffabgfifo=$(mktemp -u "${vlcpidpath}/abgafifo.XXXXXXXXXX")
mkfifo "$ffabgfifo"
if [ $? -ne 0 ]; then
exit 1
fi
if [ "${daemonize}" = true -o -n "${ffsyslog}" ]; then
vlc_audiobargraph "${ffabgfifo}" 2>/dev/null
else
vlc_audiobargraph "${ffabgfifo}"
fi
exec 4>"${ffabgfifo}"
fi
# no daemonize, no syslog
if [ "${daemonize}" != true -a -z "${ffsyslog}" ]; then
echo /usr/bin/ffmpeg ${FFOPTS[@]:2}
/usr/bin/ffmpeg ${FFOPTS[@]:2}
RC=$?
[ ${#ffabg[@]} -ne 0 ] && rm -f "${ffabgfifo}"
exit $RC
fi
coproc /usr/bin/nohup /usr/bin/ffmpeg ${FFOPTS[@]} 2>&1
if [ $? -ne 0 ]; then
exit 1
fi
FFPID=$COPROC_PID
touch "${ffpidfile}" && echo $FFPID > "${ffpidfile}"
if [ $? -ne 0 ]; then
kill -TERM ${FFPID}
fi
# daemonize, (implicit) syslog
if [ "$daemonize" = true ]; then
exec 3<&${COPROC[0]}
{
/usr/bin/logger -p "${ffsyslog:-daemon.notice}" -t "ffmpeg[${FFPID}]"
[ ${#ffabg[@]} -ne 0 ] && rm -f "${ffabgfifo}"
rm -f "${ffpidfile}"
} <&3 &
# no daemonize, explicit syslog
elif [ -n "${ffsyslog}" ]; then
trap 'rm -f "${ffpidfile}"' HUP INT QUIT TERM KILL
[ ${#ffabg[@]} -ne 0 ] && trap 'rm -f "${ffabgfifo}"' HUP INT QUIT TERM KILL
/usr/bin/logger -p "${ffsyslog}" -t "ffmpeg[${FFPID}]" <&${COPROC[0]}
kill $$
fi