How to Capture Audio from ALSA with FFmpeg

This article provides a straightforward guide on how to capture and record audio from an Advanced Linux Sound Architecture (ALSA) device using FFmpeg. You will learn how to identify your ALSA input devices, construct the correct FFmpeg command for recording, and configure essential settings such as channels, sample rates, and audio formats.

Step 1: Identify Your ALSA Recording Device

Before you can record audio, you need to find the name of your ALSA input device. You can list all available capture devices by running the following command in your terminal:

arecord -l

Or, for a more detailed list of device aliases:

arecord -L

Look for entries starting with hw: (such as hw:0,0 or hw:1,0). The first number represents the card number, and the second represents the device number. You can also use default if you want to capture from your system’s default input device.

Step 2: Write the Basic FFmpeg Capture Command

Once you have identified your device, use the -f alsa option to specify the input format, followed by the device name with the -i option.

Here is the most basic command to record audio from your default device and save it as a WAV file:

ffmpeg -f alsa -i default output.wav

To record from a specific hardware device (for example, card 0, device 0), use:

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

Step 3: Configure Channels and Sample Rates

For better control over your audio quality, you should specify the number of audio channels and the sample rate. Place these parameters before the input device (-i) so FFmpeg knows how to interpret the incoming stream.

To record in stereo at a CD-quality sample rate (44.1 kHz), use the following command:

ffmpeg -f alsa -ac 2 -ar 44100 -i hw:0,0 output.wav

Step 4: Choose Your Output Format and Codec

FFmpeg can encode the captured audio into various formats on the fly. Simply change the output file extension, and FFmpeg will select the appropriate codec.

To record to MP3 (compressed):

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

To record to FLAC (lossless compression):

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

Step 5: Stop the Recording

Once the command is running, FFmpeg will continuously capture audio from the ALSA device. To stop the recording and safely save the file, press Ctrl + C in your terminal.