How to Record ALSA Audio with FFmpeg on Linux
Recording audio from the Advanced Linux Sound Architecture (ALSA) using FFmpeg is an efficient way to capture microphone input or system sound directly from the command line. This guide provides a straightforward walkthrough on how to identify your ALSA input devices, construct the correct FFmpeg command, and customize your recording settings for optimal audio quality.
Step 1: Identify Your ALSA Input Device
Before recording, you need to find the designation of your audio hardware. Open your terminal and run the following command to list all capture hardware devices:
arecord -lThe output will list your soundcards and digital audio devices. Look
for the card number and device number. For example, if you see
card 0: PCH [HDA Intel PCH], device 0: ALC3232 Analog [ALC3232 Analog],
your device identifier is hw:0,0.
Alternatively, you can use default to use the system’s
default ALSA recording device.
Step 2: Run the Basic FFmpeg Recording Command
To capture audio using the default ALSA device and save it as a WAV file, use the following command:
ffmpeg -f alsa -i default output.wavIf you want to target a specific hardware device identified in Step 1
(such as card 0, device 0), replace default with
hw:0,0:
ffmpeg -f alsa -i hw:0,0 output.wavPress q in the terminal window to stop recording at any
time.
Step 3: Customize Channels, Sample Rate, and Codec
To control the quality and format of your recording, you can pass additional parameters to FFmpeg.
-ac: Sets the number of audio channels (e.g.,1for mono,2for stereo).-ar: Sets the audio sample rate in Hz (e.g.,44100or48000).-c:a: Sets the audio codec (e.g.,libmp3lamefor MP3,libopusfor Opus, or none for uncompressed WAV).
Here is an example of recording high-quality stereo MP3 audio at 44.1 kHz:
ffmpeg -f alsa -ac 2 -ar 44100 -i hw:0,0 -c:a libmp3lame -b:a 192k output.mp3Troubleshooting Common Issues
- Device or Resource Busy: If you receive this error, another application is likely using the audio device exclusively. Close other media players, browsers, or audio software, or route your recording through PulseAudio/PipeWire if they are running on your system.
- Low Volume: Adjust your microphone or capture
levels using the ALSA mixer interface by running
alsamixerin your terminal. Use the arrow keys to navigate and increase the capture volume.