In my previous article, I share how to install Icecast on an Ubuntu instance on AWS EC2. This next article will now focus on publishing MP3 to Icecast (for web distribution) using NodeJS.

Why Node?

Although there are many excellent tools, like Nicecast, that can help you publish an Mp3 to Icecast, I want to show developers how to publish music in a scriptable way.

Step 1 - Getting Started

The next few steps will require us first to configure our local Mac environment. The tools we need include:

  • NodeJS
  • Node Package Manager (NPM)
  • Homebrew
  • Libshout

Installing Libshout

We will need to install libshout package using Homebrew. If you're not familiar with Homebrew, then read this tutorial.

brew install libshout

Installing Node Shout

Node Shout is a Javascript package that will help us publish an MP3 or OGG to our Icecast server. Suppose you do not have Node installed, download Node here.

npm install nodeshout --save

Step 2 - Paste this Code

Demo code found in the Node Shout /examples folder. I've configured a bit more.

Create a file titled /myapp/app.js and paste this code inside. Take note that we're referencing a song titled /myapp/song.mp3.

var nodeshout = require('nodeshout'),
	FileReadStream = require('nodeshout').FileReadStream,
    ShoutStream = require('nodeshout').ShoutStream,
    fs = require('fs')

nodeshout.init();

console.log('Libshout version: ' + nodeshout.getVersion());

var song = './mysong.mp3'

var shout = nodeshout.create();
	shout.setHost('xx.xx.xxx.xx');
	shout.setPort(8000);
	shout.setUser('source');
	shout.setPassword('hackme');
	shout.setMount('mymount');
	shout.setFormat(1); // 0=ogg, 1=mp3
	shout.setAudioInfo('bitrate', '160');
	shout.setAudioInfo('samplerate', '44100');
	shout.setAudioInfo('channels', '2');

shout.open();

var metadata = nodeshout.createMetadata();
	metadata.add('song', 'Sublime - What I got');

shout.setMetadata(metadata);

var fileStream = new FileReadStream(song, 65536),
    shoutStream = fileStream.pipe(new ShoutStream(shout));

fileStream.on('data', function(chunk) {
    console.log('Read %d bytes of data', chunk.length);
});

shoutStream.on('finish', function() {
    console.log('Finished playing...');
});

Step 3 - Run Code

Now it's time to execute the code.

node app.js

You should see something like this:
icecast-publish

Step 4 - Double-Check

You will know if things are working if you see something like this within the Admin page.
icecast-double-check


Resources