Created
April 1, 2013 22:34
-
-
Save anatoliychakkaev/5288341 to your computer and use it in GitHub Desktop.
Example of using streams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var express = require('express'); | |
var spawn = require('child_process').spawn; | |
var icecast = require('icecast'); | |
// URL to a known Icecast stream | |
var url = 'http://ice.somafm.com/spacestation'; | |
// connect to the remote stream | |
icecast.get(url, function (res) { | |
// log the HTTP response headers | |
console.error(res.headers); | |
var file, old; | |
// log any "metadata" events that happen | |
res.on('metadata', function (metadata) { | |
var parsed = icecast.parse(metadata); | |
console.log('Now playing', parsed.StreamTitle); | |
if (file) { | |
old = file; | |
res.unpipe(old); | |
} | |
file = fs.createWriteStream('./' + parsed.StreamTitle + '.mp3', { | |
flags: 'a', | |
mode: 0666 | |
}); | |
res.pipe(file); | |
if (old) { | |
old.end(); | |
} | |
}); | |
var ff = fs.createWriteStream('./stream.mp3', { | |
flags: 'a', | |
mode: 0666 | |
}); | |
res.pipe(ff); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment