Skip to content

Commit 0716120

Browse files
committed
videoio: add RetrieveChannel function to make it possible to capture both video and audio
Signed-off-by: deadprogram <[email protected]>
1 parent 3e3223b commit 0716120

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

videoio.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ int VideoCapture_Retrieve(VideoCapture v, Mat buf) {
144144
}
145145
}
146146

147+
int VideoCapture_RetrieveChannel(VideoCapture v, Mat buf, int channel) {
148+
try {
149+
return v->retrieve(*buf, channel);
150+
} catch(const cv::Exception& e){
151+
setExceptionInfo(e.code, e.what());
152+
return 0;
153+
}
154+
}
155+
147156
// VideoWriter
148157
VideoWriter VideoWriter_New() {
149158
try {

videoio.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,19 @@ func (v *VideoCapture) Grab(skip int) error {
474474
// Retrieve decodes and returns the grabbed video frame. Should be used after Grab
475475
//
476476
// For further details, please see:
477-
// http://docs.opencv.org/master/d8/dfe/classcv_1_1VideoCapture.html#a9ac7f4b1cdfe624663478568486e6712
477+
// https://docs.opencv.org/4.x/d8/dfe/classcv_1_1VideoCapture.html#a9ac7f4b1cdfe624663478568486e6712
478478
func (v *VideoCapture) Retrieve(m *Mat) bool {
479479
return C.VideoCapture_Retrieve(v.p, m.p) != 0
480480
}
481481

482+
// Retrieve decodes and returns the grabbed frame for a specific channel. Should be used after Grab
483+
//
484+
// For further details, please see:
485+
// https://docs.opencv.org/4.x/d8/dfe/classcv_1_1VideoCapture.html#a9ac7f4b1cdfe624663478568486e6712
486+
func (v *VideoCapture) RetrieveChannel(m *Mat, channel int) bool {
487+
return C.VideoCapture_RetrieveChannel(v.p, m.p, C.int(channel)) != 0
488+
}
489+
482490
// CodecString returns a string representation of FourCC bytes, i.e. the name of a codec
483491
func (v *VideoCapture) CodecString() string {
484492
res := ""

videoio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ int VideoCapture_IsOpened(VideoCapture v);
3232
int VideoCapture_Read(VideoCapture v, Mat buf);
3333
OpenCVResult VideoCapture_Grab(VideoCapture v, int skip);
3434
int VideoCapture_Retrieve(VideoCapture v, Mat buf);
35+
int VideoCapture_RetrieveChannel(VideoCapture v, Mat buf, int channel);
3536

3637
// VideoWriter
3738
VideoWriter VideoWriter_New();

videoio_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,40 @@ func TestVideoCaptureFile_GrabRetrieve(t *testing.T) {
296296
}
297297
}
298298

299+
func TestVideoCaptureFile_GrabRetrieveChannel(t *testing.T) {
300+
vc, err := VideoCaptureFile("images/small.mp4")
301+
defer vc.Close()
302+
303+
if err != nil {
304+
t.Errorf("%s", err)
305+
}
306+
307+
if !vc.IsOpened() {
308+
t.Error("Unable to open VideoCaptureFile")
309+
}
310+
311+
if fw := vc.Get(VideoCaptureFrameWidth); int(fw) != 560 {
312+
t.Errorf("Expected frame width property of 560.0 got %f", fw)
313+
}
314+
if fh := vc.Get(VideoCaptureFrameHeight); int(fh) != 320 {
315+
t.Errorf("Expected frame height property of 320.0 got %f", fh)
316+
}
317+
318+
vc.Set(VideoCaptureBrightness, 100.0)
319+
320+
vc.Grab(10)
321+
322+
img := NewMat()
323+
defer img.Close()
324+
325+
if ok := vc.RetrieveChannel(&img, 0); !ok {
326+
t.Error("Unable to read VideoCaptureFile")
327+
}
328+
if img.Empty() {
329+
t.Error("Unable to read VideoCaptureFile")
330+
}
331+
}
332+
299333
func TestVideoRegistry(t *testing.T) {
300334

301335
name := VideoRegistry.GetBackendName(VideoCaptureFFmpeg)

0 commit comments

Comments
 (0)