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.

  1. Open your terminal.

  2. Run the following command:

    xwininfo
  3. Your cursor will turn into a crosshair. Click on the target window you wish to record.

  4. 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.mp4

Explanation of the FFmpeg Command:

To stop the recording, press q in the terminal window where FFmpeg is running.