Skip to content

VideoCapture

videoioclassOpenCV 5.0.0
import { VideoCapture } from '@banou/opencv-wasm'

Use after await initOpenCV(). See the initialization and named imports guide.

ARGUMENTSConstructor or factory
CLASSVideoCapture
RETURN TYPEOwned native handle
Call structure. A void return can still write to destination arguments. The parameter descriptions define inputs, outputs and ownership.

Native object: release it with using or delete(). Factories can return null; check before calling methods.

endcond: IGNORED Class for video capturing from video files, image sequences or cameras.

The class provides C++ API for capturing video from cameras or for reading video files and image sequences.

Here is how the class can be used:

Note: - (C++) A basic sample on using the %VideoCapture interface can be found at OPENCV_SOURCE_CODE/samples/cpp/videocapture_combined.cpp

  • (Python) A basic sample on using the %VideoCapture interface can be found at OPENCV_SOURCE_CODE/samples/python/video.py
  • (Python) A multi threaded video processing sample can be found at OPENCV_SOURCE_CODE/samples/python/video_threaded.py
  • (Python) %VideoCapture sample showcasing some features of the Video4Linux2 backend OPENCV_SOURCE_CODE/samples/python/video_v4l2.py

Constructors and members

static new

Opens a video file or a capturing device or an IP video stream for video capturing with API Preference and parameters

The `params` parameter allows to specify extra parameters encoded as pairs `(paramId_1, paramValue_1, paramId_2, paramValue_2, ...)`.
See cv::VideoCaptureProperties
new(filename: EmbindString, apiPreference: number, params: IntVector): VideoCapture;
4 available overloads
new(): VideoCapture;
new(_0: EmbindString): VideoCapture;
new(filename: EmbindString, apiPreference: number): VideoCapture;
new(filename: EmbindString, apiPreference: number, params: IntVector): VideoCapture;
_0

0 argument (EmbindString).

filename

