hlsrecord 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env node
  2. /* eslint-disable no-process-exit */
  3. "use strict";
  4. // record a live hls-stream storing an on-demand ready version
  5. var hlsrecord = require('commander');
  6. hlsrecord.version(require('../package').version)
  7. .usage('[options] <url>')
  8. .option('-o, --output <dir>', 'Output directory')
  9. .option('-c, --create-dir', 'Explicitly create output dir')
  10. .option('-b, --begin-date <date>', 'Start recording at', dateValue)
  11. .option('-e, --end-date <date>', 'Stop recording at', dateValue)
  12. .option('-s, --start-offset <seconds>', 'Playback start time offset', parseFloat)
  13. .option('--extension <label>', 'preserve vendor extension', function (val) {
  14. return (hlsrecord.extension || []).concat(val);
  15. })
  16. .option('--segment-ext <label>', 'preserve vendor segment extension', function (val) {
  17. return (hlsrecord.segmentExt || []).concat(val);
  18. })
  19. // .option('-a, --user-agent <string>', 'HTTP User-Agent')
  20. // .option('-f, --full', 'record all variants')
  21. .parse(process.argv);
  22. function dateValue(val) {
  23. // FIXME: negative values doesn't work with commander - https://github.com/visionmedia/commander.js/issues/61
  24. if (val === 'now') return new Date();
  25. if (val.length && (val[0] === '+' || val[0] === '-'))
  26. return new Date(Math.round(new Date().getTime() / 1000 + parseInt(val, 10)) * 1000);
  27. if (parseInt(val, 10) == val)
  28. return new Date(parseInt(val, 10) * 1000);
  29. return new Date(val);
  30. }
  31. var fs = require('fs');
  32. var HlsSegmentReader = require('hls-segment-reader');
  33. var recorder = require('../lib/recorder');
  34. var src = hlsrecord.args[0];
  35. if (!src) {
  36. hlsrecord.help();
  37. process.exit(-1);
  38. }
  39. var outDir = hlsrecord.output || 'stream';
  40. if (hlsrecord.createDir)
  41. fs.mkdirSync(outDir);
  42. if (hlsrecord.beginDate)
  43. console.log('fetching from:', hlsrecord.beginDate);
  44. if (hlsrecord.endDate)
  45. console.log('fetching until:', hlsrecord.endDate);
  46. var extensions = {};
  47. (hlsrecord.extension || []).forEach(function(ext) {
  48. extensions[ext] = false;
  49. });
  50. (hlsrecord.segmentExt || []).forEach(function(ext) {
  51. extensions[ext] = true;
  52. });
  53. var readerOptions = {
  54. withData: true,
  55. fullStream: !hlsrecord.beginDate,
  56. startDate: hlsrecord.beginDate,
  57. stopDate: hlsrecord.endDate,
  58. maxStallTime: 5 * 60 * 1000,
  59. extensions: extensions,
  60. highWaterMark: 0,
  61. };
  62. function createReader(src) {
  63. var r = new HlsSegmentReader(src, readerOptions);
  64. r.on('error', function(err) {
  65. console.error('reader error', err);
  66. });
  67. return r;
  68. }
  69. var rdr = createReader(src);
  70. recorder(rdr, outDir, { subreader:createReader, startOffset: hlsrecord.startOffset }).start();