Main Content

vision.VideoFileWriter

Write video frames and audio samples to video file

Description

The VideoFileWriter object writes video frames and audio samples to a video file. The video and audio can be compressed. The available compression types depend on the encoders installed on the platform.

Note

This block supports code generation for platforms that have file I/O available. You cannot use this block with Simulink® Desktop Real-Time™ software, because that product does not support file I/O.

This object performs best on platforms with Version 11 or later of Windows Media® Player software. This object supports only uncompressed RGB24 AVI files on Linux® and Mac platforms.

The generated code for this object relies on prebuilt library files. You can run this code outside the MATLAB® environment, or redeploy it, but be sure to account for these extra library files when doing so. The packNGo function creates a single zip file containing all of the pieces required to run or rebuild this code. See packNGo (Simulink Coder) for more information.

To run an executable file that was generated from an object, you may need to add precompiled shared library files to your system path. See MATLAB Coder and Simulink Shared Library Dependencies for details.

Note

Starting in R2016b, instead of using the step method to perform the operation defined by the System object™, you can call the object with arguments, as if it were a function. For example, y = step(obj,x) and y = obj(x) perform equivalent operations.

Creation

Description

example

videoFWriter = vision.VideoFileWriter returns a video file writer System object, videoFWriter. It writes video frames to an uncompressed 'output.avi' video file. Every call to the step method writes a video frame.

videoFWriter = vision.VideoFileWriter(Filename) returns a video file writer object, videoFWriter that writes video to a file, Filename. The file type can be .avi, .mj2, .mp4, and .m4v specified by the FileFormat property.

videoFWriter = vision.VideoFileWriter(___,Name,Value) configures the video file writer properties, specified as one or more name-value pair arguments. Unspecified properties have default values.

Name is a property name and Value is the corresponding value. Name must appear inside single quotes (''). You can specify several name-value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: videoFWriter = vision.VideoFileWriter('myFile.avi','FrameRate',... videoFReader.info.VideoFrameRate);

Properties

expand all

Video output file name, specified as a character vector. The file extension you give for Filename must match the FileFormat.

Output file format, specified as one of the supported file formats shown in the table.

File FormatDescriptionFile ExtensionSupported Platform

'AVI'

Audio-Video Interleave file.avi All platforms

'MJ2000'

Motion JPEG 2000 file.mj2 All platforms

'MPEG4'

MPEG-4/H.264 Video.mp4 , .m4vWindows® and Mac

Write audio data, specified as false or true. Use this property to control whether the object writes audio samples to the video file. Set this value to true to write audio data. To write audio and video to a file, you must use the .avi format .

Video frame rate in frames per second, specified as a positive numeric scalar. For videos which also contain audio data, the rate of the audio data will be determined as the rate of the video multiplied by the number of audio samples passed in. For example, if you use a frame rate of 30, and pass 1470 audio samples, the object sets the audio sample to 44100, (1470 x 30 = 44100).

Specify the type of compression algorithm to implement for audio data. This compression reduces the size of the video file. Choose None (uncompressed) to save uncompressed audio data to the video file. The other options reflect the available audio compression algorithms installed on your system. This property applies only when writing AVI files on Windows platforms.

Specify the type of compression algorithm to use to compress the video data. This compression reduces the size of the video file. Choose None (uncompressed) to save uncompressed video data to the video file. The VideoCompressor property can also be set to one of the compressors available on your system. To obtain a list of available video compressors, you can use tab completion. Follow these steps:

  1. Instantiate the object:

    y = vision.VideoFileWriter

  2. To launch the tab completion functionality, type the following up to the open quote.

    y.VideoCompressor='

    A list of compressors available on your system will appear after you press the Tab key. For example:

This property applies only when writing AVI files on Windows platforms.

Specify the compressed output audio data type. This property only applies when you write uncompressed WAV files.

Color space for output AVI file, specified as RGB or YCbCr 4:2:2. This property applies when you set the FileFormat property to AVI and only on Windows platforms.

Control size of output video file, specified as an integer in the range [0,100]. Increase this value for greater video quality. However, doing so increases the file size. Decrease the value to lower video quality with a smaller file size.

