1. 24
    1. 6

      This feels like something that ought to be a service provided by a compositing window manager, rather than externally. A compositing window manager knows where the windows are and has an X11 Picture associated with the backing store for each one. It should be able to stream those directly into a video. If it’s using OpenGL for compositing then it probably has the window’s Picture on the GPU as a texture already and so should be able to shove it into a hardware video CODEC without any additional bus round trips.

      1. 8

        And indeed GNOME Shell (and I think other Mutter-based compositors) can do exactly this, via PipeWire. It took 5 clicks (+ → Window Capture (Pipewire) → OK → [pick a window] → OK) to tell OBS Studio to capture my terminal window. I believe DMA-BUF sharing is used to avoid copies.

        Not sure if there is better documentation around than the documentation for the ScreenCast portal.

        1. 5

          OP uses OpenBSD so I don’t think pipewire or OBS would be an option for him at the moment.

          (I know there is a porting effort for OBS underway though)

        2. 1

          I’ve been using Kazam for years for recording the entire screen, individual windows, or sections of the screen. It works wonderfully, similar to what you are describing. It’s available in the upstream repos, though not sure if it’s available on OpenBSD.

      2. 1

        Ordinary X has all the capabilities needed to do it, just ffmpeg only allows capturing from the root window with a bounding box, hence the xwininfo extra step. There’s actually no need for any of this if you’re writing your own program (notice that things like browser screen share can pick individual windows without requiring a compositor at all), just if you are trying to make it work with only the ffmpeg command line options.

    2. 4

      I was going to mention SimpleScreenRecorder for Linux (I often use the option to simply pick a window) but that’s already pointed out in the footnotes :)

    3. 3

      I have a script using slop and ffmpeg, https://raymii.org/s/snippets/Record_your_Linux_Desktop_with_ffmpeg_and_slop.html, but it creates huge files. This articles approach looks interesting because I’ve now mostly switched to Peek, which producesway smaller ffiles and can be resized to a specific window. https://github.com/phw/peek

      1. 2

        peek is very convenient, and it’s even in most distro’s repositories, I believe. Certainly on Fedora.

        I like to output mp4s with it, even if it describes itself (in the repository) as:

        peek.x86_64 : Animated GIF screen recorder with an easy to use interface

    4. 3

      hk is pretty interesting per se ! And your knowledge of ffmpeg format conversion and post-processing is really valuable, and I’ll steal a good bit of it 😉

      I’ve been using a combination of wmutils and xrectsel to do something similar. I extend upon your solution by having the ability to record a randomly selected region of the screen, or arbitrary coordinates. I’m using different tools for that, which are more specific than simple xwindow output parsing (which is far from ideal IMO).

      • xrectsel lets you draw a region on the screen, and reports coordinates
      • slw / pfw (from wmutils) both report an X window ID (either selected by clicking it, or using the focused one)
      • wattr reports various window attributes (xywhb gives X, Y, Width, Height, Border width)
      • randr is a hacky tool I wrote to report the monitor size where the mouse cursor is (useful on multi-monitor setups)

      Here’s a showcase : ffmpeg-coordinate.webm

      1. 1

        Lovely and simple, as always. I’d love to steal that snip screenshot script off you ;)

        1. 3
          #!/bin/sh
          # require imagemagick, xrectsel
          png=$(mktemp -p /tmp x11-snip.XXXXXXXX.png)
          import -window root $png
          convert $png -crop $(xrectsel '%wx%h+%x+%y') $png
          printf '%s' "$png" | xsel # Optional, put image path in clipboard for convenience
          display $png
          
          1. 1

            Thanks! I tried searching for your rec script too but can’t find where you keep such things. Can you please share a link? I’m quite interested to see your script and learn how you do it vs the script in the article. Cheers

            1. 1

              I don’t share them online, that’s why you couldn’t find anything. The rec script is basically them same as the above, but using ffmpeg rather than convert. And OP’s ffmpeg and are much better in terms of quality than what I came up with.

    5. 2

      OBS?

      1. 2

        Yes, this is trivial on X11 with OBS.

        1. 2

          OP runs an OS with incomplete support for OBS.

      2. 1