hlsmon 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env node
  2. "use strict";
  3. var hlsmon = require('commander');
  4. hlsmon.version('0.0.0')
  5. .usage('[options] <url>')
  6. .option('-a', '--user-agent <string>', 'User-Agent')
  7. .parse(process.argv);
  8. var url = require('url');
  9. var HlsSegmentReader = require('hls-segment-reader');
  10. var src = process.argv[2];
  11. var sep = ';';
  12. function monitor(srcUrl) {
  13. var r = new HlsSegmentReader(srcUrl, { fullStream:true, withData:false });
  14. var contentBytes = function (segmentInfo) {
  15. if (segmentInfo.type === 'segment') {
  16. return segmentInfo.segment.details.byterange ? +segmentInfo.segment.details.byterange.length : segmentInfo.file.size;
  17. }
  18. if (segmentInfo.type === 'init') {
  19. return segmentInfo.init.byterange ? parseInt(segmentInfo.init.quotedString('byterange'), 10): segmentInfo.file.size;
  20. }
  21. return segmentInfo.file.size;
  22. };
  23. var time = 0;
  24. r.on('data', function (segmentInfo) {
  25. var meta = segmentInfo.file;
  26. var size = contentBytes(segmentInfo);
  27. var duration = +(segmentInfo.segment && segmentInfo.segment.details.duration);
  28. console.log(meta.modified.toJSON() + sep + size + sep + duration.toFixed(3) + sep + (size / (duration * 1024 / 8)).toFixed(3));
  29. time += duration;
  30. });
  31. r.once('index', function() {
  32. // wait until first index is returned before attaching error listener.
  33. // this will enable initials errors to throw
  34. r.on('error', function(err) {
  35. console.error('reader error', err.stack || err);
  36. });
  37. });
  38. r.on('end', function() {
  39. if (r.index && r.index.master) {
  40. var newUrl = url.resolve(r.baseUrl, r.index.variants[0].uri);
  41. console.error('found variant index, using: ', newUrl);
  42. return monitor(newUrl);
  43. }
  44. console.error('done');
  45. });
  46. }
  47. monitor(src);