/
ffcast.bash
executable file
·515 lines (459 loc) · 13.5 KB
/
ffcast.bash
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
#!/bin/bash
#
# FFcast @VERSION@
# Copyright (C) 2011 lolilolicon <lolilolicon@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
set -e
shopt -s extglob
readonly progname=ffcast progver='@VERSION@'
readonly cast_cmd_pattern='@(ffmpeg|byzanz-record|recordmydesktop)'
declare -- region_select_action
declare -i borderless=1 mod16=0 print_geometry_only=0 use_format_string=0
declare -i verbosity=0
#---
# Functions
_msg() {
local prefix=$1
shift || return 0
local fmt=$1
shift || return 0
printf '%s' "$prefix"
printf -- "$fmt\n" "$@"
}
_quote_cmd_line() {
local prefix=$1
shift || return 0
local cmd=$1
shift || return 0
printf '%s' "$prefix"
printf '%q' "$cmd"
(( $# )) && printf ' %q' "$@"
printf '\n'
}
debug_dryrun() {
(( verbosity >=2 )) || return 0
_quote_cmd_line 'debug: command: ' "$@"
} >&2
debug_run() {
debug_dryrun "$@" && "$@"
}
debug() {
(( verbosity >= 2 )) || return 0
_msg 'debug: ' "$@"
} >&2
verbose() {
(( verbosity >= 1 )) || return 0
_msg 'verbose: ' "$@"
} >&2
msg() {
(( verbosity >= 0 )) || return 0
_msg ':: ' "$@"
} >&2
warn() {
(( verbosity >= -1 )) || return 0
_msg 'warning: ' "$@"
} >&2
error() {
(( verbosity >= -1 )) || return 0
_msg 'error: ' "$@"
} >&2
format_to_string() {
local fmt=$1 str c
printf %s "$fmt" | {
while IFS= read -r -n 1 -d '' c; do
if [[ $c == '%' ]]; then
IFS= read -r -n 1 -d '' c || :
case $c in
'%')
str+=%
;;
'd')
str+=$DISPLAY
;;
'h')
str+=$h
;;
'w')
str+=$w
;;
'x')
str+=$_x
;;
'y')
str+=$_y
;;
'X')
str+=$x_
;;
'Y')
str+=$y_
;;
*)
str+=%$c
;;
esac
else
str+=$c
fi
done
printf %s "$str"; }
}
list_cast_cmds() {
local -a cmds
IFS='|' read -a cmds <<< "${cast_cmd_pattern:2: -1}"
printf "%s\n" "${cmds[@]}"
}
parse_geospec_get_corners() {
local geospec=$1
local _x _y x_ y_ w h
local n='?([-+])+([0-9])'
local m='?(-)+([0-9])'
local N='+([0-9])'
# strip whitespaces
IFS=$' \t' read -r geospec <<< "$geospec"
case $geospec in
$n,$n+([' \t'])$n,$n) # x1,y1 x2,y2
IFS=$', \t' read _x _y x_ y_ <<< "$geospec"
;;
${N}x${N}\+${m}\+${m}) # wxh+x+y
IFS='x+' read w h _x _y <<< "$geospec"
(( x_ = rootw - _x - w )) || :
(( y_ = rooth - _y - h )) || :
;;
*)
error "invalid geometry specification: \`%s'" "$geospec"
return 1
;;
esac
printf '%d,%d %d,%d' $_x $_y $x_ $y_
}
region_intersect_corners() {
local corners
local _x _y x_ y_
local _X _Y X_ Y_
# Initialize variable- otherwise bash will fallback to 0
IFS=' ,' read _X _Y X_ Y_ <<< "$1"
shift || return 1
for corners in "$@"; do
IFS=' ,' read _x _y x_ y_ <<< "$corners"
(( _X = _x > _X ? _x : _X )) || :
(( _Y = _y > _Y ? _y : _Y )) || :
(( X_ = x_ > X_ ? x_ : X_ )) || :
(( Y_ = y_ > Y_ ? y_ : Y_ )) || :
done
printf '%d,%d %d,%d' $_X $_Y $X_ $Y_
}
region_union_corners() {
local corners
local _x _y x_ y_
local _X _Y X_ Y_
# Initialize variable- otherwise bash will fallback to 0
IFS=' ,' read _X _Y X_ Y_ <<< "$1"
shift || return 1
for corners in "$@"; do
IFS=' ,' read _x _y x_ y_ <<< "$corners"
(( _X = _x < _X ? _x : _X )) || :
(( _Y = _y < _Y ? _y : _Y )) || :
(( X_ = x_ < X_ ? x_ : X_ )) || :
(( Y_ = y_ < Y_ ? y_ : Y_ )) || :
done
printf '%d,%d %d,%d' $_X $_Y $X_ $Y_
}
select_region_get_corners() {
msg "%s" "please select a region using mouse"
xrectsel_get_corners
}
select_window_get_corners() {
msg "%s" "please click once in target window"
LC_ALL=C xwininfo | xwininfo_get_corners
}
# stdout: x1,y1 x2,y2
xrectsel_get_corners() {
# Note: requires xrectsel 0.3
xrectsel "%x,%y %X,%Y"
}
# stdin: xwininfo output (locale: C)
# stdout: ${width}x${height}
xwininfo_get_dimensions() {
local line
local w h
while IFS=$' \t' read -r line; do
if [[ $line == 'Width: '+([0-9]) ]]; then
w=${line#'Width: '}
elif [[ $line == 'Height: '+([0-9]) ]]; then
h=${line#'Height: '}
else
continue
fi
if (( w && h )); then
printf '%dx%d' $w $h
return
fi
done
return 1
}
# stdin: xwininfo output (locale: C)
# stdout: x1,y1 x2,y2
xwininfo_get_corners() {
local line
local _x _y x_ y_ b
local n='-?[0-9]+'
local corners="^Corners: *\\+($n)\\+($n) *-$n\\+$n *-($n)-($n) *\\+$n-$n\$"
# Note: explicitly set IFS to ensure stripping of whitespaces
while IFS=$' \t' read -r line; do
if [[ $line == 'Border width: '+([0-9]) ]]; then
b=${line#'Border width: '}
elif [[ $line =~ $corners ]]; then
_x=${BASH_REMATCH[1]}
_y=${BASH_REMATCH[2]}
x_=${BASH_REMATCH[3]}
y_=${BASH_REMATCH[4]}
else
continue
fi
[[ -n $_x ]] || continue
if (( ! borderless )); then
:
elif [[ -n $b ]]; then
(( _x += b )) || :
(( _y += b )) || :
(( x_ += b )) || :
(( y_ += b )) || :
else
continue
fi
printf '%d,%d %d,%d' $_x $_y $x_ $y_
return
done
return 1
}
#---
# Process arguments passed to ffcast
usage() {
cat <<EOF
$progname $progver
Usage: ${0##*/} [arguments] [[%] command line]
arguments:
-s select a rectangular region by mouse
-w select a window by mouse click
-b include window borders hereafter
-m trim selected region to be mod 16
-p print region geometry only
-l list supported screencast commands
-q less verbose
-v more verbose
-h print this help and exit
<geospec> geometry specification
If no region-selecting argument is passed, select fullscreen.
All the arguments can be repeated, and are processed in order.
For example, -vv is more verbose than -v.
EOF
if (( verbosity < 1 )); then
cat <<EOF
Use \`${0##*/} -vh' for detailed help.
EOF
else
cat <<EOF
Any number of selections are supported, e.g., -sw is valid.
All selected regions are combined by union.
<geospec> is usually not used. It is intended as a way to use external
region selection commands. For example,
${0##*/} "\$(xrectsel '%x,%y %X,%Y')"
<geospec> must conform to one of the following syntax:
- x1,y1 x2,y2 (x1,y1) = (left,top) offset of the region
(x2,y2) = (right,bottom) offset of the region
- wxh+x+y (wxh) = dimensions of the region
(+x+y) = (+left+top) offset of the region
The command line can be specified in either of the following two ways:
1) Specify a recognized command name after arguments. The predefined
command-specific x11grab options are inserted into the command line,
either replacing the first instance of \`--' or, if no \`--' is found,
after the command name. For example,
${0##*/} -s ffmpeg -r 25 -- -f alsa -i hw:0 -vcodec libx264 cast.mkv
2) Specify a single \`%' after arguments, followed by any command line.
The format strings %w, %h, %x, %y, %X, %Y are replaced with the width,
height, left-, top- right- and bottom-offset of the selected region,
respectively; %d is replaced with the DISPLAY environment variable.
A literal \`%' must be escaped as \`%%' where necessary. For example,
${0##*/} -w % ${0##*/} -p %wx%h+%x+%y # Yay for recursion ;)
If no command line is specified, a default ffmpeg command line is used.
EOF
fi
exit $1
}
OPTIND=1
while getopts 'bhlmpqsvw' opt; do
case $opt in
h)
usage 0
;;
l)
list_cast_cmds
exit
;;
m)
mod16=1
;;
s)
region_select_action+='s'
;;
w)
region_select_action+='w'
;;
b)
region_select_action+='b'
;;
p)
print_geometry_only=1
;;
q)
(( verbosity-- )) || :
;;
v)
(( verbosity++ )) || :
;;
esac
done
shift $(( OPTIND -1 ))
#---
# Process region geometry
declare rootw=0 rooth=0 _x=0 _y=0 x_=0 y_=0 w=0 h=0
IFS=x read root{w,h} <<< "$(LC_ALL=C xwininfo -root | xwininfo_get_dimensions)"
# Note: this is safe because xwininfo_get_dimensions ensures that its output is
# either {int}x{int} or null, a random string like "rootw" is impossible.
if ! (( rootw && rooth )); then
# %d is OK even if rootw or rooth is null, in which case 0 is printed
error 'invalid root window dimensions: %dx%d' "$rootw" "$rooth"
exit 1
fi
declare -- i=0
declare -a corners_list
while (( $# )); do
if [[ $1 == '%' ]]; then
use_format_string=1
shift
break
elif [[ $1 == $cast_cmd_pattern ]]; then
break
fi
# The rest are geometry specifications
corners_list[i++]=$(parse_geospec_get_corners "$1")
debug "corners: %s" "${corners_list[-1]}"
shift
done
while read -n 1; do
case $REPLY in
's')
corners_list[i++]=$(select_region_get_corners)
debug "corners: %s" "${corners_list[-1]}"
;;
'w')
corners_list[i++]=$(select_window_get_corners)
debug "corners: %s" "${corners_list[-1]}"
;;
'b')
borderless=0
debug "windows: now including borders"
;;
esac
done <<< "$region_select_action"
if (( i )); then
corners=$(region_union_corners "${corners_list[@]}")
debug "corners union all selections: %s" "${corners}"
corners=$(region_intersect_corners "$corners" "0,0 0,0")
debug "corners rootwin intersection: %s" "${corners}"
IFS=' ,' read _x _y x_ y_ <<< "$corners"
fi
if ! (( (w = rootw - _x - x_) > 0 )); then
error 'region: invalid width: %d' "$w"
exit 1
fi
if ! (( (h = rooth - _y - y_) > 0 )); then
error 'region: invalid height: %d' "$h"
exit 1
fi
if (( print_geometry_only )); then
printf "%dx%d+%d+%d\n" $w $h $_x $_y
exit
fi
# x264 requires frame size divisible by 2, mod 16 is said to be optimal.
# Adapt by reducing- expanding could exceed screen edges, and would not
# be much beneficial anyway.
(( mod = mod16 ? 16 : 2 ))
w_old=$w
h_old=$h
if ! (( w = mod * (w / mod) )); then
error 'region: width too small for mod%d: %d' $mod $w_old
exit 1
fi
if ! (( h = mod * (h / mod) )); then
error 'region: height too small for mod%d: %d' $mod $h_old
exit 1
fi
if (( w < w_old )); then
verbose 'region: trim width from %d to %d' $w_old $w
fi
if (( h < h_old )); then
verbose 'region: trim height from %d to %d' $h_old $h
fi
#---
# Inject selection geometry into cast command line and execute
declare -- cast_cmd=$1
declare -a cast_args
if shift; then
if (( use_format_string )); then
while (( $# )); do
cast_args+=("$(format_to_string "$1")")
shift
done
else
declare -a x11grab_opts
case $cast_cmd in
byzanz-record)
x11grab_opts=(--x="$_x" --y="$_y" --width="$w" --height="$h" \
--display="$DISPLAY")
;;
ffmpeg)
x11grab_opts=(-f x11grab -s "${w}x$h" -i "$DISPLAY+$_x,$_y")
;;
recordmydesktop)
x11grab_opts=(-display "$DISPLAY" -width "$w" -height "$h")
# As of recordMyDesktop 0.3.8.1, x- and y-offsets default to 0,
# but -x and -y don't accept 0 as an argument. #FAIL
(( _x )) && x11grab_opts+=(-x "$_x")
(( _y )) && x11grab_opts+=(-y "$_y")
;;
*)
error "invalid cast command: \`%s'" "$cast_cmd"
exit 1
;;
esac
while (( $# )) && [[ $1 != '--' ]]; do
cast_args+=("$1")
shift
done
if shift; then
cast_args+=("${x11grab_opts[@]}" "$@")
else
cast_args=("${x11grab_opts[@]}" "${cast_args[@]}")
fi
fi
else
cast_cmd=ffmpeg
cast_args=(-r 25 -f x11grab -s "${w}x$h" -i "$DISPLAY+$_x,$_y" -vcodec
libx264 "$(printf '%s-%(%Y%m%d-%H%M%S)T.mkv' "$progname" -1)")
fi
debug_run exec "$cast_cmd" "${cast_args[@]}"
# vim:ts=4:sw=4:et:cc=80: