2016-07-26 18:25:18 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var util = require('util');
|
|
|
|
var stream = require('stream');
|
|
|
|
|
|
|
|
var FfmpegCommand = require('fluent-ffmpeg');
|
|
|
|
|
|
|
|
var helpers = require('./helpers');
|
2016-08-07 09:02:21 +00:00
|
|
|
var logger = require('./logger');
|
2016-07-26 18:25:18 +00:00
|
|
|
var BaseHTTPStreamProcessor = require('./basehttpstreamprocessor');
|
|
|
|
|
|
|
|
|
|
|
|
function HTTPFFMPEGProcessor()
|
|
|
|
{
|
|
|
|
BaseHTTPStreamProcessor.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(HTTPFFMPEGProcessor, BaseHTTPStreamProcessor);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HTTPFFMPEGProcessor.prototype.run = function()
|
|
|
|
{
|
|
|
|
this.output = new stream.PassThrough();
|
2016-08-07 08:05:18 +00:00
|
|
|
this.filename = helpers.createVariableFilename(this.cam.options.filename, this.now,
|
|
|
|
{
|
|
|
|
camId: this.camId
|
|
|
|
});
|
|
|
|
|
|
|
|
this.tempFilename = filename + '.recording';
|
|
|
|
|
|
|
|
|
2016-07-26 18:25:18 +00:00
|
|
|
var command = new FfmpegCommand();
|
|
|
|
command
|
|
|
|
.input(this.output)
|
|
|
|
.inputFormat(this.cam.options.inputFormat);
|
|
|
|
|
|
|
|
if (this.cam.options.inputFormat === 'mjpeg')
|
|
|
|
command.inputOption('-use_wallclock_as_timestamps 1');
|
|
|
|
|
|
|
|
command
|
2016-08-07 08:05:18 +00:00
|
|
|
.output(this.tempFilename)
|
2016-07-26 18:25:18 +00:00
|
|
|
.videoCodec(this.cam.options.videoCodec)
|
2016-08-07 08:05:18 +00:00
|
|
|
.outputFormat(this.cam.options.outputFormat);
|
|
|
|
|
|
|
|
command.on('error', function(err, stdout, stderr)
|
|
|
|
{
|
2016-08-07 09:02:21 +00:00
|
|
|
logger.error('FFmpeg output:' + err.message);
|
2016-08-07 08:05:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
command.run();
|
2016-07-26 18:25:18 +00:00
|
|
|
|
|
|
|
HTTPFFMPEGProcessor.super_.prototype.run.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HTTPFFMPEGProcessor.prototype.getStream = function()
|
|
|
|
{
|
|
|
|
return this.output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HTTPFFMPEGProcessor.prototype.cleanup = function()
|
|
|
|
{
|
|
|
|
if (this.output !== null)
|
|
|
|
{
|
|
|
|
this.output.end();
|
|
|
|
this.output = null;
|
|
|
|
}
|
2016-08-07 08:05:18 +00:00
|
|
|
|
|
|
|
fs.rename(this.tempFilename, this.filename, function(err)
|
|
|
|
{
|
2016-08-07 09:12:39 +00:00
|
|
|
if (err)
|
|
|
|
logger.error('Could not move ' + this.tempFilename + ' to ' + this.filename + ': ' + err);
|
2016-08-07 08:05:18 +00:00
|
|
|
});
|
2016-07-26 18:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = HTTPFFMPEGProcessor;
|