Screen Record

Post at — Sep 04, 2024

A shell script to record screen, region or window in linux

This script record the screen with the wf-recorder tool, it can be the hole screen or a specific area of it. The script is going to store the video in /tmp/screencast.mp4, this script only store one video, personally I think that have more than one record is a mess, more if you only wants to share a quick screencast to telegram, irc, cinny or even to share it through 0x0.st.

Description and main variables:

1
2
3
4
5
6
# Created By: Javier Pacheco - javier@jpacheco.xyz
# Created On: 29/03/24
# Project: Screen recorder in wayland
# Dependencies: wf-recorder wl-copy and a launcher like dmenu, fuzzel, etc.

SOUND_CARD=$(pactl list sources | awk '/Name/ && /.monitor/ {print $2}')

Recording functions:

Those functions have enable the sound recording, so if you have some music, video, etc running it will record the sound but not the mic.

screencast:

This option is going to record a specific area of the screen. This area is going to be specified by slurp.

1
2
3
screencast() {
   wf-recorder --audio=$SOUND_CARD -f /tmp/screencast.mp4
}

area:

This option is going to record a specific area of the screen. This area is going to be specified by slurp.

1
2
3
area() {
   wf-recorder --audio=$SOUND_CARD -g "$(slurp)" -f /tmp/screencast.mp4
}

Main functions:

These functions are tools that ensures this script works correctly. For example if all ready have a existing file recorded, it will removed to record a new one, because this script only will create one video always, and also have a function to kill the process when it finished.

check internet connection:

1
2
3
check_connection() {
    ping -c 1 google.com 1> /dev/null 2>&1
}

share:

This option is going to upload the video to 0x0.st and copy the url to the clipboard using wc-copy.

1
2
3
4
share() {
   notify-send "uploading.." "video is upoading to 0x0.st"
   curl -F "file=@/tmp/screencast.mp4" https://0x0.st | wl-copy && notify-send "Video stored in 0x0.st"
}

Kill existing process:

1
2
3
4
5
6
7
8
9
kill_proc(){
    pkill --signal SIGINT wf-recorder
    if [ $? -eq 0 ];
    then
        notify-send "Video stored" "Video was stored in /tmp/screencast.mp4"
        pkill --signal SIGINT wf-recorder
        exit 0
    fi
}

Remove existing video:

1
2
3
remove_vid() {
    [ -f /tmp/screencast.mp4 ] && rm /tmp/screencast.mp4
}

Sequence:

This is were the scripts actually starts, first of all look if the script is already running, if not then ask for a recording option:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
kill_proc

OPT=$(printf "screencast\narea\nshare\nquit" | fuzzel --dmenu -p 'Select an option: ' )
case $OPT in
    'screencast')
        sleep 1
        remove_vid
        sleep 1
        screencast;;
    'area')
        sleep 1
        remove_vid
        sleep 1
        area;;
    'share')
        check_connection && share || notify-send "Error" "check your internet connection" ;;
    *|quit) exit 0;;
esac

The whole code.

 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
# Created By: Javier Pacheco - javier@jpacheco.xyz
# Created On: 29/03/24
# Project: Screen recorder in wayland
# Dependencies: wf-recorder wl-copy and a launcher like dmenu, fuzzel, etc.

SOUND_CARD=$(pactl list sources | awk '/Name/ && /.monitor/ {print $2}')

screencast() {
   wf-recorder --audio=$SOUND_CARD -f /tmp/screencast.mp4
}

area() {
   wf-recorder --audio=$SOUND_CARD -g "$(slurp)" -f /tmp/screencast.mp4
}

check_connection() {
    ping -c 1 google.com 1> /dev/null 2>&1
}

share() {
   notify-send "uploading.." "video is upoading to 0x0.st"
   curl -F "file=@/tmp/screencast.mp4" https://0x0.st | wl-copy && notify-send "Video stored in 0x0.st"
}

kill_proc(){
    pkill --signal SIGINT wf-recorder
    if [ $? -eq 0 ];
    then
        notify-send "Video stored" "Video was stored in /tmp/screencast.mp4"
        pkill --signal SIGINT wf-recorder
        exit 0
    fi
}

remove_vid() {
    [ -f /tmp/screencast.mp4 ] && rm /tmp/screencast.mp4
}

kill_proc

OPT=$(printf "screencast\narea\nshare\nquit" | fuzzel --dmenu -p 'Select an option: ' )
case $OPT in
    'screencast')
        sleep 1
        remove_vid
        sleep 1
        screencast;;
    'area')
        sleep 1
        remove_vid
        sleep 1
        area;;
    'share')
        check_connection && share || notify-send "Error" "check your internet connection" ;;
    *|quit) exit 0;;
esac