it can be:

  • name of video file (eg. video.avi)
  • or image sequence (eg. img_%02d.jpg, which will read samples like img_00.jpg, img_01.jpg, img_02.jpg, ...)
  • or URL of video stream (eg. protocol://host:port/script_name?script_params|auth)
  • or GStreamer pipeline string in gst-launch tool format in case if GStreamer is used as backend Note that each video stream or IP camera feed has its own URL scheme. Please refer to the documentation of source stream to know the right URL.
apiPreference

preferred Capture API backends to use. Can be used to enforce a specific reader implementation if multiple are available: e.g. cv::CAP_FFMPEG or cv::CAP_IMAGES or cv::CAP_DSHOW.

params

params argument (IntVector).

Returns

The VideoCapture result.

static from3

Create an owned VideoCapture instance with the supplied configuration.

endcond: IGNORED Class for video capturing from video files, image sequences or cameras.

from3(_0: number, _1: number): VideoCapture | null;
2 available overloads
from3(_0: number): VideoCapture | null;
from3(_0: number, _1: number): VideoCapture | null;
_0

0 argument (number).

_1

1 argument (number).

Returns

The VideoCapture | null result.

static from4

Create an owned VideoCapture instance with the supplied configuration.

endcond: IGNORED Class for video capturing from video files, image sequences or cameras.

from4(_0: number, _1: number, _2: IntVector): VideoCapture | null;
_0

0 argument (number).

_1

1 argument (number).

_2

2 argument (IntVector).

Returns

The VideoCapture | null result.

static from5

Create an owned VideoCapture instance with the supplied configuration.

endcond: IGNORED Class for video capturing from video files, image sequences or cameras.

from5(_0: IStreamReader | null, _1: number, _2: IntVector): VideoCapture | null;
_0

0 argument (IStreamReader | null).

_1

1 argument (number).

_2

2 argument (IntVector).

Returns

The VideoCapture | null result.

static waitAny

Wait for ready frames from VideoCapture.

throws: Exception %Exception on stream errors (check .isOpened() to filter out malformed streams) or VideoCapture type is not supported

The primary use of the function is in multi-camera environments.
The method fills the ready state vector, grabs video frame, if camera is ready.

After this call use VideoCapture::retrieve() to decode and fetch frame data.
waitAny(streams: VideoCaptureVector, readyIndex: IntVector, timeoutNs: bigint): boolean;
2 available overloads
waitAny(streams: VideoCaptureVector, readyIndex: IntVector): boolean;
waitAny(streams: VideoCaptureVector, readyIndex: IntVector, timeoutNs: bigint): boolean;
streams

input video streams

readyIndex

Output destination, filled by the native operation. stream indexes with grabbed frames (ready to use .retrieve() to fetch actual frame)

timeoutNs

number of nanoseconds (0 - infinite)

Returns

true if streamReady is not empty

clone

Create another handle to the same native object. This retains the object without copying its pixels or algorithm state; dispose both handles separately.

clone(): this;
Returns

The this result.

open

Opens a video file or a capturing device or an IP video stream for video capturing.

Parameters are same as the constructor VideoCapture(const String& filename, int apiPreference = CAP_ANY)
open(filename: EmbindString, apiPreference: number): boolean;
2 available overloads
open(filename: EmbindString): boolean;
open(filename: EmbindString, apiPreference: number): boolean;
filename

filename argument (EmbindString).

apiPreference

api preference argument (number).

Returns

true if the file has been successfully opened

The method first calls VideoCapture::release to close the already opened file or camera.

open1

Opens a video file or a capturing device or an IP video stream for video capturing with API Preference and parameters

The `params` parameter allows to specify extra parameters encoded as pairs `(paramId_1, paramValue_1, paramId_2, paramValue_2, ...)`.
See cv::VideoCaptureProperties
open1(filename: EmbindString, apiPreference: number, params: IntVector): boolean;
filename

filename argument (EmbindString).

apiPreference

api preference argument (number).

params

params argument (IntVector).

Returns

true if the file has been successfully opened

The method first calls VideoCapture::release to close the already opened file or camera.

open2

Opens a camera for video capturing

Parameters are same as the constructor VideoCapture(int index, int apiPreference = CAP_ANY)
open2(index: number, apiPreference: number): boolean;
2 available overloads
open2(index: number): boolean;
open2(index: number, apiPreference: number): boolean;
index

index argument (number).

apiPreference

api preference argument (number).

Returns

true if the camera has been successfully opened.

The method first calls VideoCapture::release to close the already opened file or camera.

open3

Opens a camera for video capturing with API Preference and parameters

The `params` parameter allows to specify extra parameters encoded as pairs `(paramId_1, paramValue_1, paramId_2, paramValue_2, ...)`.
See cv::VideoCaptureProperties
open3(index: number, apiPreference: number, params: IntVector): boolean;
index

index argument (number).

apiPreference

api preference argument (number).

params

params argument (IntVector).

Returns

true if the camera has been successfully opened.

The method first calls VideoCapture::release to close the already opened file or camera.

open4

Opens a video using data stream.

The `params` parameter allows to specify extra parameters encoded as pairs `(paramId_1, paramValue_1, paramId_2, paramValue_2, ...)`.
See cv::VideoCaptureProperties
open4(source: IStreamReader | null, apiPreference: number, params: IntVector): boolean;
source

source argument (IStreamReader | null).

apiPreference

api preference argument (number).

params

params argument (IntVector).

Returns

true if the file has been successfully opened

The method first calls VideoCapture::release to close the already opened file or camera.

isOpened

Returns true if video capturing has been initialized already.

If the previous call to VideoCapture constructor or VideoCapture::open() succeeded, the method returns
true.
isOpened(): boolean;
Returns

The boolean result.

release

Closes video file or capturing device.

The method is automatically called by subsequent VideoCapture::open and by VideoCapture
destructor.

The C function also deallocates memory and clears \*capture pointer.
release(): void;

grab

Grabs the next frame from video file or capturing device.

grab(): boolean;
Returns

true (non-zero) in the case of success.

The method/function grabs the next frame from video file or camera and returns true (non-zero) in the case of success.

The primary use of the function is in multi-camera environments, especially when the cameras do not have hardware synchronization. That is, you call VideoCapture::grab() for each camera and after that call the slower method VideoCapture::retrieve() to decode and get frame from each camera. This way the overhead on demosaicing or motion jpeg decompression etc. is eliminated and the retrieved frames from different cameras will be closer in time.

Also, when a connected camera is multi-head (for example, a stereo camera or a Kinect device), the correct way of retrieving data from it is to call VideoCapture::grab() first and then call VideoCapture::retrieve() one or more times with different values of the channel parameter.

tutorial_kinect_openni

retrieve

Decodes and returns the grabbed video frame.

See: read()
retrieve(image: Mat, flag: number): boolean;
2 available overloads
retrieve(image: Mat): boolean;
retrieve(image: Mat, flag: number): boolean;
image

Output destination, filled by the native operation. the video frame is returned here. If no frames has been grabbed the image will be empty.

flag

it could be a frame index or a driver specific flag

Returns

false if no frames has been grabbed

The method decodes and returns the just grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the method returns false and the function returns an empty image (with %cv::Mat, test it with Mat::empty()).

read

Grabs, decodes and returns the next video frame.

read(image: Mat): boolean;
image

Output destination, filled by the native operation. the video frame is returned here. If no frames has been grabbed the image will be empty.

Returns

false if no frames has been grabbed

The method/function combines VideoCapture::grab() and VideoCapture::retrieve() in one call. This is the most convenient method for reading video files or capturing data from decode and returns the just grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the method returns false and the function returns empty image (with %cv::Mat, test it with Mat::empty()).

set

Sets a property in the VideoCapture.

Note: Even if it returns true this doesn't ensure that the property value has been accepted by the capture device. See note in VideoCapture::get()

set(propId: number, value: number): boolean;
propId

Property identifier from cv::VideoCaptureProperties (eg. cv::CAP_PROP_POS_MSEC, cv::CAP_PROP_POS_FRAMES, ...) or one from videoio_flags_others

value

Value of the property.

Returns

true if the property is supported by backend used by the VideoCapture instance.

get

Returns the specified VideoCapture property

Note: Reading / writing properties involves many layers. Some unexpected result might happens along this chain.

    VideoCapture -> API Backend -> Operating System -> Device Driver -> Device Hardware
    
The returned value might be different from what really used by the device or it could be encoded
using device dependent rules (eg. steps or percentage). Effective behaviour depends from device
driver and API Backend
get(propId: number): number;
propId

Property identifier from cv::VideoCaptureProperties (eg. cv::CAP_PROP_POS_MSEC, cv::CAP_PROP_POS_FRAMES, ...) or one from videoio_flags_others

Returns

Value for the specified property. Value cv::CAP_PROP_UNKNOWN is returned when querying a property that is not supported by the backend used by the VideoCapture instance.

getBackendName

Returns used backend API name

Note: Stream should be opened.

getBackendName(): string;
Returns

The string result.

setExceptionMode

Switches exceptions mode

methods raise exceptions if not successful instead of returning an error code

setExceptionMode(enable: boolean): void;
enable

enable argument (boolean).

getExceptionMode

query if exception mode is active

getExceptionMode(): boolean;
Returns

The boolean result.

These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.