7

Is it possible to record the screen with ffmpeg on wayland using the Hyprland compositor?

I've read some posts that imply people managed to do it with kmsgrab, but cannot figure out how to do it myself (I tried the common examples with kmsgrab and it didn't work, possibly because I'm on wayland)

Is there a way to do it?

I've tried:

ffmpeg -device /dev/dri/card1 -f kmsgrab -i - -vf 'format=nv12,hwupload' -c:v h264_vaapi output.mkv

But get the following error:

[kmsgrab @ 0x556c9ae1fec0] Using plane 58 to locate framebuffers.
[kmsgrab @ 0x556c9ae1fec0] Template framebuffer is 142: 2560x1600 format 34325258 modifier 200000000401b03 flags 2.
[kmsgrab @ 0x556c9ae1fec0] No handle set on framebuffer: maybe you need some additional capabilities?
[in#0 @ 0x556c9ae1fc00] Error opening input: Invalid argument
Error opening input file -.
Error opening input files: Invalid argument

Recording with vaapi with wf-recorder or wl-screenrec works fine btw, but I would prefer to directly use ffmpeg.

I'm also curious if it can be used with nvenc (which would be on card0 for me), but if I try to use it with my nvidia gpu I get:

[kmsgrab @ 0x558916595e80] No usable planes found.
[in#0 @ 0x558916595bc0] Error opening input: Invalid argument
Error opening input file -.
Error opening input files: Invalid argument
2
  • which wayland composer are you using? (this makes a difference in terms of implemented screen capture protocols!) If in doubt, tell us what your desktop environment is (GNOME? KDE? …?) Commented Jul 28 at 16:03
  • and: kmsgrap is generally the device you would want to use on wayland if you want to do a low-level screen capture. You should edit your question to show what exactly you've tried and what output you've got from that! Commented Jul 28 at 16:09

1 Answer 1

9

Using kmsgrab requires cap_sys, that was my main issue.

sudo setcap cap_sys_admin+ep /usr/bin/ffmpeg

After running the above command I could use kmsgrab with vaapi.

ffmpeg -y -device /dev/dri/card1 -f kmsgrab -i - -vf 'hwmap=derive_device=vaapi,scale_vaapi=format=nv12' -c:v h264_vaapi -qp 24 output.mkv

Learned from the ffmpeg vaapi guide: https://trac.ffmpeg.org/wiki/Hardware/VAAPI

However, there is a problem, i can start recording but I can't stop recording, pressing ctrl+c won't stop recording unless you press it 3 times for immediate termination which breaks the output file.

The easiest solution if you wish to use an mp4 container is to apply fragmentation flags:

-movflags faststart+frag_keyframe

Another solution is to use an mkv container, since by default they don't break if you abruptly stop recording, if you want to use mp4, you can just convert it to mp4 after you stop recording, e.g.

ffmpeg -i input.mkv -c copy output.mp4

As for nvenc, although kmsgrab should be able to use it, I have not been able to figure out a way that works on my machine, I feel like without a way to make it work with nvenc this answer is incomplete, which is my main reason for not accepting it, hopefully someone knows how to make it work on nvidia as well.

2
  • 1
    Try exiting ffmpeg by pressing q. It will appear to hang briefly as it creates the index, that is normal and good. Commented Jul 29 at 2:18
  • @SimonRichter doesn't do anything, it works for other commands but not for kmsgrab. It's like ffmpeg isn't receiving the command. Commented Jul 29 at 5:10

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.