This chapter shows you how to save a video stream as a compressed video file. In contrast to the Capturing an AVI File example, the MediaStreamSink is used to allow several video file formats to be recorded.
The source code for this sample program can be found in the samples\VB6\Capturing a Video File directory.
Create a new project and add IC Imaging Control to the form. Before you run the program, select the video 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 selecting a device, 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 Video. Name the buttons cmdStartLive, cmdStopLive and cmdCaptureVideo respectively.
Private Sub cmdStartLive_Click() ICImagingControl1.LiveStart End Sub
Private Sub cmdStopLive_Click() ICImagingControl1.LiveStop End Sub
Run the program. You can now start and stop the live image by clicking on the respective buttons. This does not yet create a video 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 frmSaveVideo. Add a combo box for the available media stream containers (video file types) and a combo box for the available video codecs. Insert a text box for the video filename, and buttons to start and stop video capture. (You may want to load the sample project instead of creating the form manually).
In the FormLoad event, the combo boxes are filled with the names of all media stream containers and video codecs installed on your computer.
Private Sub Form_Load() ' Fill the combo box "cboMediaStreamContainer" with the available ' Media Stream Containers (video file types) Dim Container As MediaStreamContainer For Each Container In ImagingControl.MediaStreamContainers cboMediaStreamContainer.AddItem Container.Name Next cboMediaStreamContainer.ListIndex = 0 ' Fill the combobox "cboVideoCodec" with the available ' video compressors (codecs). Dim Codec As AviCompressor For Each Codec In ImagingControl.AviCompressors cboVideoCodec.AddItem Codec.Name Next cboVideoCodec.ListIndex = 0 cboVideoCodec.Enabled = GetSelectedMediaStreamContainer().CustomCodecSupported chkPause.Value = 0 End Sub
The Click event of the "Start Capture" button initializes the global Sink variable with the currently selected media stream container, codec and filename. The recording process is started by calling LiveStart:
Private Sub cmdStartCapture_Click() On Error GoTo cmdStartCapture_Click If txtFilename.Text = "" Then MsgBox "Please select an video filename first.", vbExclamation + vbOKOnly Exit Sub End If Sink.StreamContainer = GetSelectedMediaStreamContainer() If Sink.StreamContainer.CustomCodecSupported Then Sink.Codec = GetSelectedVideoCodec() End If Sink.FileName = txtFilename.Text If chkPause.Value = 1 Then Sink.SinkModeRunning = False Else Sink.SinkModeRunning = True End If ' Save old sink settings OldLiveMode = ImagingControl.LiveVideoRunning Set OldSink = ImagingControl.Sink ' Stop live mode to set a new sink ImagingControl.LiveStop ' Set the new sink ImagingControl.Sink = Sink ' Re-start live mode. Video capturing is now started, if the sink is running. ImagingControl.LiveStart cmdStartCapture.Enabled = False cmdStopCapture.Enabled = True cmdClose.Enabled = False Exit Sub cmdStartCapture_Click: MsgBox Err.Description End Sub
The Pause button is a check box with a graphical style. It is used to set and reset the MediaStreamSink.SinkModeRunning property. If the SinkModeRunning property is set to False, video 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 SinkModeRunning property is set to True, IC Imaging Control immediately continues video capture. A previously started video file will be continued.
Private Sub chkPause_Click() If chkPause.Value = 1 Then Sink.SinkModeRunning = False Else Sink.SinkModeRunning = True End If End Sub
The video capture can be started even if the SinkModeRunning property is set to False. In this case, the image stream will be initialized and started and the live video is displayed. But the images are not saved to the video file until the SinkModeRunning property is set to True. This has the advantage that video capturing can be immediately started, paused and restarted on an event.
Finished. You can now run the program, select a codec and create a video 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.
Established in 1990, The Imaging Source is one of the leading manufacturers of industrial cameras, frame grabbers and video converters for production automation, quality assurance, logistics, medicine, science and security.
Our comprehensive range of cameras with USB 3.1, USB 3.0, USB 2.0, GigE interfaces and other innovative machine vision products are renowned for their high quality and ability to meet the performance requirements of demanding applications.