This chapter shows you how to save a video stream as a compressed AVI file.
To see how to save video streams in different file formats, see the Capturing a Video File example.
The source code for this sample program can be found under samples\C#\Capturing an AVI File in your My Documents/IC Imaging Control 3.5 directory.
Create a new project and add IC Imaging Control to the form. Before you run the program, select the video capture device, input and video format as shown in the FirstSteps chapter. Alternatively, run the program without selecting a device. In this case, the program shows the device selection dialog provided by IC Imaging Control. If you close this dialog without having first made a selection, the program will display an error message and terminate.
Now add 3 buttons to the form and label them Start Live, Stop Live and Capture AVI. Name the buttons cmdStartLive, cmdStopLive and cmdCaptureVideo respectively.
Add calls to IC Imaging Control's LiveStart and LiveStop methods:
[C#]
private void cmdStartLive_Click(object sender, EventArgs e) { icImagingControl1.LiveStart(); cmdStartLive.Enabled = false; cmdStopLive.Enabled = true; }
[C#]
private void cmdStopLive_Click(object sender, EventArgs e) { icImagingControl1.LiveStop(); cmdStartLive.Enabled = true; cmdStopLive.Enabled = false; }
Run the program. You can now start and stop the live image by clicking on the respective buttons. This does not yet create an AVI file, but is required to create a preview for the live video stream. This is usually required to set up the camera before actually starting to record.
Now add another form to your project. Name it frmWriteAVI. Add a combo box for the available video codecs, a text box for the AVI filename, and buttons to start and stop AVI capture. (You may want to load the sample project instead of creating the form manually).
In the FormLoad event, the selection list is filled with the names of all video codecs installed on your computer. These can be used as an AVI compressor.
[C#]
private void WriteAvi_Load(object sender, EventArgs e) { cboVideoCodec.DataSource = TIS.Imaging.AviCompressor.AviCompressors; // Show the first codec in the combobox. cboVideoCodec.SelectedIndex = 0; cmdStartCapture.Enabled = true; cmdStopCapture.Enabled = false; }
The Click event of the Start Capture button calls IC Imaging Control's AviStartCapture method, with the filename and the name of the video codec as a parameter. There is also some code to disable the Close button and enable the Stop Capture button and to check whether the user has selected a codec and a filename:
[C#]
private void cmdStartCapture_Click(object sender, EventArgs e) { if (txtFilename.Text == "") { MessageBox.Show("Please select an AVI filename first."); return; } _wasRunning = _imagingControl.LiveVideoRunning; if( _imagingControl.LiveVideoRunning ) _imagingControl.LiveStop(); _savedSink = _imagingControl.Sink; _imagingControl.Sink = new TIS.Imaging.MediaStreamSink( (TIS.Imaging.AviCompressor)cboVideoCodec.SelectedItem, txtFilename.Text ); try { _imagingControl.LiveStart(); cmdStopCapture.Enabled = true; cmdStartCapture.Enabled = false; } catch { _imagingControl.Sink = _savedSink; if( _wasRunning ) { _imagingControl.LiveStart(); } } }
The Pause button is a check box with a graphical style. It is used to set and reset the LiveCapturePause property of IC Imaging Control. If the LiveCapturePause property is set to True, AVI capture is paused. This means, that only the sink path of the image stream is paused. The live video is still displayed, but the video is not saved to the video file. If the LiveCapturePause property is set to False, IC Imaging Control immediately continues AVI capture. A previously started AVI file will be continued.
[C#]
private void chkPause_CheckedChanged(object sender, EventArgs e) { _imagingControl.Sink.SinkModeRunning = chkPause.Checked; }
The AVI capture can be started by a call to AviStartCapture, even if the LiveCapturePause property is set to True. In this case, the image stream is initialized and started and the live video is displayed. The images are not saved to the AVI file because the sink path is paused. As soon as the LiveCapturePause property is set to False, the sink path is started and images are written to the AVI file. The advantage of this is that the AVI capturing can be started, paused and restarted immediately on an event.
Finished. You can now run the program, select a codec, and create an AVI file. Note that compressing a video stream is a highly CPU intensive operation and not all codecs will be able to work at full image resolution and full frame rate.