Oh PulseAudio, you are an interesting little beast
Par thisisabore le samedi 7 avril 2012, 00:58 - Logiciel Libre | Free Software - Lien permanent
So, one can record anything going through PulseAudio (and therefore really, any sound being played back on Ubuntu or Debian) very simply.
Of course, this comes as no surprise.
After all, everything is a copy on a computer, no matter how many layers of
impracticality you add for a user (cumbersome flash players à la
myspace, silly DRM, etc.) to prevent them from accessing that copy.
No, what's great here is how simple it is to dump the sound being played back, once you look into it.
Without any further ado, here is how to dump anything being played back as mp3, Ogg Vorbis or the almighty FLAC:
mp3: parec -d alsa_output.pci-0000_00_1b.0.analog-stereo.monitor |
lame -r -V0 - test.mp3
Ogg Vorbis: parec -d
alsa_output.pci-0000_00_1b.0.analog-stereo.monitor | oggenc --raw -q 8 -o
test.ogg -
FLAC: parec -d alsa_output.pci-0000_00_1b.0.analog-stereo.monitor |
flac --force-raw-format --endian=little --channels=2 --sample-rate=44100
--sign=signed --bps=16 -o test.flac -
parec is the command to record from a PulseAudio device, and
-d the flag to specify which device to record from (this must be
an input device, and will vary from system to system) We then pipe the data
(using the pipe symbol | ) to an encoder, either lame for mp3, or
oggenc for ogg vorbis.
For lame, we specify the data is coming in raw format
(-r) and we ask the sound to be encoded in V0 quality (~256 KBps).
We tell it to use the stdin input (represented by single dash -)
and to output to a file called test.mp3.
For oggenc, we specify the data is coming in raw format with
the —you guessed it— --raw flag and ask for a quality of 8 (out of
10) with the -q 8 flag. We also request an output file named
test.ogg with -o test.ogg, and tell it to use stdin as the input
file (to use the data piped through with the |) with the final
-.
For flac, things are a little more complicated, as
flac demands more information when you encode from stdin. You have
to specify the endianness of the data coming in with
--endian=little (I tried both big or little, you want little), the
number of channels (1 for mono, 2 for stereo, 6 for 5.1 etc.) with
--channels=2, the sample rate (44 100 KHz is what CDs use, DVDs
are usually 48 000) with --sample-rate=44100, --sign
to “Set the sign of samples (the default is signed)” (yes, I'm quoting from the
manual, no idea what this does) and finally, the number of bits per second,
which is 16 on CDs (hence --bps=16). We then ask for the data to
be written to an external file called test.flac with -o test.flac
and tell flac to get its input data from stdin (and therefore from
parec through the pipe) with the final dash.
That's it.
Neat, eh?

