mmaelzer/mjpeg-consumer

File created cannot be opened

eliothxchan opened this issue · 4 comments

Not sure if I'm filing an issue on a dead repo, but I've been trying to figure out a way to extract individual jpegs from an mjpeg stream at timed intervals, and I came across this library. Unfortunately, it doesn't work for me. Here's my code exactly; I simply replaced your IP/configuration with an open-access IP camera.

(function() {
    'use strict';
    var request = require("request");
    var MjpegConsumer = require("mjpeg-consumer");
    var FileOnWrite = require("file-on-write");

    var writer = new FileOnWrite({ 
        path: './video',
        ext: '.jpg'
    });
    var consumer = new MjpegConsumer();

    var options = {
        url: "http://192.168.1.1/videostream.cgi"
    };

    request(options).pipe(consumer).pipe(writer);
}());

The created file is damaged and can't be opened.

This is not a dead repo. That approach looks fine. One other thing you could try (that I don't believe will make much of a difference) is use mjpeg-camera since it wraps this functionality such that you only have to provide a configuration.

If that doesn't work, and you're open to sending data from your camera, another possibility would be to capture some raw HTTP data coming from the camera and paste it via a gist or inside a comment on this issue so I can take a look. Here's an (untested) approach to capturing raw camera http data that should work:

var request = require('request');
var fs = require('fs');

request({url: 'http://192.168.1.1/videostream.cgi'}).pipe(fs.createWriteStream('http_camera_stream', 'binary');

Edit: One question - what operating system and version of node.js are you using?

Unfortunately I'm doing this for a project at work so I'm unsure if giving any data directly related to the project would be a violation of my NDA, so I hope you'll understand about that.

I'm on Yosemite 10.10.5 and Node 0.10.40. I'll give mjpeg-camera a shot and get back to you.

Edit: I have however tried it with this open-access camera: http://lwsnb160-cam.cs.purdue.edu/axis-cgi/mjpg/video.cgi and I get the same issue

Yes, thank you, it seems to work with mjpeg-camera as opposed to mjpeg-consumer. I simply used your demo code, and modified some of the configurations. Here it is for reference. The only odd thing is that when I run

npm list

it tells me that both file-on-write and mjpeg-camera are extraneous. Other than that it works perfectly fine.

(function() {
    'use strict';

    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({
      url: 'http://lwsnb160-cam.cs.purdue.edu/axis-cgi/mjpg/video.cgi'
    });

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

    // Start streaming
    camera.start();

    // Stop recording after 10 seconds
    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);
      });

    }, 10*1000);
}());

Excellent! I'm glad that mjpeg-camera worked out for you.

I used the code you pasted into your initial comment and pointed it at that open Purdue camera. It had no issue writing images into the video folder. Unsure what the problem is/was with your initial approach.

I'm going to go ahead and close this issue, but if you find other problems, please let me know.

Here's a screenshot of the output using your mjpeg-consumer code with that Purdue camera:
screenshot 2015-09-15 19 42 02