diff --git a/binding.gyp b/binding.gyp index dd37c53..5207c7c 100755 --- a/binding.gyp +++ b/binding.gyp @@ -9,6 +9,7 @@ , "src/Contours.cc" , "src/Point.cc" , "src/VideoCaptureWrap.cc" + , "src/VideoWriter.cc" , "src/CamShift.cc" , "src/HighGUI.cc" ] diff --git a/lib/opencv.js b/lib/opencv.js index 949137c..cd4d46c 100755 --- a/lib/opencv.js +++ b/lib/opencv.js @@ -129,10 +129,14 @@ videostream.read = function(){ var frame = function(){ self.video.read(function(mat){ - self.emit('data', mat) - if (!this.paused){ - process.nextTick(frame) - } + if (mat.width() && mat.height()){ + self.emit('data', mat) + if (!this.paused){ + process.nextTick(frame) + } + } else { + self.emit('end') + } }) } diff --git a/src/VideoWriter.cc b/src/VideoWriter.cc new file mode 100644 index 0000000..f92f067 --- /dev/null +++ b/src/VideoWriter.cc @@ -0,0 +1,77 @@ +#include "VideoWriter.h" +#include "Matrix.h" +#include "OpenCV.h" + +v8::Persistent VideoWriterWrap::constructor; + +void +VideoWriterWrap::Init(Handle target) { + HandleScope scope; + + // Constructor + constructor = Persistent::New( + FunctionTemplate::New(VideoWriterWrap::New)); + constructor->InstanceTemplate()->SetInternalFieldCount(1); + constructor->SetClassName(String::NewSymbol("VideoWriter")); + + // Prototype + //Local proto = constructor->PrototypeTemplate(); + + NODE_SET_PROTOTYPE_METHOD(constructor, "write", Write); + + target->Set(String::NewSymbol("VideoWriter"), constructor->GetFunction()); +}; + + + +Handle +VideoWriterWrap::New(const Arguments &args) { + HandleScope scope; + + if (args.This()->InternalFieldCount() == 0) + return v8::ThrowException( + v8::Exception::TypeError(v8::String::New("Cannot Instantiate without new"))); + + VideoWriterWrap *v; + std::string filename; + int codec = CV_FOURCC('M','J','P','G'); // Default mjpg + double fps = 24.0; + cv::Size framesize = cv::Size(640, 320); + //bool isColor; // TODO + + filename = std::string(*v8::String::AsciiValue(args[0]->ToString())); + + + + v = new VideoWriterWrap(filename, codec, fps, framesize); + v->Wrap(args.This()); + + return args.This(); +} + + + +VideoWriterWrap::VideoWriterWrap(const std::string& filename, int codec, + double fps, cv::Size framesize){ + + writer = cv::VideoWriter(filename, codec, fps, framesize); + + + if (!writer.isOpened()){ + std::cout << "VideoWriter: Could not be opened!" + } + +} + + +Handle +VideoWriterWrap::Write(const Arguments &args) { + SETUP_FUNCTION(VideoWriterWrap) + + Matrix *im = ObjectWrap::Unwrap(args[0]->ToObject()); + + self->writer << im->mat; + + return scope.Close(v8::Undefined()); +} + diff --git a/src/VideoWriter.h b/src/VideoWriter.h new file mode 100644 index 0000000..1cd19d3 --- /dev/null +++ b/src/VideoWriter.h @@ -0,0 +1,15 @@ +#include "OpenCV.h" + +class VideoWriterWrap: public node::ObjectWrap { + public: + cv::VideoWriter writer; + + static Persistent constructor; + static void Init(Handle target); + static Handle New(const Arguments &args); + + VideoWriterWrap(const std::string& filename, int codec, + double fps, cv::Size framesize); + + static Handle Write(const v8::Arguments&); +}; diff --git a/src/init.cc b/src/init.cc index 063d984..3593531 100755 --- a/src/init.cc +++ b/src/init.cc @@ -3,6 +3,7 @@ #include "Matrix.h" #include "CascadeClassifierWrap.h" #include "VideoCaptureWrap.h" +#include "VideoWriter.h" #include "Contours.h" #include "CamShift.h" #include "HighGUI.h" @@ -16,6 +17,7 @@ init(Handle target) { Matrix::Init(target); CascadeClassifierWrap::Init(target); VideoCaptureWrap::Init(target); + VideoWriterWrap::Init(target); Contour::Init(target); TrackedObject::Init(target);