Recording audio with gstreamer is easy:
gst-launch -e pulsesrc ! audioconvert ! \
lamemp3enc target=1 bitrate=64 cbr=true ! \
filesink location=audio.mp3
The pulsesrc element here refers to the pulseaudio input (pulseaudio is AFAIK the default sound system in all linux distributions nowadays). This is good and will capture audio from the default sound input, which can be microphone or line-in depending on the hardware and the pulseaudio configuration.
But what if I plug in my USB webcam with built-in microphone and I want to capture audio from that?
The answer is to specify the input device name using the “device” property of the pulsesrc element. The list of audio devices and their names can be obtained using the command:
pactl list
This will generate a lot of text and you will probably want to pipe this through less. The interesting modules are those of type “Source #” for input and “Sink #” for playback. The pulseaudio device name is then the weird text string shown under the “Name” parameter. The Pulseaudio FAQ has some neat tricks for filtering the device names out:
pactl list | grep -A2 'Source #' | grep 'Name: ' | cut -d" " -f2
On my computer this will generate the following list:
alsa_output.pci-0000_80_01.0.analog-stereo.monitor
alsa_input.pci-0000_80_01.0.analog-stereo
Note the the audio output also appears as a source. In pulseaudio every sink automatically has a monitor source – practically this means that we can also capture the output of audio applications. In the above list the monitor source refers the the master output, i.e. no particular application.
Now, if I plug in my webcam the built-in microphone will also appear as an input device:
alsa_output.pci-0000_80_01.0.analog-stereo.monitor
alsa_input.pci-0000_80_01.0.analog-stereo
alsa_input.usb-046d_0809_52A63768-02.analog-mono
It’s the last line with the USB-thing. I can now record audio from the webcam using:
gst-launch -e pulsesrc device="alsa_input.usb-046d_0809_52A63768-02.analog-mono" ! audioconvert ! \
lamemp3enc target=1 bitrate=128 cbr=true ! filesink location=audio.mp3
That was easy.
According to the pulsesrc documentation, one can also use the “device-name” property, which should be a human readable name but I have no idea what this name is.
A final note about recording the output from other applications. To get a list of all monitor type sources we can use:
pactl list | grep -A2 'Source #' | grep 'Name: .*\.monitor$' | cut -d" " -f2
This was also stolen from the Pulseaudio FAQ.