aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/home/.local/bin/record
blob: 233fe7f23e7efdc43fcd896f59ece3aec532c930 (plain) (blame)
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
#!/usr/bin/env sh

pid_file="/tmp/recordpid"
log_file="/tmp/record.log"

pid_exists() {
    test -r "$pid_file"
}

is_running() {
    if pid_exists; then
        ps "$(cat "$pid_file")" >/dev/null 2>&1 || return 1
    else
        return 1
    fi
}

start() {
    notify-send -t 500 "Record started!" &
    sleep 0.5

    ffmpeg -f x11grab -draw_mouse 0 -framerate 60 -video_size 1920x1080 -i "$DISPLAY" -c:v hevc_nvenc \
        "$HOME/vids/$(date '+%y%m%d-%H%M-%S').mkv" >"$log_file" 2>&1 &
    echo "$!" >"$pid_file"
}

stop() {
    pid_exists || exit 1
    pid="$(cat "$pid_file")"
    # kill with SIGTERM, allowing finishing touches.
    kill "$pid"
    # even after SIGTERM, ffmpeg may still run, so SIGKILL it.
    sleep 3
    is_running && kill -9 "$pid"
    rm -f "$pid_file"
    notify-send "Record stopped!"
}

toggle() {
    if is_running; then
        echo "Stopping record"
        stop
    else
        echo "Starting record"
        start
    fi
    echo
    status
}

status() {
    if is_running; then
        echo "Recording with PID $(cat "$pid_file")"
        echo "Check the logs at"
        echo
        echo "$log_file"
        echo
    else
        echo "Record inactive"
    fi
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    toggle)
        toggle
        ;;
    status)
        status
        ;;
    *)
        toggle
        ;;
esac