Like it!

Join us on Facebook!

Like it!

How to record the desktop with FFmpeg on Linux

A solution that pleasantly works out of the box.

Record a video of your desktop — and whatever happens on it — is almost dead simple with FFmpeg. I'm using one of the latest versions (as of this writing), 4.1.6 on a Linux box (Debian). I will also be using PulseAudio as the audio framework and x11grab for capturing the video part.

The generic command:

ffmpeg -video_size [resolution] -framerate [framerate] -f x11grab -i :0.0 -f pulse -ac 2 -i [source] output.mkv

As you may see you need to know the resolution, the video frame rate and the audio source.

Getting the proper video resolution

Let's start with the resolution. Type xrandr to print a list of supported video modes. For example:

Screen 0: minimum 8 x 8, current 1366 x 768, maximum 32767 x 32767
LVDS1 connected 1366x768+0+0 (normal left inverted right x axis y axis) 344mm x 193mm
   1366x768      60.06*+
   1360x768      59.80    59.96  
   1024x768      60.00  
   800x600       60.32    56.25  
   640x480       59.94  
DP1 disconnected (normal left inverted right x axis y axis)
HDMI1 disconnected (normal left inverted right x axis y axis)
VGA1 disconnected (normal left inverted right x axis y axis)
VIRTUAL1 disconnected (normal left inverted right x axis y axis)

The first mode in the list is the highest, let's go with it.

Getting the proper PulseAudio source

The command pactl list sources will print a list of supported PulseAudio sources (i.e. inputs). In my case I get the following:

Source #0
    State: SUSPENDED
    Name: alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
    Description: Monitor of Built-in Audio Analog Stereo
  ...
  [many other infos here]
  ...

The source 0 is the only one available in my system. That's good. You should always record audio from the Monitor source, which basically contains what you are hearing over your speakers.

Packing the values together

The complete command, in my case, looks something like this:

ffmpeg -video_size 1366x768 -framerate 25 -f x11grab -i :0.0 -f pulse -ac 2 -i 0 output.mkv

A frame rate of 25 fps is a common choice that works best in most cases.

comments
j0n4t on March 30, 2022 at 13:26
Thanks for this post! It was really helpful!