Skip to content

VideoWriter

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

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

ARGUMENTSConstructor or factory
CLASSVideoWriter
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.

example: samples/cpp/tutorial_code/videoio/video-write/video-write.cpp Check tutorial_video_write "the corresponding tutorial" for more details example: samples/cpp/videowriter.cpp An example using VideoCapture and VideoWriter class Video writer class.

The class provides C++ API for writing video files or image sequences.

Constructors and members

static new

Default constructors

The constructors/functions initialize video writers.
-   On Linux FFMPEG is used to write videos;
-   On Windows FFMPEG or MSWF or DSHOW is used;
-   On MacOSX AVFoundation is used.
new(filename: EmbindString, apiPreference: number, fourcc: number, fps: number, frameSize: Size, isColor: boolean): VideoWriter;
4 available overloads
new(): VideoWriter;
new(filename: EmbindString, fourcc: number, fps: number, frameSize: Size): VideoWriter;
new(filename: EmbindString, fourcc: number, fps: number, frameSize: Size, isColor: boolean): VideoWriter;
new(filename: EmbindString, apiPreference: number, fourcc: number, fps: number, frameSize: Size, isColor: boolean): VideoWriter;
filename

Name of the output video file.

fourcc

4-character code of codec used to compress the frames. For example, VideoWriter::fourcc('P','I','M','1') is a MPEG-1 codec, VideoWriter::fourcc('M','J','P','G') is a motion-jpeg codec etc. List of codes can be obtained at MSDN page or with this page of the fourcc site for a more complete list). FFMPEG backend with MP4 container natively uses other values as fourcc code: see ObjectType, so you may receive a warning message from OpenCV about fourcc code conversion.

fps

Framerate of the created video stream.

frameSize

Size of the video frames.

isColor

If it is not zero, the encoder will expect and encode color frames, otherwise it will work with grayscale frames.

Tips:

  • With some backends fourcc=-1 pops up the codec selection dialog from the system.
  • To save image sequence use a proper filename (eg. img_%02d.jpg) and fourcc=0 OR fps=0. Use uncompressed image format (eg. img_%02d.BMP) to save raw frames.
  • Most codecs are lossy. If you want lossless video file you need to use a lossless codecs (eg. FFMPEG FFV1, Huffman HFYU, Lagarith LAGS, etc...)
  • If FFMPEG is enabled, using codec=0; fps=0; you can create an uncompressed (raw) video file.
  • If FFMPEG is used, we allow frames of odd width or height, but in this case we truncate the rightmost column/the bottom row. Probably, this should be handled more elegantly, but some internal functions inside FFMPEG swscale require even width/height.
apiPreference

api preference argument (number).

Returns

The VideoWriter result.

static from2

Create an owned VideoWriter instance with the supplied configuration.

example: samples/cpp/tutorial_code/videoio/video-write/video-write.cpp Check tutorial_video_write "the corresponding tutorial" for more details example: samples/cpp/videowriter.cpp An example using VideoCapture and VideoWriter class Video writer class.

from2(_0: EmbindString, _1: number, _2: number, _3: number, _4: Size): VideoWriter | null;
_0

0 argument (EmbindString).

_1

1 argument (number).

_2

2 argument (number).

_3

3 argument (number).

_4

4 argument (Size).

Returns

The VideoWriter | null result.

static from3

Create an owned VideoWriter instance with the supplied configuration.

example: samples/cpp/tutorial_code/videoio/video-write/video-write.cpp Check tutorial_video_write "the corresponding tutorial" for more details example: samples/cpp/videowriter.cpp An example using VideoCapture and VideoWriter class Video writer class.

from3(_0: EmbindString, _1: number, _2: number, _3: Size, _4: IntVector): VideoWriter | null;
_0

0 argument (EmbindString).

_1

1 argument (number).

_2

2 argument (number).

_3

3 argument (Size).

_4

4 argument (IntVector).

Returns

The VideoWriter | null result.

static from4

Create an owned VideoWriter instance with the supplied configuration.

example: samples/cpp/tutorial_code/videoio/video-write/video-write.cpp Check tutorial_video_write "the corresponding tutorial" for more details example: samples/cpp/videowriter.cpp An example using VideoCapture and VideoWriter class Video writer class.

from4(_0: EmbindString, _1: number, _2: number, _3: number, _4: Size, _5: IntVector): VideoWriter | null;
_0

0 argument (EmbindString).

_1

1 argument (number).

_2

2 argument (number).

_3

3 argument (number).

_4

4 argument (Size).

