October 30, 2016

Using Nodejs to record microphone input to mp3 files on Ubuntu

https://ubuntuforums.org/archive/index.php/t-224748.html was very helpful.

Install lame mp3 encoder if you don’t have it.

sudo apt-get install lame

You should already have arecord, which records audio sends it to stdout.

  1. Run the command alsamixer to see your audio inputs and tweak volumes
  2. Run the command arecord -f cd | lame – out.mp3 to record audio to an mp3 file called out.mp3 until you hit ctrl-c

Now do that with Nodejs!

OK! This will record audio until you exit the script with Ctrl+C.

const spawn = require("child_process").spawn;

// prepare 2 child processes
const recordProcess = spawn("arecord", ["-f", "cd"]);
const encodeProcess = spawn("lame", ["-", "out.mp3"]);

// pipe them

// get debug info if you want
recordProcess.stdout.on('data', function (data) {
  console.log('Data: ' + data);
recordProcess.stderr.on('data', function (data) {
  console.log('Error: ' + data);
recordProcess.on('close', function (code) {
  console.log('arecord closed: ' + code);

// this seems like a good idea, but might not be needed
process.on("exit", (code) => {
  console.log(`About to exit with code: ${code}`);