To quickly record the whole primary display (only works for X11 and not for Wayland)

ffmpeg \
   -f x11grab \
   -y \
   -framerate 20 \
   -s "$(xdpyinfo | grep dimensions | awk '{ print $2 }' )" \
   -i :0.0 \
   -c:v libx264 \
   -preset superfast \
   -crf 21 \
   "$(date +'%Y-%m-%d_%H-%M-%S').mp4"

Let’s break this down:

  • -f specifies the input format, which is x11grab to allow screen capturing on X11
  • -y overwrites the output file without a prompt
  • -framerate indicates the framerate to use, which is 20
  • -s signifies the area in pixels that we want to capture
  • -i represents the input source as the display :0.0, which represents the primary display on X11
  • -c:v selects the audio codec to use for the output format, which is x264
  • -preset specifies the encoding preset, which is set to superfast to prioritize speed over compression
  • -crf sets the constant rate factor to determine the video quality and file size
  • $(date +’%Y-%m-%d_%H-%M-%S’).mp4 is the dynamic output filename for the video

If you record a video this way, it may not play in Firefox because it is recorded with h264 (in yuv444?). At least, it did not work when I uploaded such a screen recording to a comment in Github. It erroneously showed the error message, “Video can’t be played because the file is corrupt.” To fix this, convert the file once again

ffmpeg -i in.mp4 -pix_fmt yuv420p -c:a copy -movflags +faststart out.mp4