# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

# vision.VideoFileWriter System object

Write video frames and audio samples to video file

## Description

The `VideoFileWriter` object writes video frames and audio samples to a video file. `.wmv` files can be written only on Windows®. 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.

Windows 7 UAC (User Account Control), may require administrative privileges to encode `.mwv` and `.wma` files.

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` 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.

This object allows you to write `.wma/.wmv` streams to disk or across a network connection. Similarly, the `vision.VideoFileReader` object allows you to read `.wma/.wmv` streams to disk or across a network connection. If you want to play an `.mp3/.mp4` file, but you do not have the codecs, you can re-encode the file as `.wma/.wmv`, which is supported by the Computer Vision System Toolbox™.

### 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.

## Construction

`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` or `.wmv`, 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.

### To write to a file:

1. Define and set up your video file writer object using the constructor.

2. Call the `step` method with the optional output filename, `FILENAME`, the video file writer object, `videoFWriter`, and any optional properties. See the syntax below for using the `step` method.

`step(videoFWriter,I)` writes one frame of video, `I`, 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.

`step(videoFWriter,Y,Cb,Cr)` writes one frame of YCbCr 4:2:2 video. The width of Cb and Cr color components must be half of the width of Y. You must set the value of the `FileColorSpace` property to '`YCbCr 4:2:2`'.

`step(videoFWriter,I,AUDIO)` writes one frame of the input video, `I`, and one frame of audio samples, `AUDIO`, to the output file. This applies when you set the `AudioInputPort` property to `true`.

`step(videoFWriter,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.

## Properties

`Filename`

Video output file name

Specify the name of the video file as a character vector. The file extension you give for `Filename` must match the `FileFormat`. The supported file extensions are, `'.wmv'`, `'.avi'`, `'.mj2'`, `'.mp4'`, and `'.m4v'`.

Default: `output.avi`

`FileFormat`

Output file format

Specify the format of the file that is created. Supported formats and abbreviations:

File FormatDescriptionFile ExtensionSupported Platform

`AVI`

Audio-Video Interleave file`.avi` All platforms

`MJ2000`

Motion JPEG 2000 file`.mj2` All platforms

`WMV`

Windows Media Video`.wmv` Windows

`MPEG4`

MPEG-4/H.264 Video`.mp4` , `.m4v`Windows and Mac

Default: `AVI`

`AudioInputPort`

Write audio data

Use this property to control whether the object writes audio samples to the video file. Set this value to `true` to write audio data.

Default: `false`

`FrameRate`

Video frame rate

Specify the frame rate of the video data in frames per second 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 each invocation of the `step` method. For example, if you use a frame rate of `30`, and pass 1470 audio samples to the `step` method, the object sets the audio sample to `44100`, ($1470×30=44100$).

Default: `30`

`AudioCompressor`

Audio compression encoder

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.

`VideoCompressor`

Video compression encoder

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.

`AudioDataType`

Uncompressed audio data type

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

`FileColorSpace`

Color space for output file

Specify the color space of `AVI` files as `RGB` or `YCbCr 4:2:2`. This property applies when you set the `FileFormat` property to `AVI` and only on Windows platforms.

Default: `RGB`

`Quality`

Control size of output video file

Specify the output video quality 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.

`CompressionFactor`

Target ratio between number of bytes in input image and compressed image

Specify the compression factor as an integer greater than `1` to indicate 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.

## Methods

Common to All System Objects
`clone`

Create System object with same property values

`getNumInputs`

Expected number of inputs to a System object

`getNumOutputs`

Expected number of outputs of a System object

`isLocked`

Check locked states of a System object (logical)

`release`

Allow System object property value changes

## Examples

expand all

Set up the reader and writer objects.

```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 AVI file.

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

Close the input and output files.

```release(videoFReader); release(videoFWriter);```