1
0
mirror of synced 2024-11-16 11:03:50 +00:00

Initial commit

This commit is contained in:
Mark van Renswoude 2016-07-10 19:34:49 +02:00
commit 84b3e8f1a7
6 changed files with 186 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/config.js
/node_modules/
*.sublime-workspace

80
capture.js Normal file
View File

@ -0,0 +1,80 @@
var http = require('http');
var fs = require('fs');
var util = require('util');
var Readable = require('stream').Readable;
var FfmpegCommand = require('fluent-ffmpeg');
var PushStream = function(options) { Readable.call(this, options); };
util.inherits(PushStream, Readable);
PushStream.prototype._read = function(n) { };
module.exports =
{
start: function(camId, cam, now)
{
var timer = null;
var req = null;
console.log('Starting FFmpeg');
var output = new PushStream();
var command = new FfmpegCommand();
command
.input(output)
.inputFormat('mjpeg')
.inputOption('-use_wallclock_as_timestamps 1')
.output('D:/Temp/' + camId + '.avi')
.videoCodec('libx264')
.outputFormat('avi')
.run();
var cleanup = function()
{
if (timer !== null)
{
clearTimeout(timer);
timer = null;
}
if (command !== null)
{
output.push(null);
command = null;
}
};
req = http.request(cam.url, function(res)
{
timer = setTimeout(function()
{
console.log('Timeout!');
req.abort();
cleanup();
}, cam.timeout);
res.on('data', function(chunk)
{
output.push(chunk);
});
res.on('end', function()
{
console.log('End!');
cleanup();
});
});
req.on('error', function(e)
{
console.log(e);
cleanup();
});
req.end();
}
};

21
config.sample.js Normal file
View File

@ -0,0 +1,21 @@
var config = {};
config.port = 5705;
config.defaultOutputBase = '/srv/cam/';
config.defaultOutputFilename = 'YYYY-MM-DD HH.mm.ss <camId>.avi';
config.defaultTimeout = 5000;
config.cams =
{
frontdoor:
{
url: 'http://10.138.1.10/videostream.cgi?user=viewer&pwd=verysecure',
timeout: 5000,
outputBase: '/srv/www/publiccam/YYYY-MM-DD HH.mm.ss/',
outputFilename: '<camId>.avi'
}
};
module.exports = config;

58
index.js Normal file
View File

@ -0,0 +1,58 @@
var config = require('./config');
var capture = require('./capture');
var moment = require('moment');
var express = require('express');
var app = express();
// Some sanity checking to avoid issues later on
if (!config.cams)
config.cams = [];
app.get('/', function(req, res)
{
res.send(JSON.stringify(
{
status: 'running',
cams: Object.keys(config.cams)
}));
});
app.get('/capture', function(req, res)
{
var now = moment();
var cams = [];
for (var camId in config.cams)
{
cams.push(camId);
capture.start(camId, config.cams[camId], now);
}
res.send(JSON.stringify(cams));
});
app.get('/capture/:camId', function(req, res)
{
var camId = req.params.camId;
if (config.cams.hasOwnProperty(camId))
{
capture.start(config.cams[camId], moment());
res.send(JSON.stringify([camId]));
}
else
{
res.sendStatus(404);
}
});
app.listen(config.port, function()
{
console.log('SecurityCam.js running on port ' + config.port);
});

16
package.json Normal file
View File

@ -0,0 +1,16 @@
{
"name": "securitycam.js",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.14.0",
"fluent-ffmpeg": "^2.1.0",
"moment": "^2.14.1"
}
}

View File

@ -0,0 +1,8 @@
{
"folders":
[
{
"path": "."
}
]
}