#!/usr/bin/env node /* eslint-disable no-process-exit */ "use strict"; // record a live hls-stream storing an on-demand ready version var hlsrecord = require('commander'); hlsrecord.version(require('../package').version) .usage('[options] ') .option('-o, --output ', 'Output directory') .option('-c, --create-dir', 'Explicitly create output dir') .option('-b, --begin-date ', 'Start recording at', dateValue) .option('-e, --end-date ', 'Stop recording at', dateValue) .option('-s, --start-offset ', 'Playback start time offset', parseFloat) // .option('-a, --user-agent ', 'HTTP User-Agent') // .option('-f, --full', 'record all variants') .parse(process.argv); function dateValue(val) { // FIXME: negative values doesn't work with commander - https://github.com/visionmedia/commander.js/issues/61 if (val === 'now') return new Date(); if (val.length && (val[0] === '+' || val[0] === '-')) return new Date(Math.round(new Date().getTime() / 1000 + parseInt(val, 10)) * 1000); if (!isNaN(parseInt(val, 10))) return new Date(parseInt(val, 10) * 1000); return new Date(val); } var fs = require('fs'), path = require('path'), util = require('util'); var mime = require('mime'); var reader = require('../lib/reader'), recorder = require('../lib/recorder'); mime.define({ 'application/vnd.apple.mpegURL': ['m3u8'], 'video/mp2t': ['ts'], 'audio/x-aac': ['aac'], 'audio/aac': ['aac'], 'audio/ac3': ['ac3'], }); var src = hlsrecord.args[0]; if (!src) { hlsrecord.help(); process.exit(-1); } var outDir = hlsrecord.output || 'stream'; if (hlsrecord.createDir) fs.mkdirSync(outDir); if (hlsrecord.beginDate) console.log('fetching from:', hlsrecord.beginDate); if (hlsrecord.endDate) console.log('fetching until:', hlsrecord.endDate); var options = { startDate: hlsrecord.beginDate, stopDate: hlsrecord.endDate, maxStallTime: 5 * 60 * 1000, fullStream:true, highWaterMark:0, }; function createReader(src) { var r = reader(src, options); r.on('error', function(err) { console.error('reader error', err); }); return r; } var rdr = createReader(src); recorder(rdr, outDir, { subreader:createReader, startOffset: hlsrecord.startOffset }).start();