VideoWriter
Objective-C
@interface VideoWriter : NSObject
Swift
class VideoWriter : NSObject
Video writer class.
The class provides C++ API for writing video files or image sequences.
Member of Videoio
-
The `apiPreference` parameter allows to specify 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_GSTREAMER.
Declaration
Objective-C
- (nonnull instancetype)initWithFilename:(nonnull NSString *)filename apiPreference:(int)apiPreference fourcc:(int)fourcc fps:(double)fps frameSize:(nonnull Size2i *)frameSize isColor:(BOOL)isColor;
Swift
init(filename: String, apiPreference: Int32, fourcc: Int32, fps: Double, frameSize: Size2i, isColor: Bool)
-
The `apiPreference` parameter allows to specify 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_GSTREAMER.
-
Declaration
Objective-C
- (nonnull instancetype)initWithFilename:(nonnull NSString *)filename fourcc:(int)fourcc fps:(double)fps frameSize:(nonnull Size2i *)frameSize isColor:(BOOL)isColor;
Swift
init(filename: String, fourcc: Int32, fps: Double, frameSize: Size2i, isColor: Bool)
Parameters
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 Video Codecs by FOURCC page. 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.
@b 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
) andfourcc=0
ORfps=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.
- With some backends
-
Declaration
Objective-C
- (nonnull instancetype)initWithFilename:(nonnull NSString *)filename fourcc:(int)fourcc fps:(double)fps frameSize:(nonnull Size2i *)frameSize;
Swift
init(filename: String, fourcc: Int32, fps: Double, frameSize: Size2i)
Parameters
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 Video Codecs by FOURCC page. 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. will work with grayscale frames.
@b 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
) andfourcc=0
ORfps=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.
- With some backends
-
The
params
parameter allows to specify extra encoder parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, … .) see cv::VideoWriterProperties -
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.
Declaration
Objective-C
- (nonnull instancetype)init;
Swift
init()
-
Returns used backend API name
- note: Stream should be opened.
Declaration
Objective-C
- (nonnull NSString *)getBackendName;
Swift
func getBackendName() -> String
-
Returns true if video writer has been successfully initialized.
Declaration
Objective-C
- (BOOL)isOpened;
Swift
func isOpened() -> Bool
-
Declaration
Objective-C
- (BOOL)open:(NSString*)filename apiPreference:(int)apiPreference fourcc:(int)fourcc fps:(double)fps frameSize:(Size2i*)frameSize isColor:(BOOL)isColor NS_SWIFT_NAME(open(filename:apiPreference:fourcc:fps:frameSize:isColor:));
Swift
func open(filename: String, apiPreference: Int32, fourcc: Int32, fps: Double, frameSize: Size2i, isColor: Bool) -> Bool
-
Declaration
Objective-C
- (BOOL)open:(NSString*)filename apiPreference:(int)apiPreference fourcc:(int)fourcc fps:(double)fps frameSize:(Size2i*)frameSize NS_SWIFT_NAME(open(filename:apiPreference:fourcc:fps:frameSize:));
Swift
func open(filename: String, apiPreference: Int32, fourcc: Int32, fps: Double, frameSize: Size2i) -> Bool
-
Initializes or reinitializes video writer.
The method opens video writer. Parameters are the same as in the constructor VideoWriter::VideoWriter. - returns: `true` if video writer has been successfully initialized The method first calls VideoWriter::release to close the already opened file.
-
Initializes or reinitializes video writer.
The method opens video writer. Parameters are the same as in the constructor VideoWriter::VideoWriter. - returns: `true` if video writer has been successfully initialized The method first calls VideoWriter::release to close the already opened file.
-
Sets a property in the VideoWriter.
Declaration
Objective-C
- (BOOL)set:(int)propId value:(double)value;
Swift
func set(propId: Int32, value: Double) -> Bool
Parameters
propId
Property identifier from cv::VideoWriterProperties (eg. cv::VIDEOWRITER_PROP_QUALITY) or one of REF: videoio_flags_others
value
Value of the property.
Return Value
true
if the property is supported by the backend used by the VideoWriter instance. -
Returns the specified VideoWriter property
Declaration
Objective-C
- (double)get:(int)propId;
Swift
func get(propId: Int32) -> Double
Parameters
propId
Property identifier from cv::VideoWriterProperties (eg. cv::VIDEOWRITER_PROP_QUALITY) or one of REF: videoio_flags_others
Return Value
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.
-
Concatenates 4 chars to a fourcc code
- 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.
Declaration
Objective-C
+ (int)fourcc:(char)c1 c2:(char)c2 c3:(char)c3 c4:(char)c4;
Swift
class func fourcc(c1: Int8, c2: Int8, c3: Int8, c4: Int8) -> Int32
-
Writes the next video frame
Parameters
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.