How to Capture Linux Desktop Screen with FFmpeg and X11
This article provides a straightforward guide on how to record your
Linux desktop screen using FFmpeg and the X11 screen grabber
(x11grab). You will learn the essential commands to capture
your entire screen, record specific regions, include audio, and optimize
recording performance.
Prerequisites
To record your screen, you must be running an X11 (Xorg) session and
have FFmpeg installed. You can verify your FFmpeg installation and check
if x11grab is supported by running:
ffmpeg -decoders | grep x11Basic Screen Capture
To capture your entire desktop at a specific resolution and frame rate, use the following basic command:
ffmpeg -f x11grab -video_size 1920x1080 -framerate 30 -i :0.0 output.mp4Parameter Breakdown: * -f x11grab:
Forces FFmpeg to use the X11 screen-grabbing input device. *
-video_size 1920x1080: Sets the resolution of the capture
area. Replace this with your actual screen resolution. *
-framerate 30: Sets the recording speed to 30 frames per
second. * -i :0.0: Specifies the target X11 display
(usually :0.0 for the primary monitor). *
output.mp4: The name of the resulting video file.
To stop the recording at any time, press q in your
terminal window.
Capturing a Specific Screen Region
If you only want to record a specific portion of your screen, you can
define an offset coordinate after the display number using the format
:0.0+X,Y.
For example, to capture an 800x600 pixel window starting 100 pixels from the left and 200 pixels from the top of your screen, run:
ffmpeg -f x11grab -video_size 800x600 -framerate 30 -i :0.0+100,200 output.mp4Capturing Screen with Audio
To record desktop audio or microphone input alongside the video, you can map an audio device using PulseAudio or ALSA.
Use this command to capture both the screen and system audio simultaneously:
ffmpeg -f x11grab -video_size 1920x1080 -framerate 30 -i :0.0 -f pulse -i default output.mp4Additional Parameters: * -f pulse:
Specifies PulseAudio as the audio input format. *
-i default: Selects the default system audio source.
Optimizing Performance
Screen recording can be CPU-intensive. You can reduce processor load
during recording by using the ultrafast compression preset,
which prioritizes speed over compression efficiency:
ffmpeg -f x11grab -video_size 1920x1080 -framerate 30 -i :0.0 -c:v libx264 -preset ultrafast crf 18 output.mp4-c:v libx264: Sets the video codec to H.264.-preset ultrafast: Reduces CPU usage to prevent dropped frames.-crf 18: Maintains high visual quality (lower numbers yield better quality).