
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
motion-detect
Advanced tools
A node.js motion detection library that supports node.js streams.
Using motion streams requires ImageMagick CLI tools to be installed. There's plenty of ways to install ImageMagick, choose what's right for you.
npm install motion-detect
Write images of any format to a motion stream, and the motion stream will emit, if motion is detected, objects of the format { time: 1394600350750, data: <Buffer ...> }
where time
is the moment the motion stream received the frame.
var request = require("request");
var MjpegConsumer = require("mjpeg-consumer");
var MotionStream = require("motion-detect").Stream;
var FileOnWrite = require("file-on-write");
var writer = new FileOnWrite({
path: './video',
ext: '.jpg',
filename: function(image) {
return image.time;
},
transform: function(image) {
return image.data;
},
sync: true
});
var consumer = new MjpegConsumer();
var motion = new MotionStream();
var username = "admin";
var password = "password";
var options = {
url: "http://192.168.1.5/videostream.cgi",
headers: {
'Authorization': 'Basic ' + new Buffer(username + ':' + password).toString('base64')
}
};
request(options).pipe(consumer).pipe(motion).pipe(writer);
minimumMotion: Number : default 2
: The minimum number of seconds of motion required before emitting data
prebuffer: Number : default 4
: The motion stream will cache and emit the prebuffer number of seconds of images prior to motion occurring.
postbuffer: Number : default 4
: The motion stream will emit the postbuffer number of seconds of images after motion occurs.
var Motion = require('motion-detect').Motion;
var motion = new Motion();
var hasMotion = motion.detect(image1, image2);
Array
of Number
Array
of Number
Detect is called with one or two flat arrays of RGBA values. If called with one parameter, detect will use the last image1
as image2
.
var Motion = require('motion-detect').Motion;
var motion = new Motion();
// img1, img2, img3, img4 created ...
// all img's are strings of RGBA values
// and look like [128,128,128,255,...]
var hasMotion;
hasMotion = motion.detect(img1);
console.log(hasMotion);
// > false
hasMotion = motion.detect(img2);
console.log(hasMotion);
// > true
hasMotion = motion.detect(img3, img4);
console.log(hasMotion);
// > false
Returns the last image.
var motion = new Motion();
console.log(motion.detect(img1));
// > false
console.log(motion.detect(img2));
// > true
console.log(img2 === motion.getLastImage());
// > true
Array
of Number
Array
of Number
Returns an image with detected motion as white pixels on a black background in the form of a flat array of RGBA numbers.
Inspiration for this library comes from Romuald Quantin's excellent write up on motion detection in Javascript.
FAQs
A node.js motion detection library that supports node.js streams
The npm package motion-detect receives a total of 22 weekly downloads. As such, motion-detect popularity was classified as not popular.
We found that motion-detect demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.