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 -lOr for a more detailed list of device names:
arecord -LLook 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.wavTo record from your default system microphone, run:
ffmpeg -f alsa -i default output.wavTo record from a specific hardware card and device (e.g., card 1, device 0):
ffmpeg -f alsa -i hw:1,0 output.wavStep 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.
-ac: Sets the number of audio channels (e.g.,1for mono,2for stereo).-ar: Sets the audio sampling rate in Hz (e.g.,44100or48000).-c:a: Sets the audio codec (e.g.,libmp3lamefor MP3,flacfor lossless compression).
Record Stereo Audio at 44.1kHz to MP3
ffmpeg -f alsa -ac 2 -ar 44100 -i default -c:a libmp3lame output.mp3Record Lossless FLAC Audio
ffmpeg -f alsa -ac 2 -ar 48000 -i hw:0 -c:a flac output.flacStep 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.mp4In 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.