hlsdump 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env node
  2. var hlsdump = require('commander');
  3. hlsdump.version('0.0.0')
  4. .usage('[options] <url>')
  5. .option('-o, --output <path>', 'target file')
  6. .option('-u, --udp [host:port]', 'relay TS over UDP', function(val) {
  7. var r = { host:'localhost', port:1234 };
  8. if (val) {
  9. var s = val.split(':');
  10. if (s.length === 1) {
  11. r.port = parseInt(s[0], 10);
  12. } else {
  13. r.host = s[0];
  14. r.port = parseInt(s[1], 10);
  15. }
  16. }
  17. return r;
  18. })
  19. .option('-b, --buffer-size <bytes>|full', 'try to buffer <bytes> of input data (implies -s)', function(val) {
  20. if (val === 'full') return 0x80000000-1;
  21. return parseInt(val, 0);
  22. })
  23. .option('-s, --sync', 'clock sync using stream PCR')
  24. .option('-f, --full-stream', 'fetch all stream data')
  25. .option('-c, --concurrent <count>', 'fetch using concurrent connections', parseInt)
  26. .option('-a, --user-agent <string>', 'HTTP User-Agent')
  27. .parse(process.argv);
  28. var util = require('util'),
  29. url = require('url'),
  30. fs = require('fs');
  31. var reader = require('../lib/reader'),
  32. tssmooth = require('../lib/tssmooth'),
  33. tsblast = require('../lib/tsblast'),
  34. oncemore = require('../lib/oncemore');
  35. try {
  36. var Passthrough = require('stream').Passthrough;
  37. assert(Passthrough);
  38. } catch (e) {
  39. var Passthrough = require('readable-stream/passthrough');
  40. }
  41. var src = hlsdump.args[0];
  42. if (!src) return hlsdump.help();
  43. if (hlsdump.bufferSize) hlsdump.sync = true;
  44. var r = reader(src, {highWaterMark:(hlsdump.concurrent || 1) - 1, fullStream:hlsdump.fullStream});
  45. var time = 0;
  46. var reading = false;
  47. r.on('readable', function() {
  48. if (reading) return;// console.error('readable call error');
  49. function grabnext() {
  50. var obj = r.read();
  51. if (obj) {
  52. var meta = obj.meta;
  53. var duration = obj.segment.duration;
  54. var size = meta.size;
  55. time += duration;
  56. reading = true;
  57. obj.stream.pipe(buffer, { end: false });
  58. if (size === -1 || !hooked) {
  59. size = 0;
  60. obj.stream.on('data', function(chunk) {
  61. size += chunk.length;
  62. if (!hooked && size >= hlsdump.bufferSize)
  63. hook(buffer);
  64. });
  65. }
  66. oncemore(obj.stream).once('end', 'error', function(err) {
  67. reading = false;
  68. console.error('segment done at '+time.toFixed(0)+' seconds, avg bitrate (kbps):', (size / (duration * 1024/8)).toFixed(1));
  69. if (err) console.error('stream error', err.stack || err);
  70. else hook(buffer);
  71. grabnext();
  72. });
  73. }
  74. }
  75. grabnext();
  76. });
  77. r.once('index', function() {
  78. // wait until first index is returned before attaching error listener.
  79. // this will enable initials errors to throw
  80. r.on('error', function(err) {
  81. console.error('reader error', err.stack || err);
  82. });
  83. });
  84. r.on('end', function() {
  85. console.error('done');
  86. });
  87. var buffer = new Passthrough({highWaterMark:hlsdump.bufferSize});
  88. var outputs = [];
  89. if (hlsdump.udp)
  90. outputs.push(tsblast(hlsdump.udp));
  91. if (hlsdump.output) {
  92. if (hlsdump.output === '-')
  93. outputs.push(process.stdout);
  94. else
  95. outputs.push(fs.createWriteStream(hlsdump.output));
  96. }
  97. // the hook is used to prebuffer
  98. var hooked = false;
  99. function hook(stream) {
  100. if (hooked) return;
  101. console.error('hooking output');
  102. var s = stream;
  103. if (hlsdump.sync) {
  104. var smooth = tssmooth();
  105. smooth.on('unpipe', function() {
  106. this.unpipe();
  107. });
  108. smooth.once('error', function(err) {
  109. console.error('smooth error', err);
  110. unhook(stream);
  111. smooth.on('error', function () {});
  112. });
  113. s = s.pipe(smooth);
  114. }
  115. outputs.forEach(function (o) {
  116. s.pipe(o);
  117. });
  118. hooked = true;
  119. }
  120. function unhook(stream) {
  121. if (hooked) {
  122. console.error('unhooking output');
  123. stream.unpipe();
  124. hooked = false;
  125. }
  126. }
  127. if (!hlsdump.sync)
  128. hook(buffer);