Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streaming PulseAudio to file (possibly with GStreamer)

I'm on Ubuntu and I want to record PulseAudio output to a file, to make a recording of a pygame program. The format doesn't matter, because I can change it afterward, so raw audio is fine.

Looking around, it seems like GStreamer may be able to handle this, but I'm not familiar with it, and extensive searching has not yielded an answer. So answers involving GStreamer or otherwise are welcome.

Thanks!

like image 483
Cosmologicon Avatar asked Sep 21 '11 15:09

Cosmologicon


1 Answers

There is a monitor for each pulseaudio sink. You just need to get it's name:

$ pactl list
...
Sink #0
    State: RUNNING
    Name: alsa_output.pci-0000_00_1b.0.analog-stereo
    Description: Internal Audio Analog Stereo
    Driver: module-alsa-card.c
    Sample Specification: s16le 2ch 44100Hz
    Channel Map: front-left,front-right
    Owner Module: 4
    Mute: no
    Volume: 0:  40% 1:  40%
            0: -23.87 dB 1: -23.87 dB
            balance 0.00
    Base Volume:  96%
                 -1.00 dB
    Monitor Source: alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
    Latency: 119973 usec, configured 210000 usec
    Flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY 
    ...

Note line Monitor Source: alsa_output.pci-0000_00_1b.0.analog-stereo.monitor. It is your monitor source.

First, you need to unmute it:

$ pacmd
Welcome to PulseAudio! Use "help" for usage information.
>>> set-source-mute alsa_output.pci-0000_00_1b.0.analog-stereo.monitor false
>>> exit

And now you can record sound form it:

$ parec \
>     --format=s16le \
>     --device=alsa_output.pci-0000_00_1b.0.analog-stereo.monitor \
> | oggenc --raw --quiet -o dump.ogg -

Or with lame:

$ parec \
>    --format=s16le \
>    --device=alsa_output.pci-0000_00_1b.0.analog-stereo.monitor \
> | lame -r - dump.mp3

The same could be done with gstreamer, but there is not much sense in it if you don't need some complex processing:

$ gst-launch-0.10 \
>  pulsesrc device=alsa_output.pci-0000_00_1b.0.analog-stereo.monitor \
>  ! lame \
>  ! filesink location=dump.mp3
like image 126
max taldykin Avatar answered Oct 09 '22 15:10

max taldykin