#!/usr/bin/env node "use strict"; var hlsmon = require('commander'); hlsmon.version('0.0.0') .usage('[options] ') .option('-a', '--user-agent ', 'User-Agent') .parse(process.argv); var url = require('url'); var HlsSegmentReader = require('hls-segment-reader'); var src = process.argv[2]; var sep = ';'; function monitor(srcUrl) { var r = new HlsSegmentReader(srcUrl, { fullStream:true, withData:false }); var contentBytes = function (segmentInfo) { if (segmentInfo.type === 'segment') { return segmentInfo.segment.details.byterange ? +segmentInfo.segment.details.byterange.length : segmentInfo.file.size; } if (segmentInfo.type === 'init') { return segmentInfo.init.byterange ? parseInt(segmentInfo.init.quotedString('byterange'), 10): segmentInfo.file.size; } return segmentInfo.file.size; }; var time = 0; r.on('data', function (segmentInfo) { var meta = segmentInfo.file; var size = contentBytes(segmentInfo); var duration = +(segmentInfo.segment && segmentInfo.segment.details.duration); console.log(meta.modified.toJSON() + sep + size + sep + duration.toFixed(3) + sep + (size / (duration * 1024 / 8)).toFixed(3)); time += duration; }); r.once('index', function() { // wait until first index is returned before attaching error listener. // this will enable initials errors to throw r.on('error', function(err) { console.error('reader error', err.stack || err); }); }); r.on('end', function() { if (r.index && r.index.master) { var newUrl = url.resolve(r.baseUrl, r.index.variants[0].uri); console.error('found variant index, using: ', newUrl); return monitor(newUrl); } console.error('done'); }); } monitor(src);