Record ALSA Audio with FFmpeg on Raspberry Pi

This guide explains how to capture audio from an ALSA (Advanced Linux Sound Architecture) input device on a Raspberry Pi using FFmpeg. You will learn how to identify your connected audio hardware, construct the proper FFmpeg command using ALSA input flags, and configure parameters such as sample rate, channel count, and output formats.

1. Identify Your ALSA Audio Device

Before capturing audio, you must find the card and device number assigned to your microphone or sound card by ALSA. Connect your audio interface to the Raspberry Pi and run:

arecord -l

Look for your device in the output list. It will appear similar to this:

card 1: Device [USB Audio Device], device 0: USB Audio [USB Audio]

Note the card and device numbers. In this example, the card is 1 and the device is 0. ALSA references this hardware endpoint as hw:1,0 or plughw:1,0. Using plughw is recommended because it automatically handles sample rate and format conversions if the hardware does not natively support your requested settings.

2. Basic Audio Recording Command

To capture raw audio using default settings to a .wav file, specify the -f alsa format flag and your device string with the -i flag:

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

Press Ctrl + C to stop recording.

3. Configure Channels, Sample Rate, and Codecs

Most recording scenarios require specific audio properties such as mono/stereo channels, standard sample rates, or compressed formats like MP3.

  • Channels (-ac): Set to 1 for mono or 2 for stereo.
  • Sample Rate (-ar): Common values are 44100 (CD quality) or 48000 (standard digital audio).
  • Audio Codec (-c:a): Specify an encoder such as libmp3lame or aac.

To record a 44.1 kHz mono stream encoded directly to an MP3 file, use:

ffmpeg -f alsa -ac 1 -ar 44100 -i plughw:1,0 -c:a libmp3lame -b:a 128k output.mp3

4. Limit Recording Duration

To automatically stop recording after a designated amount of time instead of stopping it manually, include the -t parameter followed by the duration in seconds (or HH:MM:SS format).

To record for 30 seconds:

ffmpeg -f alsa -i plughw:1,0 -t 30 output.wav

5. Adjusting Input Volume

If the recording is too quiet or clipping, adjust the hardware gain using the ALSA mixer utility:

alsamixer

Press F6 to select your sound card, use F4 to navigate to the capture controls, and adjust the input levels with the arrow keys before running your FFmpeg command.