GIF/WebP Screen Capture on Wayland(Hyprland)

Andrew O

Andrew O

3 min read Tags: Linux

Table of Contents


Introduction

Do you need a simple way to create a screen capture on Wayland, like this one below?

demo

Screen capture demo - click to enlarge

In this tutorial, I will show a really easy setup with wf-recorder running on hyprland, and provide the script to automate the process.

This can be pretty handy to create a screen capture for a part of the screen or the whole screen with just one hotkey, and share it in a WebP format. By default, I will use WebP, but you can change it to gif or any other format supported by ffmpeg.

Prerequisites

You will need to install the following packages:

Use your package manager to install them. For example, on Arch:

sudo pacman -S wf-recorder ffmpeg libnotify

libnotify is used to send a notification when the recording is started/finished.

Script

Here’s a script that can start and stop the recording:

#!/bin/bash
# screen-capture.sh

DIR="screen-capture"

# Check whether to start or stop the recording (if wf-recorder is running)
if [ -f /tmp/wf-recorder.pid ] && [ -f /tmp/wf-recorder.timestamp ]; then
    # Read the PID and timestamp of wf-recorder
    pid=$(cat /tmp/wf-recorder.pid)
    timestamp=$(cat /tmp/wf-recorder.timestamp)
    kill $pid
    rm /tmp/wf-recorder.pid /tmp/wf-recorder.timestamp

    # Send a notification
    notify-send "Recording Finished" \
                "Converting..."

    # Convert the recording to webp
    output_path="$DIR/recording_${timestamp}.webp"
    ffmpeg -i "$DIR/recording_${timestamp}.mkv" \
           -vf "fps=20,scale=-1:-1:flags=lanczos" \
           -c:v libwebp -q:v 30 -loop 0 \
           -preset default -an -vsync 0 -slices 4 "$output_path"

    # Remove mkv file
    rm "$DIR/recording_${timestamp}.mkv"

    # Send a notification
    notify-send "Converting Finished" \
                "Saved to $output_path"
else
    # Generate a timestamp
    timestamp=$(date +"%Y%m%d_%H%M%S")

    # Create output dir if not exists
    mkdir -p $DIR

    # Start recording with wf-recorder and save to a file with the timestamp
    wf-recorder -g "$(slurp)" -f "$DIR/recording_${timestamp}.mkv" &

    # Save the PID of wf-recorder and the timestamp
    echo $! > /tmp/wf-recorder.pid
    echo $timestamp > /tmp/wf-recorder.timestamp

    # Send a notification
    notify-send "Recording Started" \
                "Saved to $DIR/recording_${timestamp}.mkv"
fi

Save it to a directory where you keep your keyboard shortcuts scripts, for example ~/scripts/screen-capture.sh, and make it executable:

chmod +x ~/scripts/screen-capture.sh

Inside the script, you can specify the output folder by changing the DIR variable. By default, it will save the recordings to the screen-capture directory. The output path will also be shown with a notification.

This script will start the recording with wf-recorder and save it to a file with a timestamp in the screen-capture directory. Once the recording is started, the PID and timestamp of wf-recorder will be saved to /tmp/wf-recorder.pid and /tmp/wf-recorder.timestamp respectively. This way, when the script is called again, it will find those files and stop the recording, convert the recording to WebP, and remove the mkv file.

Other parameters of ffmpeg can be changed to suit your needs. Here are some of the parameters that you might want to tweak:

If you are uploading the recording to the web, you might want to reduce the quality to save bandwidth - manipulating the above parameters can help you achieve that.

Hotkey

It is very useful to bind this script to a hotkey, so you can start and stop the recording with just one keystroke. You can do this with the your tool of choice, I will provide an example with Hyprland.

Navigate to the keybindings file, for example

~/.config/hypr/conf/keybindings/default.conf

and add the following lines:

bind = $mainMod SHIFT, S, exec, $HYPRSCRIPTS/screen-capture.sh # Start/stop a screen capture to webp

Conclusion

With this setup, you can easily create a screen capture on Wayland with just one hotkey. The script will start and stop the recording, convert it to WebP, and send a notification when the recording is finished and converted. You can easily modify the script to change the output format or other settings.

Have a good day,

Andrew

Recent Posts

/../assets/images/posts/gdb1.png
GDB Quick Reference for Most Used Commands

A quick reference for the most commonly used GDB commands for debugging C/C++ programs.

Tags: Linux
Andrew O

Andrew O

3 min read
/../assets/images/posts/tgu1.png
Heavy Kettlebell Turkish Getups, part 1

Benefits, drawbacks, forearm pain

Andrew O

Andrew O

6 min read
/../assets/images/posts/pw1.png
Remote plant watering with Raspberry Pi

Remote plant watering with Raspberry Pi, 5$ water pump, and Tor network for remote access

Andrew O

Andrew O

4 min read

Leave a comment

Need help? Get 1:1 live session on a trusted platform

Contact me on Codementor to schedule a 1:1 live session to help with your project or to learn something new.