aboutsummaryrefslogtreecommitdiff
path: root/dotfiles/.local/bin/swayrecord
blob: 91f6d3146a8edbf3842a4af750bc281d016ec34f (plain)
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
#!/bin/sh

# Source settings from .bemenurc
. ~/.config/bemenurc

if ! [ -d $VREC_DIR ]; then
	mkdir -p $VREC_DIR
fi
if ! [ -d $AREC_DIR ]; then
	mkdir -p $AREC_DIR
fi

MONITOR=""
OUTPUT="$(date '+%y%m%d-%H%M-%S')"
TYPE="$1"
VIDEO_PID=""
AUDIO_PID=""

trap finishrecording INT

finishvideo () {
  kill -2 "$VIDEO_PID"
  notify-send "🔴 Recording" \
    "Screen recording stopped and saved at "$VREC_DIR"/video-"$OUTPUT".mp4"
}

finishaudio () {
  kill -2 "$AUDIO_PID"
  notify-send "🎙 Recording" \
    "Audio recording stopped and saved at "$AREC_DIR/$OUTPUT.flac""
}

finishscreencast () {
  kill -2 "$VIDEO_PID"
  kill -2 "$AUDIO_PID"
  notify-send "📼 Recording" "One moment, screencast is being encoded..."
  ffmpeg -i "$VREC_DIR/temp-$OUTPUT.mp4" \
    -i "$VREC_DIR/temp-$OUTPUT.opus" \
    -codec copy \
    -shortest \
    "$VREC_DIR/screencast-$OUTPUT.mp4"
  rm -f "$VREC_DIR/temp-$OUTPUT.mp4"
  rm -f "$VREC_DIR/temp-$OUTPUT.opus"
  notify-send "📼 Recording" \
    "Screencast recording stopped and saved at "$VREC_DIR/screencast-$OUTPUT.mp4""
}

finishscreenshare() {
  kill -2 "$VIDEO_PID"
  notify-send "📺 Recording" \
    "Screen sharing stopped."
}

finishrecording () {
  case "$TYPE" in
    video) finishvideo;;
    audio) finishaudio;;
    screencast) finishscreencast;;
    screenshare) finishscreenshare;;
  esac
  rm -f ~/.cache/recordingpid
  rm -f ~/.cache/recordingicon
  killall -39 waybar
}

killrecording () {
  RECPID="$(cat "$HOME/.cache/recordingpid")"
  kill -2 "$RECPID"
  exit
}

video () {
  wf-recorder -o "$MONITOR" -f "$VREC_DIR/video-$OUTPUT.mp4" &
  VIDEO_PID=$!
  echo $$ > ~/.cache/recordingpid
  echo "🔴" > ~/.cache/recordingicon
  notify-send "🔴 Recording" "Screen recording started"
  killall -39 waybar
  wait
}

screenshare () {
  # Hack to share screen in certain apps under wayland wlroots-based compositors
  loopbackcam="$(v4l2-ctl --list-devices | \
  	grep v4l2loopback -m1 -A1 | \
  	sed -e '1d' -e 's/\s//g')"
  wf-recorder -o "$MONITOR" -x yuv420p -c rawvideo -m v4l2 -f $loopbackcam &
  VIDEO_PID=$!
  echo $$ > ~/.cache/recordingpid
  echo "📺" > ~/.cache/recordingicon
  notify-send "📺 Recording" "Screen share to fake webcam started"
  killall -39 waybar
  wait
}

screencast () {
  wf-recorder -o "$MONITOR" -f "$VREC_DIR/temp-$OUTPUT.mp4" &
  VIDEO_PID=$!
  ffmpeg -f alsa -i default -c:a libopus "$VREC_DIR/temp-$OUTPUT.opus" &
  AUDIO_PID=$!
  echo $$ > ~/.cache/recordingpid
  echo "📼" > ~/.cache/recordingicon
  notify-send "📼 Recording" "Screencast recording started"
  killall -39 waybar
  wait
}

audio () {
  ffmpeg -f alsa -i default -c:a libopus "$AREC_DIR/$OUTPUT.opus" &
  AUDIO_PID=$!
  echo $$ > ~/.cache/recordingpid
  echo "🎙" > ~/.cache/recordingicon
  notify-send "🎙 Recording" "Audio recording started"
  killall -39 waybar
  wait
}

askrecording () {
  TYPE="$(printf "video\nscreencast\nscreenshare\naudio\n" | bemenu -l 6 -i -p "What should be recorded?" --tf="$BEMENU_TF" --tb="$BEMENU_NB" --fb="$BEMENU_NB" --fn="$BEMENU_FN" --nb="$BEMENU_NB" --nf="$BEMENU_NF" --hf="$BEMENU_HF" --hb="$BEMENU_HB")"
  case "$TYPE" in
    video) video;;
    screencast) screencast;;
  	screenshare) screenshare;;
    audio) audio;;
    *) exit;;
  esac
}

if [ -f "$HOME/.cache/recordingpid" ]; then
  killrecording
  exit
fi

if [ $(swaymsg -t get_outputs | jq '. | length') -gt 1 ]; then
MONITOR=$(swaymsg -t get_outputs | grep -E "name" | sed "s/^.*\"name\": \"//g;s/\",$//g;" | bemenu -p "Select display for capture: " --tf="$BEMENU_TF" --tb="$BEMENU_NB" --fb="$BEMENU_NB" --fn="$BEMENU_FN" --nb="$BEMENU_NB" --nf="$BEMENU_NF" --hf="$BEMENU_HF" --hb="$BEMENU_HB" --monitor="$BEMENU_MONITOR")
else
	MONITOR="$(swaymsg -t get_outputs | jq -r '.[0]["name"]')"
fi

case $1 in
  video) video;;
  screencast) screencast;;
  screenshare) screenshare;;
  audio) audio;;
  kill) killrecording;;
  *) askrecording;;
esac