From 088cc0931f29cdb3881091beda9a646d7d251171 Mon Sep 17 00:00:00 2001 From: Steve McDaniel Date: Wed, 29 Jul 2020 21:17:27 -0400 Subject: [PATCH 1/3] Added support for passing apiPreference. --- videoio.cpp | 8 +++++++ videoio.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ videoio.h | 2 ++ videoio_test.go | 50 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+) 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 13f7620f..a33218fb 100644 --- a/videoio.go +++ b/videoio.go @@ -152,6 +152,43 @@ const ( VideoCaptureAutoFocus = 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..599f94b2 100644 --- a/videoio_test.go +++ b/videoio_test.go @@ -4,7 +4,9 @@ import ( "io/ioutil" "path/filepath" "strings" + "fmt" "testing" + "log" ) func TestVideoCaptureEmptyNumericalParameters(t *testing.T) { @@ -121,3 +123,51 @@ 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 { + log.Println(in_err) + } + + log.Println("Opened input") + img := NewMat() + defer img.Close() + _ = vr.Read(&img) + + _ = vr + + out_pipeline := fmt.Sprintf("appsrc ! videoconvert ! x264enc ! filesink location=%s", tmpfn) + + vw, out_err := VideoWriterCap(out_pipeline, CAP_GSTREAMER, "avc1", 20, img.Cols(), img.Rows(), true) + if out_err != nil { + log.Println(in_err) + } + defer vw.Close() + log.Println("Opened output") + + if !vw.IsOpened() { + t.Error("Unable to open VideoWriterFile") + } + + i := 0 + for i < 30 { + + img := NewMat() + defer img.Close() + + _ = vr.Read(&img) + + err := vw.Write(img) + if err != nil { + t.Error("Invalid Write() in VideoWriter") + } + i++ + } + +} From f411b67a3f4e2b19e168bed53e0560a481b7bbf5 Mon Sep 17 00:00:00 2001 From: Steve McDaniel Date: Wed, 29 Jul 2020 21:23:35 -0400 Subject: [PATCH 2/3] removed debug prints --- videoio_test.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/videoio_test.go b/videoio_test.go index 599f94b2..39c0294b 100644 --- a/videoio_test.go +++ b/videoio_test.go @@ -6,7 +6,6 @@ import ( "strings" "fmt" "testing" - "log" ) func TestVideoCaptureEmptyNumericalParameters(t *testing.T) { @@ -132,10 +131,9 @@ func TestVideoWriterCap(t *testing.T) { vr, in_err := OpenVideoCapture(in_pipeline) if in_err != nil { - log.Println(in_err) + t.Error(in_err) } - log.Println("Opened input") img := NewMat() defer img.Close() _ = vr.Read(&img) @@ -145,11 +143,11 @@ func TestVideoWriterCap(t *testing.T) { out_pipeline := fmt.Sprintf("appsrc ! videoconvert ! x264enc ! filesink location=%s", tmpfn) vw, out_err := VideoWriterCap(out_pipeline, CAP_GSTREAMER, "avc1", 20, img.Cols(), img.Rows(), true) + if out_err != nil { - log.Println(in_err) + t.Fatal(in_err) } defer vw.Close() - log.Println("Opened output") if !vw.IsOpened() { t.Error("Unable to open VideoWriterFile") From 3e5cffbee63efd92d217410d17955ee6caa6a8a1 Mon Sep 17 00:00:00 2001 From: Steve McDaniel Date: Sun, 2 Aug 2020 21:59:56 -0400 Subject: [PATCH 3/3] added mp4mux to output --- videoio_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/videoio_test.go b/videoio_test.go index 39c0294b..4b313311 100644 --- a/videoio_test.go +++ b/videoio_test.go @@ -140,7 +140,7 @@ func TestVideoWriterCap(t *testing.T) { _ = vr - out_pipeline := fmt.Sprintf("appsrc ! videoconvert ! x264enc ! filesink location=%s", tmpfn) + 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) @@ -154,7 +154,7 @@ func TestVideoWriterCap(t *testing.T) { } i := 0 - for i < 30 { + for i < 100 { img := NewMat() defer img.Close() @@ -167,5 +167,4 @@ func TestVideoWriterCap(t *testing.T) { } i++ } - }