Skip to content

Commit

Permalink
Ability to select the desired VideoWriter output codec on Open()
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Oct 8, 2017
1 parent 7389571 commit 927cf4c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/savevideo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func main() {
return
}

writer, err := gocv.VideoWriterFile(saveFile, 25, img.Cols(), img.Rows())
writer, err := gocv.VideoWriterFile(saveFile, "MJPG", 25, img.Cols(), img.Rows())
if err != nil {
fmt.Printf("error opening video writer device: %v\n", saveFile)
return
Expand Down
5 changes: 3 additions & 2 deletions videoio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ void VideoWriter_Close(VideoWriter vw) {
delete vw;
}

void VideoWriter_Open(VideoWriter vw, const char* name, double fps, int width,
void VideoWriter_Open(VideoWriter vw, const char* name, const char* codec, double fps, int width,
int height) {
vw->open(name, CV_FOURCC('M', 'J', 'P', 'G'), fps, cv::Size(width, height), true);
int codecCode = cv::VideoWriter::fourcc(codec[0], codec[1], codec[2], codec[3]);
vw->open(name, codecCode, fps, cv::Size(width, height), true);
}

int VideoWriter_IsOpened(VideoWriter vw) {
Expand Down
14 changes: 12 additions & 2 deletions videoio.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,25 @@ type VideoWriter struct {
}

// VideoWriterFile opens a VideoWriter with a specific output file.
func VideoWriterFile(name string, fps float64, width int, height int) (vw *VideoWriter, err error) {
// The "codec" param should be the four-letter code for the desired output
// codec, for example "MJPG".
//
// For further details, please see:
// http://docs.opencv.org/3.3.0/dd/d9e/classcv_1_1VideoWriter.html#a0901c353cd5ea05bba455317dab81130
//
func VideoWriterFile(name string, codec string, fps float64, width int, height int) (vw *VideoWriter, err error) {
vw = &VideoWriter{
p: C.VideoWriter_New(),
mu: &sync.RWMutex{},
}

cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
C.VideoWriter_Open(vw.p, cName, C.double(fps), C.int(width), C.int(height))

cCodec := C.CString(codec)
defer C.free(unsafe.Pointer(cCodec))

C.VideoWriter_Open(vw.p, cName, cCodec, C.double(fps), C.int(width), C.int(height))
return
}

Expand Down
2 changes: 1 addition & 1 deletion videoio.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void VideoCapture_Grab(VideoCapture v, int skip);
// VideoWriter
VideoWriter VideoWriter_New();
void VideoWriter_Close(VideoWriter vw);
void VideoWriter_Open(VideoWriter vw, const char* name, double fps, int width,
void VideoWriter_Open(VideoWriter vw, const char* name, const char* codec, double fps, int width,
int height);
int VideoWriter_IsOpened(VideoWriter vw);
void VideoWriter_Write(VideoWriter vw, Mat img);
Expand Down
2 changes: 1 addition & 1 deletion videoio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestVideoWriterFile(t *testing.T) {
}
defer img.Close()

vw, _ := VideoWriterFile("/tmp/test.avi", 25, img.Cols(), img.Rows())
vw, _ := VideoWriterFile("/tmp/test.avi", "MJPG", 25, img.Cols(), img.Rows())
defer vw.Close()

if !vw.IsOpened() {
Expand Down

0 comments on commit 927cf4c

Please sign in to comment.