_5

5 argument (IntVector).

Returns

The VideoWriter | null result.

static fourcc

Concatenates 4 chars to a fourcc code

fourcc(c1: number, c2: number, c3: number, c4: number): number;
c1

c1 argument (number).

c2

c2 argument (number).

c3

c3 argument (number).

c4

c4 argument (number).

Returns

a fourcc code

This static method constructs the fourcc code of the codec to be used in the constructor VideoWriter::VideoWriter or VideoWriter::open.

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

Initializes or reinitializes video writer.

The method opens video writer. Parameters are the same as in the constructor
VideoWriter::VideoWriter.
open(filename: EmbindString, fourcc: number, fps: number, frameSize: Size, isColor: boolean): boolean;
2 available overloads
open(filename: EmbindString, fourcc: number, fps: number, frameSize: Size): boolean;
open(filename: EmbindString, fourcc: number, fps: number, frameSize: Size, isColor: boolean): boolean;
filename

filename argument (EmbindString).

fourcc

fourcc argument (number).

fps

fps argument (number).

frameSize

frame size argument (Size).

isColor

is color argument (boolean).

Returns

true if video writer has been successfully initialized

The method first calls VideoWriter::release to close the already opened file.

open1

Initializes or reinitializes video writer.

The method opens video writer. Parameters are the same as in the constructor
VideoWriter::VideoWriter.
open1(filename: EmbindString, apiPreference: number, fourcc: number, fps: number, frameSize: Size, isColor: boolean): boolean;
2 available overloads
open1(filename: EmbindString, apiPreference: number, fourcc: number, fps: number, frameSize: Size): boolean;
open1(filename: EmbindString, apiPreference: number, fourcc: number, fps: number, frameSize: Size, isColor: boolean): boolean;
filename

filename argument (EmbindString).

apiPreference

api preference argument (number).

fourcc

fourcc argument (number).

fps

fps argument (number).

frameSize

frame size argument (Size).

isColor

is color argument (boolean).

Returns

true if video writer has been successfully initialized

The method first calls VideoWriter::release to close the already opened file.

open2

Initializes or reinitializes video writer.

The method opens video writer. Parameters are the same as in the constructor
VideoWriter::VideoWriter.
open2(filename: EmbindString, fourcc: number, fps: number, frameSize: Size, params: IntVector): boolean;
filename

filename argument (EmbindString).

fourcc

fourcc argument (number).

fps

fps argument (number).

frameSize

frame size argument (Size).

params

params argument (IntVector).

Returns

true if video writer has been successfully initialized

The method first calls VideoWriter::release to close the already opened file.

open3

Initializes or reinitializes video writer.

The method opens video writer. Parameters are the same as in the constructor
VideoWriter::VideoWriter.
open3(filename: EmbindString, apiPreference: number, fourcc: number, fps: number, frameSize: Size, params: IntVector): boolean;
filename

filename argument (EmbindString).

apiPreference

api preference argument (number).

fourcc

fourcc argument (number).

fps

fps argument (number).

frameSize

frame size argument (Size).

params

params argument (IntVector).

Returns

true if video writer has been successfully initialized

The method first calls VideoWriter::release to close the already opened file.

isOpened

Returns true if video writer has been successfully initialized.

isOpened(): boolean;
Returns

The boolean result.

release

Closes the video writer.

The method is automatically called by subsequent VideoWriter::open and by the VideoWriter
destructor.
release(): void;

write

Writes the next video frame

write(image: Mat): boolean;
image

The written frame. In general, color images are expected in BGR format.

The function/method writes the specified image to video file. It must have the same size as has been specified when opening the video writer.

Returns

true if the frame was written successfully by the underlying backend, false otherwise (for example, on network errors when streaming, encoder failures, or unsupported input frames). Backends that do not surface per-frame status from their native API report true on best-effort success.

set

Sets a property in the VideoWriter.

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

Property identifier from cv::VideoWriterProperties (eg. cv::VIDEOWRITER_PROP_QUALITY) or one of videoio_flags_others

value

Value of the property.

Returns

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

get

Returns the specified VideoWriter property

get(propId: number): number;
propId

Property identifier from cv::VideoWriterProperties (eg. cv::VIDEOWRITER_PROP_QUALITY) or one of videoio_flags_others

Returns

Value for the specified property. Value 0 is returned when querying a property that is not supported by the backend used by the VideoWriter instance.

getBackendName

Returns used backend API name

Note: Stream should be opened.

getBackendName(): string;
Returns

The string 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.