npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mjpeg-camera

v2.0.1

Published

A full-featured mjpeg camera streaming library

Downloads

133

Readme

mjpeg-camera

A full-featured mjpeg camera streaming library.

Sometimes you want a slim library that does one thing well, like mjpeg-consumer.

Other times, you want the pieces put together for you so that you can work on more important things.

mjpeg-camera is for those other times.

Install

npm install mjpeg-camera

Stand-alone Server Example

$ npm install mjpeg-camera -g
$ cs

  Usage: server [options]

  Options:

    -h, --help          output usage information
    -V, --version       output the version number
    -u --user []        Set the username for camera authentication
    -pw --password []   Set the password for camera authentication
    -l --url []         Set the url for the camera
    -p --port [8080]    Set the port for the http server to listen on
    -n --name [camera]  Set the name of the camera


$ cs -l http://204.248.124.202/mjpg/video.mjpg?camera=1 -n "Spring Grove" -p 12345
=== Spring Grove camera server listening on 12345 ===

// open up http://localhost:12345 in your browser to view the live video stream

Libary Example

var MjpegCamera = require('mjpeg-camera');
var FileOnWrite = require('file-on-write');
var fs = require('fs');

// Create a writable stream to generate files
var fileWriter = new FileOnWrite({
  path: './frames',
  ext: '.jpeg',
  filename: function(frame) {
    return frame.name + '-' + frame.time;
  },
  transform: function(frame) {
    return frame.data;
  }
});

// Create an MjpegCamera instance
var camera = new MjpegCamera({
  name: 'backdoor',
  user: 'admin',
  password: 'wordup',
  url: 'http://192.168.7.1/video',
  motion: true
});

// Pipe frames to our fileWriter so we gather jpeg frames into the /frames folder
camera.pipe(fileWriter);

// Start streaming
camera.start();

// Stop recording after an hour
setTimeout(function() {

  // Stahp
  camera.stop();

  // Get one last frame
  // Will open a connection long enough to get a single frame and then
  // immediately close the connection
  camera.getScreenshot(function(err, frame) {
    fs.writeFile('final.jpeg', frame, process.exit);
  });

}, 60*60*1000);

Options

  • url String - The url of the mjpeg camera.
  • name String (optional) - The name of the camera. The name is used when passing along a frame as frames are objects of the format { name: <string>, time: <number>, data: <buffer> }. Defaults to 'camera' and a random number between 0 and 1000.
  • motion Boolean (optional) - A flag that tells the camera to only emit frames when motion is detected. mjpeg-camera uses the motion-detect library for motion detection. Defaults to false.
  • password String (optional) - The password required for authenticating with the camera.
  • user String (optional) - The username required for authenticating with the camera.
  • timeout Number (optional) - The time in milliseconds that must elapse since last receiving data from the camera before trying to reconnect to the camera.

Methods

camera.start()

Calling start will open the connection to the mjpeg camera and begin streaming jpeg images.

camera.stop()

Calling stop will close the connection the the mjpeg camera and unhook internal streams.

camera.getScreenshot(callback)

If called before start() or after stop(), getScreenshot will open a connection long enough to get a single frame from the camera, then close the connection. If getScreenshot is called while there is a connection to the camera, it will pass the latest frame to the callback

Example

var MjpegCamera = require('mjpeg-camera');
var fs = require('fs');

var camera = new MjpegCamera({ url: 'http://192.168.1.2/feed' });

camera.getScreenshot(function(err, frame) {
  fs.writeFileSync('camera-screenshot.jpg', frame);  
});

Streams

mjpeg-camera is a node.js Transform stream. You can pipe it to a file writer file file-on-write or addition frame analysis streams. When using the motion flag to only stream motion-detected frames.

The data format passed by the stream looks like this:

{
  name: 'camera-name',
  time: 1413063729650,
  data: Buffer < ... >
}

Example

var MjpegCamera = require('mjpeg-camera');
var fs = require('fs');
var FileOnWrite = require('file-on-write');

var cameraName = 'front-door';

var camera = new MjpegCamera({
  name: cameraName,
  url: 'http://192.168.1.2/feed',
  motion: true
});

var motionWriter = new FileOnWrite({
  path: 'motion/',
  filename: function(frame) {
    return frame.name + '-' + frame.time;
  },
  // We need to pull the jpeg out of the frame object
  transform: function(frame) {
    return frame.data;
  },
  ext: '.jpg'
});

// Write frames with motion to the 'motion/'' folder
camera.pipe(motionWriter);

The MIT License

Copyright (c) 2015 Michael Maelzer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.