1. Overview

In this tutorial, we’ll learn how to create a GIF from a screen recording in Linux. We’ll start by exploring how we can record our screen under X11 and Wayland. Then, we’ll convert the recorded video to GIF and optimize it for the web.

Finally, we’ll explore an alternative approach to quickly recording the screen as a GIF with Peek.

2. A Manual Approach

On Linux, the method for recording screens differs between X11 and Wayland due to their underlying architecture. On X11, we can use utilities like FFmpeg, SimpleScreenRecorder, and Kazam. These tools offer a bunch of awesome features.

However, on Wayland, screen recording is more tightly integrated with the Desktop Environment to provide enhanced security. Therefore, tools implemented for X11 don’t work directly in Wayland. Fortunately, there are tools like wf-recorder and the GNOME in-built recording tool that we can use to record screens easily.

While some tools can accomplish this task in a simpler way, the manual approach provides much more flexibility. Thus, they allow us to create GIFs more aligned with our requirements.

2.1. Screen Recording: X11

Most modern Desktop Environments provide an in-built solution for screen recording. For instance, we can fire up the GNOME Screenshot utility to record our screen:

Recording Screen on Ubuntu

But wait, there’s more! We’re not confined to just one tool. We can use FFmpeg, the ultimate Swiss Army knife for multimedia processing. With FFmpeg, we can screen record as well as convert the recorded video to GIF.

Not only that, but we can also use it for Desktop Environments and Window Managers that don’t include an in-built utility to record screens.

Let’s record our primary display using ffmpeg:

$ ffmpeg -f x11grab -y -framerate 20 -s 1920x1080 -i :0.0 -c:v libx264 \
  -preset superfast -crf 18 "$(date +'%Y-%m-%d_%H-%M-%S').mp4"

Let’s break this down:

  • -f specifies the input format, which is x11grab to allow screen capturing on X11
  • -y overwrites the output file without a prompt
  • -framerate indicates the framerate to use, which is 20
  • -s signifies the area in pixels that we want to capture
  • -i represents the input source as the display :0.0, which represents the primary display on X11
  • -c:v selects the audio codec to use for the output format, which is x264
  • -preset specifies the encoding preset, which is set to superfast to prioritize speed over compression
  • -crf sets the constant rate factor to determine the video quality and file size
  • $(date +’%Y-%m-%d_%H-%M-%S’).mp4 is the dynamic output filename for the video

We can change the resolution based on our requirements. In addition, a lower -crf value produces a high-quality video at the expense of file size. It can be between 051, where 0 means lossless.

In some cases, we aren’t sure about the screen resolution, but we want to capture the entire screen. For that specific use case, we can grab the screen resolution dynamically:

$ xdpyinfo | grep dimensions | awk '{ print $2 }'

We can inject this handy snippet in the command to substitute the value for -s:

$ ffmpeg \
   -f x11grab \
   -y \
   -framerate 20 \
   -s "$(xdpyinfo | grep dimensions | awk '{ print $2 }' )" \
   -i :0.0 \
   -c:v libx264 \
   -preset superfast \
   -crf 21 \
   "$(date +'%Y-%m-%d_%H-%M-%S').mp4"

Once our recording is complete, we can head over to the terminal and terminate the process using Ctrl+C. It sends the -SIGINT signal, and FFmpeg quits gracefully.

And that’s not all! Once we’re done recording, we can convert the video to GIF and optimize the resulting GIF.

2.2. Screen Recording: Wayland

Currently, there is no direct support in FFmpeg for capturing the screen on Wayland. However, like X11, some Desktop Environments using Wayland also contain utilities for recording screens. For instance, the GNOME Screenshot can record the screen under Wayland without any issues.

However, if we’re using something else like SwayWM or Hyprland, we can use wf-recorder, which is available on most official package repositories.

Once installed, we can simply run wf-recorder to record the entire current display:

$ wf-recorder

In addition, we can also specify the geometry using the -g option. However, we can make this much easier with slurp. The slurp utility lets us select the region and prints its geometry to the standard output:

$ wf-recorder -g "$(slurp)"

Like wf-recorder, slurp is also available in most official package repositories.

2.3. Converting Screencast to GIF

Once we have our recorded video, we can use FFmpeg to convert it to GIF:

$ ffmpeg -i Recording.mp4 \
    -vf "fps=10,scale=640:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" \
    -loop 0 Recording.gif

Here is a breakdown of -vf (video filters):

  • fps=10 specifies the framerate to use, which is usually set to 10 for GIFs
  • scale=640:-1 specifies the width and height of the resulting GIF in pixels, respectively — -1 being proportional to the width
  • flags specify additional parameters for the scale filter:
    • lanczos specifies the Lanczos resizing algorithm, which provides high-quality scaling
    • split[s0][s1] splits the video stream into two for generating and applying the color palette
    • [s0]palettegen[p] generates color palette from s0 and stores it in p
    • [s1][p]paletteuse applies the color palette from p to s1

The -loop option indicates the number of counts to loop the GIF. We’ve set it to 0 to loop the GIF indefinitely.

Once we run the command, it outputs a GIF file that’s good enough to use. Here’s a simple demo:

Demo GIF of a Screen Recording

2.4. Optimizing GIF

The resulting GIF that we created in the previous section is around 300 KB:

$ du -h Recording.gif | cut -f1

However, we can optimize it further by about 50% using ImageMagick‘s mogrify tool:

$ mogrify -optimize layers -fuzz 10% Recording.gif

Let’s break this down:

  • -optimize layers optimize the GIF image by merging identical layers to reduce its size
  • -fuzz 10% allows for a slight variation in colors during the process

Once the command has been processed, we can check the size of the resulting GIF:

$ du -h Recording.gif | cut -f1

Notably, the size shrunk down to 177 KB with no real loss of quality, which makes it more suitable for use on the web.

3. Alternative: Peek

If we want to quickly record our screen as GIF, we can opt out for a quicker solution. For this purpose, we can use Peek on both X11 and Wayland.

Peek is a screen recording tool for Linux. It has an intuitive UI for quickly visualizing screen captures. While it doesn’t give us much control, it’s suitable for scenarios where we don’t care about optimizations and custom image processing.

We can download Peek from its official website.

Once we fire up Peek, we’ll need to position the window according to our requirements:

Screen Recording with Peek

Once it’s set, we can press the “Record as GIF” button to start recording.

4. Conclusion

In this article, we explored how to record screens on both X11 and Wayland. Apart from that, we discussed how to convert the resulting video to GIF and optimize it further.

Finally, we looked at the Peek utility to quickly record our screens as a GIF.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.