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
|
#!/bin/sh
usage() {
echo "Asks for recording type via dmenu."
echo "If there is already a running instance, user will be prompted to end it. "
echo ""
echo "Usage: ${0##*/} [-h] [-a|--audio|audio] [-k|--kill|kill] [-v|--video|video] [-s|--screencast|screencast]"
echo ""
echo "Options:"
echo " - audio : Records only audio"
echo " - kill : Kills existing recording"
echo " - video : Records only screen"
echo " - screencast : Records both audio and screen"
}
getdim() { xrandr | grep -oP '(?<=current ).*(?=,)' | tr -d ' '; }
updateicon() {
echo "$1" >/tmp/recordingicon
pkill -RTMIN+24 "${STATUSBAR:-dwmblocks}"
}
killrecording() {
recpid="$(cat /tmp/recordingpid)"
kill -15 "$recpid"
rm -f /tmp/recordingpid
updateicon ""
pkill -RTMIN+24 "${STATUSBAR:-dwmblocks}"
}
getmonitor() {
xrandr | awk '
/^[^ ]+ connected/ {
name=$1
if (match($0, /([0-9]+)x([0-9]+)\+([0-9]+)\+([0-9]+)/, a)) {
printf "%s %s %s+%s\n", name, a[1] "x" a[2], a[3], a[4]
}
}
'
}
selectmonitor() {
map=$(getmonitor)
[ -n "$map" ] || exit 1
names=$(printf "%s\n" "$map" | awk '{print $1}')
monitor_count=$(printf "%s\n" "$names" | wc -l)
if [ "$monitor_count" -ge 2 ]; then
options=$(printf "%s\nall" "$names")
choice=$(printf "%s\n" "$options" | dmenu -p "Select monitor to record:")
[ -n "$choice" ] || exit 1
if [ "$choice" = "all" ]; then
echo "all"
else
line=$(printf "%s\n" "$map" | awk -v mon="$choice" '$1 == mon')
[ -n "$line" ] || exit 1
res=$(echo "$line" | awk '{print $2}')
pos=$(echo "$line" | awk '{print $3}')
echo "$res+$pos"
fi
else
echo "all"
fi
}
screencast() {
ffmpeg -y \
-f x11grab \
-framerate 30 \
-s "$(getdim)" \
-i "$DISPLAY" \
-r 24 \
-use_wallclock_as_timestamps 1 \
-f alsa -thread_queue_size 1024 -i default \
-c:v h264 \
-crf 0 -preset ultrafast -c:a aac \
"$recordings/screencast-$(date '+%y%m%d-%H%M-%S').mp4" &
echo $! >/tmp/recordingpid
updateicon "⏺️🎙️"
}
screencastselected() {
geometry=$(selectmonitor) || exit 1
if [ "$geometry" = "all" ]; then
screencast
else
ffmpeg -y \
-f x11grab \
-framerate 30 \
-video_size "${geometry%%+*}" \
-i "$DISPLAY+${geometry#*+}" \
-r 24 \
-use_wallclock_as_timestamps 1 \
-f alsa -thread_queue_size 1024 -i default \
-c:v h264 \
-crf 0 -preset ultrafast -c:a aac \
"$recordings/screencast-$(date '+%y%m%d-%H%M-%S').mp4" &
echo $! >/tmp/recordingpid
updateicon "⏺️🎙️"
fi
}
video() {
ffmpeg \
-f x11grab \
-framerate 30 \
-s "$(getdim)" \
-i "$DISPLAY" \
-c:v libx264 -qp 0 -r 30 \
"$recordings/video-$(date '+%y%m%d-%H%M-%S').mkv" &
echo $! >/tmp/recordingpid
updateicon "⏺️"
}
videoselected() {
slop -f "%x %y %w %h" >/tmp/slop
read -r X Y W H </tmp/slop
rm /tmp/slop
ffmpeg \
-f x11grab \
-framerate 30 \
-video_size "$W"x"$H" \
-i :0.0+"$X,$Y" \
-c:v libx264 -qp 0 -r 30 \
"$recordings/box-$(date '+%y%m%d-%H%M-%S').mkv" &
echo $! >/tmp/recordingpid
updateicon "⏺️"
}
webcamselect() {
cameras=$(
v4l2-ctl --list-devices | awk '
BEGIN { RS=""; FS="\n" }
{
name = $1;
sub(/ \(.*$/, "", name);
gsub(/^[ \t]+|[ \t]+$/, "", name);
for (i=2; i<=NF; i++) {
if ($i ~ /\/dev\/video/) {
gsub(/^[ \t]+/, "", $i);
print name "|" $i;
break;
}
}
}'
)
names=$(echo "$cameras" | cut -d '|' -f1)
choice=$(echo "$names" | dmenu -i -p 'Choose a camera:')
camera=$(echo "$cameras" | awk -F '|' -v sel="$choice" '$1 == sel {print $2}')
}
webcamhidef() {
[ -z "$camera" ] && exit
ffmpeg \
-display_hflip \
-f v4l2 \
-i "$camera" \
-video_size 1920x1080 \
"$recordings/webcam-$(date '+%y%m%d-%H%M-%S').mkv" &
echo $! >/tmp/recordingpid
updateicon "🎥"
}
webcam() {
[ -z "$camera" ] && exit
ffmpeg \
-display_hflip \
-f v4l2 \
-i "$camera" \
-video_size 640x480 \
"$recordings/webcam-$(date '+%y%m%d-%H%M-%S').mkv" &
echo $! >/tmp/recordingpid
updateicon "🎥"
}
audio() {
ffmpeg \
-f alsa -i default \
-c:a flac \
"$recordings/audio-$(date '+%y%m%d-%H%M-%S').flac" &
echo $! >/tmp/recordingpid
updateicon "🎙️"
}
askrecording() {
choice=$(printf "screencast\\nscreencast selected\\nvideo\\nvideo selected\\naudio\\nwebcam\\nwebcam (hi-def)" | dmenu -i -p "Select recording style:")
case "$choice" in
screencast) screencast ;;
"screencast selected") screencastselected ;;
audio) audio ;;
video) video ;;
"video selected") videoselected ;;
webcam) webcamselect && webcam ;;
"webcam (hi-def)") webcamselect && webcamhidef ;;
*) exit ;;
esac
}
asktoend() {
response=$(printf "No\\nYes" | dmenu -i -p "Recording still active. End recording?")
[ -z "$response" ] && exit
[ "$response" = "Yes" ] && killrecording
}
recordings="${XDG_VIDEOS_DIR:-$HOME/Videos}/recordings"
[ -d "$recordings" ] || mkdir -p "$recordings"
case "$1" in
-h | --help | help) usage && exit 0 ;;
-a | --audio | audio) audio ;;
-k | --kill | kill) killrecording ;;
-s | --screencast | screencast) screencast ;;
-ss | --screencast-selected | "screencast selected") screencastselected ;;
-v | --video | video) video ;;
-vs | --video-selected | "video selected") videoselected ;;
*) ([ -f /tmp/recordingpid ] && asktoend && exit) || askrecording ;;
esac
|