The Quality property only applies when you are writing MPEG4 video files (on Windows or Mac) or when you are writing MJPEG-AVI video only files on a Mac or Linux.

Target ratio between number of bytes in input image and compressed image, specified as an integer greater than 1. The CompressionFactor indicates the target ratio between the number of bytes in the input image and the compressed image. The data is compressed as much as possible, up to the specified target. This property applies only when writing Lossy MJ2000 files.

Usage

Description

example

Filename = videoFWriter(videoFrame) writes one frame of video, videoFrame, to the output file. The input video can be an M-by-N-by-3 truecolor RGB video frame, or an M-by-N grayscale video frame..

Filename = videoFWriter(videoFrame,audio) writes one frame of the input video, videoFrame, and one frame of audio samples, audio, to the output file. This applies when you set the AudioInputPort property to true.

Filename = videoFWriter(videoFrame,Y,Cb,Cr,audio) writes one frame of YCbCr 4:2:2 video, and one frame of audio samples, audio, to the output file. This applies when you set the AudioInputPort to true and the FileColorSpace property to 'YCbCr 4:2:2'. The width of Cb and Cr color components must be half of the width of Y.

Input Arguments

expand all

Video frame, returned as a truecolor or 2-D grayscale image.

YCbCr color format, returned in the YCbCr 4:2:2 format.

One frame of audio samples, returned in one of the following formats.

PlatformSupported File Name Extensions
All PlatformsAVI (.avi)
Windows Image:
.jpg,.bmp
Video:
MPEG (.mpeg)
MPEG-2 (.mp2)
MPEG-1.mpg

MPEG-4, including H.264 encoded video (.mp4, .m4v)
Motion JPEG 2000 (.mj2)
Windows Media Video (.wmv,.asf, .asx, .asx)
and any format supported by Microsoft DirectShow® 9.0 or higher.
Audio:
WAVE (.wav)
Windows Media Audio File (.wma)
Audio Interchange File Format (.aif, .aiff)
Compressed Audio Interchange File Format(.aifc),
MP3 (.mp3)
Sun Audio (.au)
Apple (.snd)
Macintosh Video:
.avi
Motion JPEG 2000 (.mj2)
MPEG-4, including H.264 encoded video (.mp4, .m4v)
Apple QuickTime Movie (.mov)
and any format supported by QuickTime as listed on http://support.apple.com/kb/HT3775.
Audio:
Uncompressed .avi
Linux Motion JPEG 2000 (.mj2)
Any format supported by your installed plug-ins for GStreamer 0.1 or higher, as listed on http://gstreamer.freedesktop.org/documentation/plugins.html, including Ogg Theora (.ogg).

Windows XP and Windows 7 x64 platform ships with a limited set of 64-bit video and audio codecs. If a compressed multimedia file fails to play, try saving the multimedia file to a supported file format listed in the table above.

If you use Windows, use Windows Media player Version 11 or later.

Note

MJ2 files with bit depth higher than 8-bits is not supported by vision.VideoFileReader. Use VideoReader and VideoWriter for higher bit depths.

Reading audio from compressed MP4 files with video and audio is not supported by vision.VideoFileReader.

Output Arguments

expand all

Video output file name, specified as '.avi', '.mj2', '.mp4', and '.m4v'. The file extension you give for Filename must match the FileFormat.

Object Functions

To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object named obj, use this syntax:

release(obj)

expand all

infoInformation about specified video file
isDoneEnd-of-file status (logical)
stepRun System object algorithm
releaseRelease resources and allow changes to System object property values and input characteristics
resetReset internal states of System object

Examples

collapse all

Load a video file and write the file to a writer object.

videoFReader = vision.VideoFileReader('viplanedeparture.mp4');
videoFWriter = vision.VideoFileWriter('myFile.avi', ...
    'FrameRate',videoFReader.info.VideoFrameRate);

Write the first 50 frames from original file into a newly created myFile.avi file.

for i=1:50
  videoFrame = videoFReader();
  videoFWriter(videoFrame);
end

Close the input and output files.

release(videoFReader);
release(videoFWriter);

Extended Capabilities

Introduced in R2012a