wiki:

Capture

/

ALSA


Version 3 (modified by burek, 5 years ago) (diff)

--

Syntax

Capturing audio with FFmpeg and ALSA is pretty much straightforward. Usual way to do this is like this:

ffmpeg -f alsa <input_options> -i <input_device> ... output.wav

Selecting the input card

input_device tells FFmpeg which audio capturing card/device you would like to use. To get the list of all installed cards on your machine, you can type arecord -l (small L) or arecord -L (longer output).

So, first, list your recording cards/devices:

# arecord -l

**** List of CAPTURE Hardware Devices ****
card 0: ICH5 [Intel ICH5], device 0: Intel ICH [Intel ICH5]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: ICH5 [Intel ICH5], device 1: Intel ICH - MIC ADC [Intel ICH5 - MIC ADC]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: ICH5 [Intel ICH5], device 2: Intel ICH - MIC2 ADC [Intel ICH5 - MIC2 ADC]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: ICH5 [Intel ICH5], device 3: Intel ICH - ADC2 [Intel ICH5 - ADC2]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: U0x46d0x809 [USB Device 0x46d:0x809], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

We can see there are 2 audio cards installed that provide capturing capabilities, namely "card 0" (Intel ICH5) and "card 1" (Microphone on the USB web cam). The easiest thing to do is to reference each of them directly using "-f alsa -i hw:0" or "-f alsa -i hw:1":

ffmpeg -f alsa -i hw:1 -t 30 out.wav

That will give us a 30 seconds WAV audio output, recorded from our USB camera's default recording device (microphone). The default recording device can be selected using "alsamixer" tool (see below) or specifying the device using an additional parameter Y in hw:<X>,<Y>, where <X>=card, <Y>=device. For example, to select "MIC2 ADC" from Intel card (look above at the list), we would use:

ffmpeg -f alsa -i hw:0,2 -t 30 out.wav

The best way is to select your card and default recording device with "alsamixer" tool, because some audio cards have a complicated way of selecting the default input through the FFmpeg's command line.

Surviving the reboot

If you reboot your machine, you will notice sometimes your cards get reordered, so "card 0" is listed as USB Audio and "card 1" is listed as Intel audio card. You might want to play with udev rules, but there is an easier solution for this. Typing arecord -L will give us a little bit more detailed listing of recording devices:

# arecord -L
null
    Discard all samples (playback) or generate zero samples (capture)
default:CARD=ICH5
    Intel ICH5, Intel ICH5
    Default Audio Device
sysdefault:CARD=ICH5
    Intel ICH5, Intel ICH5
    Default Audio Device
front:CARD=ICH5,DEV=0
    Intel ICH5, Intel ICH5
    Front speakers
surround40:CARD=ICH5,DEV=0
    Intel ICH5, Intel ICH5
    4.0 Surround output to Front and Rear speakers
surround41:CARD=ICH5,DEV=0
    Intel ICH5, Intel ICH5
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=ICH5,DEV=0
    Intel ICH5, Intel ICH5
    5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=ICH5,DEV=0
    Intel ICH5, Intel ICH5
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
default:CARD=U0x46d0x809
    USB Device 0x46d:0x809, USB Audio
    Default Audio Device
sysdefault:CARD=U0x46d0x809
    USB Device 0x46d:0x809, USB Audio
    Default Audio Device
front:CARD=U0x46d0x809,DEV=0
    USB Device 0x46d:0x809, USB Audio
    Front speakers
surround40:CARD=U0x46d0x809,DEV=0
    USB Device 0x46d:0x809, USB Audio
    4.0 Surround output to Front and Rear speakers
surround41:CARD=U0x46d0x809,DEV=0
    USB Device 0x46d:0x809, USB Audio
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=U0x46d0x809,DEV=0
    USB Device 0x46d:0x809, USB Audio
    5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=U0x46d0x809,DEV=0
    USB Device 0x46d:0x809, USB Audio
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=U0x46d0x809,DEV=0
    USB Device 0x46d:0x809, USB Audio
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
iec958:CARD=U0x46d0x809,DEV=0
    USB Device 0x46d:0x809, USB Audio
    IEC958 (S/PDIF) Digital Audio Output

We can tell FFmpeg exactly what card we want to use, specifying the exact card's name, no matter which ordering it is, like this:

ffmpeg -f alsa -i default:CARD=U0x46d0x809 -t 30 out.wav

This way, you're always asking for the input from that certain device (the default recording device from the USB Audio device) and will never mix things up.

ALSA mixer tool

You might find useful a tool named alsamixer.

It will let you visually select, for each specified card (intel or usb), which recording device do you want to use (if the specified card has got multiple inputs, like Line-In, CD-In, Mic, ...), so you can just run "alsamixer", press F6 to choose the card and then use TAB key to switch to recording devices (pressing it multiple times just switches between playback/recording/all devices), after that just use arrow keys to highlight desired device and just hit the SPACEBAR key to select it (and up/down, page up/dn keys to change the input volume).

Input options

The only useful audio input options for ALSA input are "-ar" (audio sample rate) and "-ac" (audio channels).

Specifying audio sampling rate/frequency will force the audio card to record the audio at that specified rate. Usually the default value is "44100" (Hz). Specifying audio channels will force the audio card to record the audio as mono, stereo or even 2.1/5.1 (if supported by your audio card). Usually the default value is "1" (mono) for Mic input and "2" (stereo) for Line-In input.

Examples

Grab an audio from your microphone

When doing screencast recordings, you usually want to record your voice too, so you would probably want to do something like this:

ffmpeg -f alsa -ac 1 -ar 44100 -i hw:0 -t 30 out.wav

Looking at our example device listing, this would be the same as this:

ffmpeg -f alsa -ac 1 -ar 44100 -i default:CARD=ICH5 -t 30 out.wav

Attachments (1)

Download all attachments as: .zip