Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose apiPreference in VideoWriter #718

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions videoio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
63 changes: 63 additions & 0 deletions videoio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions videoio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
47 changes: 47 additions & 0 deletions videoio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"path/filepath"
"strings"
"fmt"
"testing"
)

Expand Down Expand Up @@ -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++
}
}