hlsrecord 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env node
  2. "use strict";
  3. // record a live hls-stream storing an on-demand ready version
  4. var hlsrecord = require('commander');
  5. hlsrecord.version(require('../package').version)
  6. .usage('[options] <url>')
  7. .option('-o, --output <dir>', 'Output directory')
  8. .option('-c, --create-dir', 'Explicitly create output dir')
  9. .option('-b, --begin-date <date>', 'Start recording at', dateValue)
  10. .option('-e, --end-date <date>', 'Stop recording at', dateValue)
  11. // .option('-a, --user-agent <string>', 'HTTP User-Agent')
  12. // .option('-f, --full', 'record all variants')
  13. .parse(process.argv);
  14. function dateValue(val) {
  15. // FIXME: negative values doesn't work with commander, as
  16. if (val === 'now') return new Date();
  17. if (val.length && (val[0] === '+' || val[0] === '-'))
  18. return new Date(Math.round(new Date().getTime()/1000 + parseInt(val, 10))*1000);
  19. if (parseInt(val, 10) == val)
  20. return new Date(parseInt(val, 10)*1000);
  21. return new Date(val);
  22. }
  23. var fs = require('fs'),
  24. path = require('path'),
  25. util = require('util');
  26. var mime = require('mime');
  27. var reader = require('../lib/reader'),
  28. recorder = require('../lib/recorder');
  29. mime.define({
  30. 'application/vnd.apple.mpegURL': ['m3u8'],
  31. 'video/mp2t': ['ts'],
  32. 'audio/x-aac': ['aac'],
  33. 'audio/aac': ['aac'],
  34. 'audio/ac3': ['ac3'],
  35. });
  36. var src = hlsrecord.args[0];
  37. if (!src) return hlsrecord.help();
  38. var outDir = hlsrecord.output || 'stream';
  39. if (hlsrecord.createDir)
  40. fs.mkdirSync(outDir);
  41. if (hlsrecord.beginDate)
  42. console.log('fetching from:', hlsrecord.beginDate);
  43. if (hlsrecord.endDate)
  44. console.log('fetching until:', hlsrecord.endDate);
  45. var options = {
  46. startDate: hlsrecord.beginDate,
  47. stopDate: hlsrecord.endDate,
  48. maxStallTime: 5*60*1000,
  49. fullStream:true,
  50. highWaterMark:0,
  51. };
  52. function createReader(src) {
  53. var r = reader(src, options);
  54. r.on('error', function(err) {
  55. console.error('reader error', err);
  56. });
  57. return r;
  58. }
  59. var r = createReader(src);
  60. recorder(r, outDir, { subreader:createReader }).start();