# 2.11.4 Exporting Videos

To export group of graphs as a video, you need to use the Video Writer (vw) object. In order to export a video with actual frames, you must create a video writer object, write some windows to it as frames and then release the video writer. You can only work with one video writer at a time, i.e. each time you create a video writer object with the vw.Create( ) method, you must use vw.Release( ) to release the video writer before you can use the vw.Create( ) method again.

We provide several example scripts showing how to create, write graphs to, or release a video writer object. You can also view a full example in the LabTalk Examples category.

## Create a Video Writer Object

To export a video, first create a video writer object with the vw.Create( ) method. At a minimum, you need to specify the file name (including the complete file path) of the video. You can also specify the codec value for the compression method, frames per second and video dimensions, at this stage.

For example, this script creates a video file named "test.avi" in an existing file path D:\Exported Videos\ with other settings as default (i.e. no compression, 1 frame per second, 640 px as width and 480 px as height):

vw.Create("D:\Exported Videos\test.avi");
//The above script is the same as the script below
//vw.Create("D:\Exported Videos\test.avi", 0, 1, 640, 480);

You can also define the compression method with FourCC code. For example, the script below uses WMV1 compressed format to create the video:

//Define codec with four character code
int codec = vw.FourCC('W', 'M', 'V', '1');
//Create a 800*600 video file as test.avi under user files folder
vw.Create(%Y\My Video.avi, codec, 1, 800, 600)

To check if the video creation is successful, use vw.Create( ). The vw.Create( ) method returns 0 if the video is created, and returns a non-zero value if the creation fails. For example:

//If file path D:\AAA exist,the following should return 0
//If the file path does NOT exist, it will return error code
int err = vw.Create(D:\AAA\test.avi);
if(err==0)
type "video creation is successful";
else
type "video creation failure, the error code is \$(err)";

## Write Graph(s) to a Video Writer Object

Once a video writer object is created, you can start to write graphs into it with the vw.WriteGraph( ) method. In addition to graph windows, you can write other windows including function plots, workbooks, matrixes, and layout pages.

This example script writes the current active window to the video.

vw.WriteGraph( );

You can specify the window name and also the number of frames to write. For example, the following script will add Graph1 as 5 frames:

vw.WriteGraph(Graph1,5);

You can make use of a loop structure to, for example, add all graphs in a video, so that you do not need to write multiple lines of script. The example below writes all graph windows in the project to the video. Each graph will be inserted as 2 frames.

doc -e P
{
vw.WriteGraph(,2);
}

You can return an error code from this method as in the last example of the create video writer session. If the return value is 0, it means that writing the graph (or other window) was successful.

## Release a Video Writer Object

For each video writer object, it is essential to release the video writer to generate the video. The method used in this case is vw.Release( ).

The following script shows a complete example of generating a video file "example.avi" in the user files folder with a newly created empty graph window.

int err = vw.Create(%Y\example.avi);
//Write existing graphs into the video if the video can be created.
if(0 == err)
{
//Create an empty graph window with default template
win -t plot;
vw.WriteGraph( );
}
//Release the video writer
vw.Release( );

The vw.Release( ) method also has a return value. If it is 0 then the video generation is successful; if it is 1, it indicates that the video generation failed.