Capture a Linux Window with FFmpeg and x11grab

Capturing a specific application window on Linux using FFmpeg and x11grab requires identifying the window's precise geometry and screen coordinates. By utilizing the xwininfo utility, you can extract the target window's dimensions and placement, then pass these parameters directly into an FFmpeg command to record only that specific application instead of your entire desktop. This guide will walk you through finding window coordinates and executing the exact FFmpeg command needed for a clean, isolated window capture.

Step 1: Find the Target Window Coordinates

Before running FFmpeg, you need to tell x11grab exactly where the window is positioned on your X11 display. The easiest way to find this is by using a utility called xwininfo.

  1. Open your terminal and run the following command:
xwininfo
  1. Your mouse cursor will turn into a crosshair. Click directly on the application window you want to record.
  2. The terminal will output several lines of data. Look for the following specific values:
  • Absolute top-left X: (The X-coordinate offset)
  • Absolute top-left Y: (The Y-coordinate offset)
  • Width: (The window width)
  • Height: (The window height)

For example, let's assume xwininfo gave you a width of 1024, a height of 768, an absolute X of 100, and an absolute Y of 200.

Step 2: Construct and Run the FFmpeg Command

Once you have the coordinates, you can plug them into FFmpeg using the -video_size and -i (input) arguments. The standard syntax for desktop grabbing follows this structure:

ffmpeg -f x11grab -video_size [Width]x[Height] -i :0.0+[X-offset],[Y-offset] output.mp4

Using the example coordinates gathered from xwininfo, the complete command to capture just that specific application window looks like this:

ffmpeg -f x11grab -video_size 1024x768 -i :0.0+100,200 -c:v libx264 -preset ultrafast crf.mp4

Understanding the Command Flags

  • -f x11grab: Forces FFmpeg to use the X11 screen-grabbing device.
  • -video_size 1024x768: Sets the resolution of the capture area to match your window's width and height.
  • -i :0.0+100,200: Specifies the X11 server display path (:0.0) followed by the X and Y offsets (+100,200) where the recording window starts.
  • -c:v libx264: Encodes the video using the H.264 codec for high compatibility.
  • -preset ultrafast: Lowers CPU usage during recording to prevent dropped frames, which is ideal for real-time screen capturing.

When you are finished recording, simply press q or Ctrl+C in the terminal to stop FFmpeg and save your video file.