diff --git a/videoio.cpp b/videoio.cpp index 2090fd59..927558ef 100644 --- a/videoio.cpp +++ b/videoio.cpp @@ -54,6 +54,14 @@ void VideoWriter_Open(VideoWriter vw, const char* name, const char* codec, doubl vw->open(name, codecCode, fps, cv::Size(width, height), isColor); } +void VideoWriter_OpenCap(VideoWriter vw, const char* name, int apiPreference, const char* codec, double fps, int width, + int height, bool isColor) { + int codecCode = cv::VideoWriter::fourcc(codec[0], codec[1], codec[2], codec[3]); + vw->open(name, apiPreference, codecCode, fps, cv::Size(width, height), isColor); +} + + + int VideoWriter_IsOpened(VideoWriter vw) { return vw->isOpened(); } diff --git a/videoio.go b/videoio.go index 4c92b645..20d489ae 100644 --- a/videoio.go +++ b/videoio.go @@ -152,6 +152,43 @@ const ( VideoCaptureAutoFocus VideoCaptureProperties = 39 ) +const ( + CAP_ANY = 0 + CAP_VFW = 200 + CAP_V4L = 200 + CAP_V4L2 = 200 + CAP_FIREWIRE = 300 + CAP_FIREWARE = 300 + CAP_IEEE1394 = 300 + CAP_DC1394 = 300 + CAP_CMU1394 = 300 + CAP_QT = 500 + CAP_UNICAP = 600 + CAP_DSHOW = 700 + CAP_PVAPI = 800 + CAP_OPENNI = 900 + CAP_OPENNI_ASUS = 910 + CAP_ANDROID = 1000 + CAP_XIAPI = 1100 + CAP_AVFOUNDATION = 1200 + CAP_GIGANETIX = 1300 + CAP_MSMF = 1400 + CAP_WINRT = 1410 + CAP_INTELPERC = 1500 + CAP_OPENNI2 = 1600 + CAP_OPENNI2_ASUS = 1610 + CAP_GPHOTO2 = 1700 + CAP_GSTREAMER = 1800 + CAP_FFMPEG = 1900 + CAP_IMAGES = 2000 + CAP_ARAVIS = 2100 + CAP_OPENCV_MJPEG = 2200 + CAP_INTEL_MFX = 2300 + CAP_XINE = 2400 +) + + + // VideoCapture is a wrapper around the OpenCV VideoCapture class. // // For further details, please see: @@ -284,6 +321,32 @@ func VideoWriterFile(name string, codec string, fps float64, width int, height i return } +func VideoWriterCap(name string, apiPreference int, codec string, fps float64, width int, height int, isColor bool) (vw *VideoWriter, err error) { + + if fps == 0 || width == 0 || height == 0 { + return nil, fmt.Errorf("one of the numerical parameters "+ + "is equal to zero: FPS: %f, width: %d, height: %d", fps, width, height) + } + + vw = &VideoWriter{ + p: C.VideoWriter_New(), + mu: &sync.RWMutex{}, + } + + cName := C.CString(name) + defer C.free(unsafe.Pointer(cName)) + + cCodec := C.CString(codec) + defer C.free(unsafe.Pointer(cCodec)) + + cApiPreference := C.int(apiPreference) + ////defer C.free(unsafe.Pointer(apiPreference)) + + C.VideoWriter_OpenCap(vw.p, cName, cApiPreference, cCodec, C.double(fps), C.int(width), C.int(height), C.bool(isColor)) + + return +} + // Close VideoWriter object. func (vw *VideoWriter) Close() error { C.VideoWriter_Close(vw.p) diff --git a/videoio.h b/videoio.h index b779fd9e..5121fef1 100644 --- a/videoio.h +++ b/videoio.h @@ -32,6 +32,8 @@ VideoWriter VideoWriter_New(); void VideoWriter_Close(VideoWriter vw); void VideoWriter_Open(VideoWriter vw, const char* name, const char* codec, double fps, int width, int height, bool isColor); +void VideoWriter_OpenCap(VideoWriter vw, const char* name, int apiPreference, const char* codec, double fps, int width, + int height, bool isColor); int VideoWriter_IsOpened(VideoWriter vw); void VideoWriter_Write(VideoWriter vw, Mat img); diff --git a/videoio_test.go b/videoio_test.go index 4292f5b0..4b313311 100644 --- a/videoio_test.go +++ b/videoio_test.go @@ -4,6 +4,7 @@ import ( "io/ioutil" "path/filepath" "strings" + "fmt" "testing" ) @@ -121,3 +122,49 @@ func TestVideoWriterFile(t *testing.T) { t.Error("Invalid Write() in VideoWriter") } } + +func TestVideoWriterCap(t *testing.T) { + dir, _ := ioutil.TempDir("", "gocvtests") + tmpfn := filepath.Join(dir, "test.mp4") + + in_pipeline := fmt.Sprintf("videotestsrc ! video/x-raw,framerate=20/1 ! videoscale ! videoconvert ! appsink") + vr, in_err := OpenVideoCapture(in_pipeline) + + if in_err != nil { + t.Error(in_err) + } + + img := NewMat() + defer img.Close() + _ = vr.Read(&img) + + _ = vr + + out_pipeline := fmt.Sprintf("appsrc ! videoconvert ! x264enc ! mp4mux ! filesink location=%s", tmpfn) + + vw, out_err := VideoWriterCap(out_pipeline, CAP_GSTREAMER, "avc1", 20, img.Cols(), img.Rows(), true) + + if out_err != nil { + t.Fatal(in_err) + } + defer vw.Close() + + if !vw.IsOpened() { + t.Error("Unable to open VideoWriterFile") + } + + i := 0 + for i < 100 { + + img := NewMat() + defer img.Close() + + _ = vr.Read(&img) + + err := vw.Write(img) + if err != nil { + t.Error("Invalid Write() in VideoWriter") + } + i++ + } +}