Record screen with audio (loopback) using FFmpeg

Recording your screen with audio, including loopback audio, can be a valuable skill for creating tutorials, capturing gameplay, or archiving video conferences. This guide will show you how to use FFmpeg to record your screen along with loopback audio with ALSA as backend.


Load `snd-aloop` Module:

To enable loopback audio recording, you need to load the snd-aloop module with a single PCM substream. Use the following command (use sudo if you don’t have doas installed):

doas modprobe snd-aloop pcm_substreams=1 
Edit the ALSA Configuration File:

To direct audio output to the loopback device and record it, edit your ALSA configuration file. This step involves creating or appending to /etc/asound.conf. This will temporarily disable audio output. Execute the following command:

echo ’pcm.!default { type plug slave.pcm "hw:Loopback,0,0" }’ >> /etc/asound.conf 

If you wish to record audio from an application while also routing the audio to an output device, you can find alternative configurations on the FFmpeg Wiki.

Start Recordning:

Now, you can start the actual screen recording. In this example, we’ll record a 1280x1080 portion of the screen starting from coordinates 0,0 using the ALSA audio hardware ’Loopback,1,0’. You can adjust the resolution and other parameters as needed.

ffmpeg -video_size 1280x1080 -framerate 25 -f x11grab -i :0.0+0,0 -f alsa -ac 2 -ar 44100 -i hw:Loopback,1,0 output.mkv 

-video_size: Sets the video size to 1280x1080 pixels.

-framerate: Specifies the frame rate (25 frames per second in this example).

-f x11grab: Uses the x11grab input format for screen capture.

-i :0.0+0,0: Specifies the screen area to capture (0,0 starting from the top-left corner).

-f alsa: Sets the audio input format to ALSA.

-ac 2: Configures 2 audio channels.

-ar 44100: Sets the audio sample rate to 44.1 kHz.

-i hw:Loopback,1,0: Specifies the loopback audio input.

output.mkv: Defines the output file name (you can use any desired format).

  • To stop the recording, simply press Ctrl+C in the terminal where you started FFmpeg.
  • Once you’ve finished your recording, remove or comment out the line added to /etc/asound.conf in step 2. This will re-enable audio output on your system.

Now you can record your screen with loopback audio using FFmpeg. This can be especially useful for various content creation and screen capture needs.