Skip to content

Instantly share code, notes, and snippets.

@rustyconover
Created January 12, 2021 16:12
Show Gist options
  • Save rustyconover/a03ba4ec460531999cba252c338988b1 to your computer and use it in GitHub Desktop.
Save rustyconover/a03ba4ec460531999cba252c338988b1 to your computer and use it in GitHub Desktop.
Listen to Icecast Stream
function listenToStream(stream_id: number, description: string) {
const options = {
hostname: 'broadcastify.cdnstream1.com',
port: 443,
headers: {
'sec-ch-ua-mobile': '?0',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
'Accept': '*/*',
'Origin': 'https://www.broadcastify.com',
'Sec-Fetch-Site': 'cross-site',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'audio',
'Referer': 'https://www.broadcastify.com/',
'Accept-Language': 'en-US,en;q=0.9',
'Range': 'bytes=0-'
},
path: `/${stream_id}`,
method: 'GET'
};
// Accumulate the data
const interval_data: Buffer[] = [];
const req = https.request(options, (res) => {
if (res.statusCode !== 200) {
console.error(`Bad stream HTTP status code (${stream_id}) (${description}) ${res.statusCode}, will retry`);
setTimeout(listenToStream, 5000);
return;
}
const timeout_handler = () => {
// Make one large buffer containing all of the partial
// response data.
const final_data = Buffer.concat(interval_data);
// Remove all of the previously received buffers of data
interval_data.splice(0);
// Start a new timer for a sampling interval.
setTimeout(timeout_handler, window_length);
// Send the data to ffmpeg and parse out the values.
analyze_data(final_data, description);
};
setTimeout(timeout_handler, window_length);
res.on('data', (d) => {
interval_data.push(d);
});
res.once('end', () => {
console.log(`Reached the end of the stream`);
// Try to listen again after 2 seconds.
setTimeout(listenToStream, 2000);
})
});
req.on('error', (e) => {
console.error(`Error receiving stream: ${e}`);
// Try to listen again after 2 seconds.
setTimeout(listenToStream, 2000);
});
req.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment