Recording PulseAudio output with ffmpeg

Recently I was in a position whereby I wanted to record some audio coming from my ArchLinux system. I’d looked into this a long time ago and remember having to setup complicated PulseAudio sinks and separate outputs to achieve a simple recording. I was pleasantly surprised at just how easy it was now using ffmpeg, the Swiss army knife of multimedia manipulation.

First you have to identify which source (audio output) you would like to record from. There can be many sources on your machine, you can list them with the following command:

pacmd list-sources

In my case I only have the one:

1 source(s) available.
  * index: 5
        name: <alsa_output.pci-0000_00_1f.3.analog-stereo.monitor>
        driver: <module-alsa-card.c>
        flags: DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
        state: IDLE
        suspend cause: (none)
        priority: 1030
        volume: front-left: 65536 / 100% / 0.00 dB,   front-right: 65536 / 100% / 0.00 dB
                balance 0.00
        base volume: 65536 / 100% / 0.00 dB
        volume steps: 65537
        muted: no
        current latency: 0.00 ms
        max rewind: 14 KiB
        sample spec: s16le 2ch 48000Hz
        channel map: front-left,front-right
                     Stereo
        used by: 0
        linked by: 0
        configured latency: 1837.50 ms; range is 0.50 .. 1837.50 ms
        monitor_of: 5
        card: 0 <alsa_card.pci-0000_00_1f.3>
        module: 6
        properties:
                device.description = "Monitor of Built-in Audio Analog Stereo"
                device.class = "monitor"
                alsa.card = "0"
                alsa.card_name = "HDA Intel PCH"
                alsa.long_card_name = "HDA Intel PCH at 0x2fff020000 irq 140"
                alsa.driver_name = "snd_hda_intel"
                device.bus_path = "pci-0000:00:1f.3"
                sysfs.path = "/devices/pci0000:00/0000:00:1f.3/sound/card0"
                device.bus = "pci"
                device.vendor.id = "8086"
                device.vendor.name = "Intel Corporation"
                device.product.id = "9d71"
                device.product.name = "Sunrise Point-LP HD Audio"
                device.form_factor = "internal"
                device.string = "0"
                module-udev-detect.discovered = "1"
                device.icon_name = "audio-card-pci"

You will need to take note of the name of the source. For example:

alsa_output.pci-0000_00_1f.3.analog-stereo.monitor

Once you are ready to record simply run the following ffmpeg command:

ffmpeg -f pulse -i alsa_output.pci-0000_00_1f.3.analog-stereo.monitor output.mp3

Hit q to stop recording when the audio has finished playback. Simple.

Bonus - Extracting part of an MP3 with ffmpeg

If you were too slow to stop the recording and would like to extract a smaller part of your MP3, simply find the start time (00:12 for example) and end time (29:23 for example) and run the following command:

ffmpeg -i inputfile.mp3 -acodec copy -ss 00:12 -to 29:23 output.mp3

You can see what else ffmpeg is capable of by heading over to their documentation online https://ffmpeg.org/ffmpeg.html.