hlsrecord 1.9 KB

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