Record X11 Window by ID with FFmpeg
Capturing a specific window instead of your entire desktop is a highly efficient way to record screencasts on Linux. This article explains how to identify a specific window’s X11 ID and configure the FFmpeg command-line tool to capture only that window’s contents.
Step 1: Find the Window ID
To capture a specific window, you first need its unique hexadecimal
window ID. The easiest way to find this is by using the
xwininfo utility, which is standard on most Linux
distributions running X11.
Open your terminal.
Run the following command:
xwininfoYour cursor will turn into a crosshair. Click on the target window you wish to record.
The terminal will output details about the selected window. Look for the line containing Window id:
xwininfo: Window id: 0x4a00012 "Terminal"In this example, the Window ID is
0x4a00012.
Step 2: Record the Window Using FFmpeg
Once you have the ID, pass it to FFmpeg using the
x11grab input device and the -window_id
option.
Run the following command in your terminal, replacing
0x4a00012 with your actual Window ID and :0.0
with your X11 display number (which is typically :0.0 or
:0):
ffmpeg -f x11grab -window_id 0x4a00012 -framerate 30 -i :0.0 -c:v libx264 -pix_fmt yuv420p output.mp4Explanation of the FFmpeg Command:
-f x11grab: Tells FFmpeg to use the X11 screen-grabbing input device.-window_id 0x4a00012: Instructs FFmpeg to capture only the window associated with this specific ID.-framerate 30: Sets the capture rate to 30 frames per second.-i :0.0: Specifies the target X11 display screen.-c:v libx264: Encodes the video using the H.264 codec.-pix_fmt yuv420p: Sets the pixel format to ensure wide compatibility with modern media players.output.mp4: The output video file name.
To stop the recording, press q in the terminal window
where FFmpeg is running.