hlsrecord 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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('-a, --user-agent <string>', 'HTTP User-Agent')
  13. // .option('-f, --full', 'record all variants')
  14. .parse(process.argv);
  15. function dateValue(val) {
  16. // FIXME: negative values doesn't work with commander, as
  17. if (val === 'now') return new Date();
  18. if (val.length && (val[0] === '+' || val[0] === '-'))
  19. return new Date(Math.round(new Date().getTime() / 1000 + parseInt(val, 10)) * 1000);
  20. if (parseInt(val, 10) == val)
  21. return new Date(parseInt(val, 10) * 1000);
  22. return new Date(val);
  23. }
  24. var fs = require('fs'),
  25. path = require('path'),
  26. util = require('util');
  27. var mime = require('mime');
  28. var reader = require('../lib/reader'),
  29. recorder = require('../lib/recorder');
  30. mime.define({
  31. 'application/vnd.apple.mpegURL': ['m3u8'],
  32. 'video/mp2t': ['ts'],
  33. 'audio/x-aac': ['aac'],
  34. 'audio/aac': ['aac'],
  35. 'audio/ac3': ['ac3'],
  36. });
  37. var src = hlsrecord.args[0];
  38. if (!src) {
  39. hlsrecord.help();
  40. process.exit(-1);
  41. }
  42. var outDir = hlsrecord.output || 'stream';
  43. if (hlsrecord.createDir)
  44. fs.mkdirSync(outDir);
  45. if (hlsrecord.beginDate)
  46. console.log('fetching from:', hlsrecord.beginDate);
  47. if (hlsrecord.endDate)
  48. console.log('fetching until:', hlsrecord.endDate);
  49. var options = {
  50. startDate: hlsrecord.beginDate,
  51. stopDate: hlsrecord.endDate,
  52. maxStallTime: 5 * 60 * 1000,
  53. fullStream:true,
  54. highWaterMark:0,
  55. };
  56. function createReader(src) {
  57. var r = reader(src, options);
  58. r.on('error', function(err) {
  59. console.error('reader error', err);
  60. });
  61. return r;
  62. }
  63. var rdr = createReader(src);
  64. recorder(rdr, outDir, { subreader:createReader }).start();