0

I have researched this and found that I cannot find data on our how to read live metadata from Icecast streams.

What I would like to do is something like this. Note this script may not work - it's just a concept:

var http = require('http');
var fs = require('fs');
http.createServer(function(req, res) {
    res.writeHead(200, {
        'Content-Type': 'audio/mp3'
    });
    var rs = fs.createReadStream('ICECAST STREAM');
    rs.pipe(res);
})

However, when you try this script it won't work due to it can't read a remote stream.

Another issue is how do I get it to read metadata in the stream? I want to do some triggering when the metadata equals a certain tag.

Update script

var http = require('http'),fs   = require('fs'),request = require('request');
var icecast = require('icecast-stack');
var url = 'http://stream.radiomedia.com.au:8003/stream'; // URL to a known Icecast stream
var stream = icecast.createReadStream(url);


http.createServer(function(request, response) {
    response.writeHead(200, {
        'Content-Type': 'audio/mpeg',
        'Content-Length': 15000
    });
// Fired when the `net.Stream` has it's 'connect' event.
stream.on('connect', function() {
  console.error("Radio Stream connected!");
});

// Fired after the HTTP response headers have been received.
stream.on('response', function(res) {
  console.error("Radio Stream response!");
  console.error(res.headers);
});

// When a 'metadata' event happens, usually a new song is starting.
stream.on('metadata', function(metadata) {
  var title = icecast.parseMetadata(metadata).StreamTitle;
  if(title === "SkypeTherapy.pro - Ad 1 [2b]"){
  console.error("advert");
  }
  else{
      console.error(title);
  }

});

// Proxy the raw audio stream to 'stdout', redirect to a file!
//stream.pipe(process.stdout);
http.get(url, res => res.pipe(fs.createReadStream('some.mp3')));
})
.listen(2000);

However the script does not play and instead just prints data in the console.

halfer
  • 19,824
  • 17
  • 99
  • 186
RussellHarrower
  • 6,470
  • 21
  • 102
  • 204
  • Are you planning to use that on one particular Icecast instance? What's the use case? – TBR Sep 22 '19 at 14:42
  • The use case is to read metadata and then when script sees metadata tag say Advert Advert is fires off another fs.createReadStream to load an advert from a URL then connects the user back to the icecast stream. This is the only way I can fire adverts or tracks that are geo located to the end user. Services like AdWizz do something like this (not 100% sure how they do it) but this is my version which I intend to make open sourced – RussellHarrower Sep 22 '19 at 14:53
  • @RussellHarrower See my answer here: https://stackoverflow.com/a/4914538/362536 – Brad Sep 22 '19 at 21:13
  • @Brad nodejs not PHP – RussellHarrower Sep 23 '19 at 07:43
  • @RussellHarrower Yes, I saw that, but if you please read the answer you'll see that it's language-agnostic. The only difference is that you *can* use a regular HTTP client these days. So, make your HTTP request to the stream and parse the stream as described in that answer. – Brad Sep 23 '19 at 14:06

0 Answers0