| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #!/usr/bin/env node
- /* eslint-disable no-process-exit */
- "use strict";
- // record a live hls-stream storing an on-demand ready version
- var fs = require('fs'),
- path = require('path');
- var nopt = require('noptify/node_modules/nopt');
- var HlsSegmentReader = require('hls-segment-reader');
- var recorder = require('../lib/recorder');
- function DateValue(){}
- nopt.typeDefs[DateValue] = { type: DateValue, validate: function (data, key, val) {
- var date;
- if (val === 'now') val = '+0';
- if (val.length && (val[0] === '+' || val[0] === '-')) {
- date = new Date(Math.round(new Date().getTime() / 1000 + parseInt(val, 10)) * 1000);
- } else if (parseInt(val, 10) == val) {
- date = new Date(parseInt(val, 10) * 1000);
- } else {
- date = new Date(val);
- }
- if (!date) return false;
- data[key] = date;
- }};
- function HexValue(){}
- nopt.typeDefs[HexValue] = { type: HexValue, validate: function (data, key, val) {
- console.log('!', data, key, val)
- data[key] = new Buffer(val, 'hex');
- }};
- var hlsrecord = require('noptify')(process.argv, { program: 'hlsrecord <url>' });
- hlsrecord.version(require('../package').version)
- .option('collect', '-C', 'Collect output segments to a single file', Boolean)
- .option('output', '-o', 'Output directory', path)
- .option('create-dir', '-c', 'Explicitly create output dir', Boolean)
- .option('begin-date', '-b', 'Start recording at', DateValue)
- .option('end-date', '-e', 'Stop recording at', DateValue)
- .option('start-offset', '-s', 'Playback start time offset in seconds', Number)
- .option('create-dir', '-c', 'Explicitly create output dir', Boolean)
- .option('extension', 'Preserve specified vendor extension', Array)
- .option('segment-extension', 'Preserve specified vendor segment extension', Array)
- // .option('-a, --user-agent <string>', 'HTTP User-Agent')
- // .option('-f, --full', 'record all variants')
- .parse(process.argv);
- var options = hlsrecord.nopt;
- var src = options.argv.remain[0];
- if (!src) {
- hlsrecord.help();
- process.exit(-1);
- }
- var outDir = options.output || 'stream';
- if (options['create-dir'])
- fs.mkdirSync(outDir);
- if (options['begin-date'])
- console.log('fetching from:', options['begin-date']);
- if (options['end-date'])
- console.log('fetching until:', options['end-date']);
- var extensions = {};
- (options.extension || []).forEach(function(ext) {
- extensions[ext] = false;
- });
- (options['segment-extension'] || []).forEach(function(ext) {
- extensions[ext] = true;
- });
- var readerOptions = {
- withData: true,
- fullStream: !options['begin-date'],
- startDate: options['begin-date'],
- stopDate: options['end-date'],
- maxStallTime: 5 * 60 * 1000,
- extensions: extensions,
- highWaterMark: 0,
- };
- function createReader(src) {
- var r = new HlsSegmentReader(src, readerOptions);
- r.on('error', function(err) {
- console.error('reader error', err);
- });
- return r;
- }
- var rdr = createReader(src);
- recorder(rdr, outDir, { subreader:createReader, startOffset: options['start-offset'], collect: options.collect }).start();
|