How to Record the Sound Coming From an Application With Pulseaudio
fleeting- External reference: https://askubuntu.com/questions/60837/record-a-programs-output-with-pulseaudio
Say I want to save something that is currently played on my machine.
recording the sound card
Pulseaudio provides a source called monitor for each sound card
pacmd list-sources | grep name: |grep monitor
name: <alsa_output.usb-GeneralPlus_USB_Audio_Device-00.analog-stereo.monitor>
name: <alsa_output.usb-C-Media_Electronics_Inc._USB_Audio_Device-00.analog-stereo.monitor>
name: <alsa_output.pci-0000_00_1f.3.analog-stereo.monitor>
I can see that I have a monitor source for each card I have.
I only need to run parecord then.
parecord --device=alsa_output.usb-GeneralPlus_USB_Audio_Device-00.analog-stereo.monitor > output.wav
recording from an application only
Following this instructions1
pacmd list-sink-inputs
2 sink input(s) available.
index: 131
driver: <protocol-native.c>
flags:
state: RUNNING
sink: 0 <alsa_output.usb-GeneralPlus_USB_Audio_Device-00.analog-stereo>
volume: front-left: 65536 / 100% / 0.00 dB, front-right: 65536 / 100% / 0.00 dB
balance 0.00
muted: no
current latency: 287.33 ms
requested latency: 42.67 ms
sample spec: s16le 2ch 48000Hz
channel map: front-left,front-right
Stereo
resample method: (null)
module: 9
client: 147 <ALSA plug-in [sox]>
properties:
media.name = "ALSA Playback"
application.name = "ALSA plug-in [sox]"
native-protocol.peer = "UNIX socket client"
native-protocol.version = "35"
application.process.id = "2753496"
application.process.user = "sam"
application.process.host = "konixwork"
application.process.binary = "sox"
application.language = "C"
window.x11.display = ":0"
application.process.machine_id = "21fdcf6901834b06813ffa8423c27210"
application.process.session_id = "2"
module-stream-restore.id = "sink-input-by-application-name:ALSA plug-in [sox]"
index: 144
driver: <protocol-native.c>
flags:
state: RUNNING
sink: 0 <alsa_output.usb-GeneralPlus_USB_Audio_Device-00.analog-stereo>
volume: front-left: 65536 / 100% / 0.00 dB, front-right: 65536 / 100% / 0.00 dB
balance 0.00
muted: no
current latency: 107.98 ms
requested latency: 30.00 ms
sample spec: s16le 2ch 48000Hz
channel map: front-left,front-right
Stereo
resample method: (null)
module: 19
client: 189 <ALSA plug-in [snapclient]>
properties:
media.name = "ALSA Playback"
application.name = "ALSA plug-in [snapclient]"
native-protocol.peer = "UNIX socket client"
native-protocol.version = "33"
application.process.id = "1"
application.process.user = "sam"
application.process.host = "konixwork"
application.process.binary = "snapclient"
application.language = "C"
application.process.machine_id = "79372d8564c8545262975c46157f1482"
module-stream-restore.id = "sink-input-by-application-name:ALSA plug-in [snapclient]"
My snapclient is running on sink 144.
I then run
pactl load-module module-null-sink sink_name=steam
pactl move-sink-input 144 steam
And get the sound to stop being heard.
Then
parecord -d steam.monitor > truc.wav
And then vlc truc.wav
shows that the file indeed contains the sound.
I don’t know the difference between parecord and parec, but the suggested command works as well.
parec -d steam.monitor | oggenc -b 192 -o steam.ogg --raw -
record only one application while still hearing the sound
I want the sink input to go to the null sink and be recorded to a file while still be streamed.
I can combine two sinks into one with the module-combine-sink.
Provided I already created the null sink.
pactl load-module module-combine-sink sink_name=combined slaves=steam,alsa_output.usb-GeneralPlus_USB_Audio_Device-00.analog-stereo
Then I can move the application to use the combined sink.
pactl move-sink-input 144 combined
The sound will go to the sound card AND to the null sink, allowing me to hear the sound while recording it with.
parecord -d steam.monitor > truc.wav
doing the same thing without the extra null sink
Actually, what I need is only a proxy sink that will redirect its content to the sound card and record this proxy. This can simply achieved with the same module.
pactl load-module module-combine-sink sink_name=proxy slaves=alsa_output.usb-GeneralPlus_USB_Audio_Device-00.analog-stereo
parecord -d proxy.monitor > output.wav
Permalink
-
Try something like this:
In a terminal enter
pacmd (this is the CLI of the PulseAudio-Server) then use
list-sink-inputs (where you get the indices of the running inputs) Now find the index of your input. Now referred to as $INDEX
the scriptable part is:
- pactl load-module module-null-sink sink_name=steam
- pactl move-sink-input $INDEX steam
- parec -d steam.monitor | oggenc -b 192 -o steam.ogg –raw -
Explanations:
- The first command will add a null-sink as you already knew.
- The second command moves the sink-input from your standard-audio-sink to steam
- The third command records the monitor of the device steam (-d) and puts the output (raw-wave-stream) into oggenc, which encodes this wave-stream to an oga-file. (for mp3 use lame)
— https://askubuntu.com/questions/60837/record-a-programs-output-with-pulseaudio