-
Notifications
You must be signed in to change notification settings - Fork 1
/
record
executable file
·286 lines (249 loc) · 5.7 KB
/
record
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
#!/bin/bash
CARD=0
PRESET=hd_mjpeg
QUALITY=90
VBITRATE=4500k
FILE_PATH=/mnt/video-storage
STREAM_URL=rtmp://video1.rpitv.org:1935/rpitv-live/rpitv-live
BUG=0
BUGFILE=/home/rpitv/livebug.svg
PNGBUG=/home/armena/bug.png
FILTERARGS=()
MONO_STREAM=
FFMPEG=/home/armena/ffmpeg/ffmpeg
FFMPEG_PRESET=veryfast
AVPINPUT_DECKLINK=/home/armena/exacore_work/avspipe/avpinput_decklink
HTTPKEYER=/home/armena/exacore_work/keyer/http_keyer.rb
SCOREBOARD=/opt/scoreboard/scoreboard.sh
set -eux
# set some defaults based on whether run as 'record' or 'stream'
case $0 in
*stream)
PRESET=h264_1080
CARD=1
usage() {
stream_usage
}
;;
*)
usage() {
record_usage
}
;;
esac
record_usage() {
cat >&2 << EOF
usage: $0 [-q n] [-p preset] [-c n] [-b] [-B FILE] production_title
Record video from Blackmagic card to file.
-p preset: use capture preset (default $PRESET, also: sd_mjpeg sd_mpeg)
-c n: use capture card n (default $CARD)
-q n: for JPEG presets, use quality n (default $QUALITY)
-b: insert bug (does nothing on M-JPEG presets)
-B FILE: use svg FILE for the bug (default $BUGFILE)
Files are put into $FILE_PATH.
The date is automatically prepended to the production_title.
EOF
}
stream_usage() {
cat >&2 << EOF
usage: $0 [-p preset] [-c n] [-b] [-B FILE] [url]
Stream video from Blackmagic card to the given url.
-p preset: use capture preset (default $PRESET, also: ...)
-c n: use capture card n (default $CARD)
-b: insert bug
-B FILE: use svg FILE for the bug (default $BUGFILE)
-r RATE: set video bit rate (default $RATE)
url: stream destination (default $STREAM_URL)
EOF
}
is_numeric() {
case $1 in
''|*[!0-9]*)
return 1
;;
*)
return 0
;;
esac
}
error_exit() {
echo "error: $@" >&2
exit 1
}
mjpeg_input() {
local CHANNELS=8
local arg
# process options
OPTIND=1
while getopts "c:" arg; do
case $arg in
c)
is_numeric $OPTARG || error_exit "invalid number of channels"
CHANNELS=$OPTARG
;;
esac
done
shift $((OPTIND-1))
# -probesize 32 on audio, which shouldn't need probing anyway
# this should avoid deadlocks
exec $AVPINPUT_DECKLINK -R "${FILTERARGS[@]}" -c $CARD \
-j -q $QUALITY --channels $CHANNELS \
"$FFMPEG \
-thread_queue_size 512 -probesize 32 \
-f s16le -ar 48000 -ac $CHANNELS -i pipe:%a \
-thread_queue_size 512 -f mjpeg -r 29.97 -i pipe:%v $@"
}
raw_input() {
local CHANNELS=8
local arg
local MONO_MIX=
OPTIND=1
while getopts "mc:" arg; do
case $arg in
c)
is_numeric $OPTARG || error_exit "invalid number of channels"
CHANNELS=$OPTARG
;;
m)
MONO_MIX=-m
;;
esac
done
shift $((OPTIND-1))
# -probesize 32 is very important here especially on the audio input.
# this tells ffmpeg to read less data when starting up.
exec $AVPINPUT_DECKLINK -R "${FILTERARGS[@]}" $MONO_MIX \
-c $CARD --channels $CHANNELS \
"$FFMPEG -thread_queue_size 512 -probesize 32 \
-f s16le -ar 48000 -ac $CHANNELS -i pipe:%a \
-thread_queue_size 512 -probesize 32 \
-f rawvideo -r 29.97 -pix_fmt uyvy422 \
-s 1920x1080 -i pipe:%v $@"
}
hd_mjpeg() {
mjpeg_input -- "-f mov -r 29.97 -vcodec copy -acodec copy /mnt/video-storage/$1.mov"
}
sd_mjpeg() {
# record to SD M-JPEG
raw_input -- "-s 720x480 -f mov -r 29.97 -vcodec mjpeg -q:v 4 \
-acodec copy /mnt/video-storage/$1.mov"
}
sd_mpeg() {
raw_input -- "-s 720x480 -r 29.97 -vcodec mpeg2video -b:v 1600k \
-acodec mp2 -ac 2 -b:a 192k -ar 44100 -bufsize 32M \
-maxrate 1600k -minrate 1600k /mnt/video-storage/$1.mpg"
}
h264_1080() {
raw_input $MONO_STREAM -c 2 -- "-vf yadif -s 1920x1080 -pix_fmt yuv420p \
-vcodec libx264 -acodec aac -ac 2 -ar 44100 \
-preset $FFMPEG_PRESET -maxrate $VBITRATE -bt 40k -bufsize 32M -threads 8 -r 29.97 \
-nr 70 -g 60 -ab 256k $1"
}
h264_1080_qsv() {
raw_input $MONO_STREAM -c 2 -- "-vaapi_device /dev/dri/renderD128 \
-vf 'yadif,format=nv12,hwupload' \
-vcodec h264_vaapi -acodec aac -ac 2 -ar 44100 \
-b:v $VBITRATE -r 29.97 -g 60 -ab 256k $1"
}
h264_480() {
raw_input -c 2 -- "-vf yadif -f flv -vf crop=1440:1080 -s 640x480 -pix_fmt yuv420p \
-vcodec libx264 -acodec aac -ac 2 -ar 44100 \
-vpre $FFMPEG_PRESET -b:v $VBITRATE -bt:v 40k -threads 4 -r 29.97 \
-nr 70 -g 60 -ab 64k $1"
}
setup_keyer() {
case "$1" in
pnghttp)
FILTERARGS+=("--cg-x" "0" "--cg-y" "0" "--png-cg" "$HTTPKEYER -h :: -p 4567")
;;
pngbug)
FILTERARGS+=("--cg-x" "0" "--cg-y" "0" "--png-cg" "$HTTPKEYER -h :: -p 3005 -f $PNGBUG")
;;
scoreboard)
FILTERARGS+=("--cg-x" "150" "--cg-y" "50" "--svg-cg" "$SCOREBOARD")
;;
*)
error_exit "unknown key type"
;;
esac
}
OPTIND=1
while getopts "c:p:q:bB:r:mX:o:" arg; do
case $arg in
c)
# validate if OPTARG is numeric
is_numeric $OPTARG || error_exit "invalid card index"
CARD=$OPTARG
;;
q)
is_numeric $OPTARG || error_exit "invalid quality"
[ $OPTARG -ge 0 -a $OPTARG -le 100 ] || error_exit "invalid quality"
QUALITY=$OPTARG
;;
p)
# validate that this is a preset we know
case $OPTARG in
hd_mjpeg|sd_mjpeg|sd_mpeg|h264_1080|h264_1080_qsv|h264_480)
;;
*)
error_exit "unknown preset"
;;
esac
PRESET=$OPTARG
;;
b)
BUG=1
;;
B)
[ -r "$OPTARG" && -f "$OPTARG" ] || error_exit "bug not found or not readable"
BUGFILE="$OPTARG"
BUG=1
;;
r)
VBITRATE="$OPTARG"
;;
m)
MONO_STREAM=-m
;;
X)
setup_keyer "$OPTARG"
;;
o)
FILTERARGS+=("-o" "$OPTARG")
;;
\?)
usage
exit 1
;;
:)
usage
exit 1
;;
esac
done
[ $BUG -eq 1 ] && FILTERARGS+=(-b "$BUGFILE")
shift $((OPTIND-1))
case "$0" in
*stream)
# streaming mode
if [ "$#" -gt 1 ]; then
echo "USAGE"
usage
exit 1
fi
# override stream url if supplied
[ "$#" -eq 1 ] && STREAM_URL="$1"
# run chosen preset
$PRESET "$STREAM_URL"
;;
*)
# recording mode
if [ "$#" -ne 1 ]; then
usage
exit 1
fi
# recording mode: generate filename
DATE=`date +%Y%m%d`
FILENAME="${DATE}_$1"
$PRESET "$FILENAME"
esac