aboutsummaryrefslogtreecommitdiff
path: root/dotfiles/.local/bin/swayrecord
blob: 5c1c2a6f7945463db637bbbb70b2d72b7a1b11ee (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
#!/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

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

trap finishrecording SIGINT

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.mp3" \
    -codec copy \
    -shortest \
    "$VREC_DIR/screencast-$OUTPUT.mp4"
  rm -f "$VREC_DIR/temp-$OUTPUT.mp4"
  rm -f "$VREC_DIR/temp-$OUTPUT.mp3"
  notify-send "📼 Recording" \
    "Screencast recording stopped and saved at "$VREC_DIR/screencast-$OUTPUT.mp4""
}

finishrecording () {
  case "$TYPE" in
    video) finishvideo;;
    audio) finishaudio;;
    screencast) finishscreencast;;
  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 -f "$VREC_DIR/video-$OUTPUT.mp4" &
  VIDEO_PID=$!
  echo $$ > ~/.cache/recordingpid
  echo "🔴" > ~/.cache/recordingicon
  notify-send "🔴 Recording" "Screen recording started"
  killall -39 waybar
  wait
}

screencast () {
  wf-recorder -f "$VREC_DIR/temp-$OUTPUT.mp4" &
  VIDEO_PID=$!
  ffmpeg -f alsa -i default -c:a mp3 "$VREC_DIR/temp-$OUTPUT.mp3" &
  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 flac "$AREC_DIR/$OUTPUT.flac" &
  AUDIO_PID=$!
  echo $$ > ~/.cache/recordingpid
  echo "🎙" > ~/.cache/recordingicon
  notify-send "🎙 Recording" "Audio recording started"
  killall -39 waybar
  wait
}

askrecording () {
  TYPE="$(printf "video\nscreencast\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;;
    audio) audio;;
    *) exit;;
  esac
}

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

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