hlsdump 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env node
  2. /* eslint-disable no-process-exit */
  3. "use strict";
  4. var hlsdump = require('commander');
  5. hlsdump.version('0.0.0')
  6. .usage('[options] <url>')
  7. .option('-o, --output <path>', 'target file')
  8. .option('-u, --udp [host:port]', 'relay TS over UDP', function(val) {
  9. var r = { host: 'localhost', port: 1234 };
  10. if (val) {
  11. var s = val.split(':');
  12. if (s.length === 1) {
  13. r.port = parseInt(s[0], 10);
  14. } else {
  15. r.host = s[0];
  16. r.port = parseInt(s[1], 10);
  17. }
  18. }
  19. return r;
  20. })
  21. .option('-b, --buffer-size <bytes>|full', 'try to buffer <bytes> of input data (implies -s)', function(val) {
  22. if (val === 'full') return 0x80000000 - 1;
  23. return parseInt(val, 0);
  24. })
  25. .option('-s, --sync', 'clock sync using stream PCR')
  26. .option('-f, --full-stream', 'fetch all stream data')
  27. .option('-c, --concurrent <count>', 'fetch using concurrent connections', parseInt)
  28. .option('-a, --user-agent <string>', 'HTTP User-Agent')
  29. .option('-i, --info-port <port>', 'report status using HTTP + json', parseInt)
  30. .option('--cookie <data>', 'add cookie header to key requests')
  31. .option('--key <hex>', 'use oob key for decrypting segments', function(opt) {return new Buffer(opt, 'hex');})
  32. .parse(process.argv);
  33. var fs = require('fs'),
  34. http = require('http');
  35. var oncemore = require('oncemore'),
  36. HlsSegmentReader = require('hls-segment-reader'),
  37. UdpBlast = require('udp-blast');
  38. var HlsReader = require('../lib/hls-reader');
  39. var stats = require('measured').createCollection();
  40. var src = hlsdump.args[0];
  41. if (!src) {
  42. hlsdump.help();
  43. process.exit(-1);
  44. }
  45. if (hlsdump.bufferSize) hlsdump.sync = true;
  46. var r = new HlsReader(new HlsSegmentReader(src, { highWaterMark: (hlsdump.concurrent || 1) - 1, fullStream: hlsdump.fullStream }), hlsdump);
  47. var totalDuration = 0, currentSegment = -1;
  48. r.on('segment', function(data) {
  49. var downloadSize = data.meta.size;
  50. var duration = data.segment.duration;
  51. // calculate size when missing
  52. if (downloadSize === -1) {
  53. downloadSize = 0;
  54. data.stream.on('data', function(chunk) {
  55. downloadSize += chunk.length;
  56. });
  57. }
  58. var stopwatch = stats.timer('fetchTime').start();
  59. oncemore(data.stream).once('close', 'end', 'error', function(err) {
  60. stopwatch.end();
  61. console.error('segment done at ' + totalDuration.toFixed(0) + ' seconds, avg bitrate (kbps):', (downloadSize / (duration * 1024 / 8)).toFixed(1));
  62. if (err) {
  63. stats.meter('streamErrors').mark();
  64. }
  65. });
  66. });
  67. if (hlsdump.udp) {
  68. var dst = (hlsdump.udp === true) ? null : hlsdump.udp;
  69. r.pipe(new UdpBlast(dst, { packetSize: 7 * 188 }));
  70. }
  71. if (hlsdump.output) {
  72. if (hlsdump.output === '-')
  73. r.pipe(process.stdout);
  74. else
  75. r.pipe(fs.createWriteStream(hlsdump.output));
  76. }
  77. // setup stat tracking
  78. stats.gauge('bufferBytes', function() { return r.buffer._readableState.length/* + buffer._writableState.length*/; });
  79. stats.gauge('currentSegment', function() { return currentSegment; });
  80. stats.gauge('index.first', function() { return r.reader.index ? r.reader.index.first_seq_no : -1; });
  81. stats.gauge('index.last', function() { return r.reader.index ? r.reader.index.lastSeqNo() : -1; });
  82. stats.gauge('totalDuration', function() { return totalDuration; });
  83. stats.meter('streamErrors');
  84. if (hlsdump.infoPort) {
  85. http.createServer(function (req, res) {
  86. if (req.method === 'GET') {
  87. var data = JSON.stringify(stats, null, ' ');
  88. res.writeHead(200, {
  89. 'Content-Type': 'application/json',
  90. 'Content-Length': data.length
  91. });
  92. res.write(data);
  93. }
  94. res.end();
  95. }).listen(hlsdump.infoPort);
  96. }