How to Use FFmpeg ALSA on Linux

This guide provides a straightforward tutorial on how to use the -f alsa option in FFmpeg to record audio on Linux. It covers finding your ALSA audio devices, configuring recording settings like channels and sample rates, and executing commands to capture audio directly from your microphone or system soundcard.

Step 1: Identify Your ALSA Audio Devices

Before using FFmpeg to capture audio, you must identify the name of the ALSA device you want to record from. You can list all available capture devices using the arecord command:

arecord -l

Or for a more detailed list of device names:

arecord -L

Look for entries like hw:0,0 (card 0, device 0) or default. If your system uses PulseAudio or PipeWire alongside ALSA, default or pulse will usually route to your current active input device.

Step 2: Basic Audio Recording Command

The fundamental syntax for recording audio using the ALSA demuxer in FFmpeg is:

ffmpeg -f alsa -i <device_name> output.wav

To record from your default system microphone, run:

ffmpeg -f alsa -i default output.wav

To record from a specific hardware card and device (e.g., card 1, device 0):

ffmpeg -f alsa -i hw:1,0 output.wav

Step 3: Configure Channels, Sample Rate, and Codec

For better control over your audio quality, you can specify the number of audio channels, the sample rate, and the audio encoder.

Record Stereo Audio at 44.1kHz to MP3

ffmpeg -f alsa -ac 2 -ar 44100 -i default -c:a libmp3lame output.mp3

Record Lossless FLAC Audio

ffmpeg -f alsa -ac 2 -ar 48000 -i hw:0 -c:a flac output.flac

Step 4: Record Desktop Video with ALSA Audio

You can combine the -f alsa option with a video grabber like x11grab to record your Linux desktop screen and microphone audio simultaneously:

ffmpeg -f x11grab -video_size 1920x1080 -framerate 30 -i :0.0 -f alsa -ac 2 -i default -c:v libx264 -c:a aac output.mp4

In this command: * -f x11grab -video_size 1920x1080 -framerate 30 -i :0.0 captures the desktop screen. * -f alsa -ac 2 -i default captures the stereo audio. * -c:v libx264 -c:a aac encodes the video as H.264 and the audio as AAC into an MP4 container.