X264 VFW

From Emgu CV: OpenCV in .NET (C#, VB, C++ and more)
Jump to: navigation, search

VideoWriter & H.264

Open CV's VideoWriter is used for saving image sequences into Video files. For Windows, the default Open CV implementation for saving MJPEG files do not provide much compression, if you are saving a long video file it may consume lost of hard drive space. H.264 video format generally provide great compression ratio with good image quality. One of the option to encode H.264 file using Open CV's VideoWriter, is to use the FFMPEG backend, and install Cisco's H.264 codec from here. While the cisco H.264 implementation using FFMpeg provides good default compression ratio, there is no option to specific the quality of the video. Some times we would like to have higher quality video output that take up more spaces, some times we want lower quality video that can save spaces and we would like to have that control.

Here we are going to walk you through using the X264 VFW pacakge with VideoWriter, you will be able to specify the video quality and producing H.264 files that matches your needs.

System Requirement

Component Requirement Detail
Emgu CV Version 4.0+
Operation System Windows
  • The code used in this tutorial use components from the Open CV / Emgu CV 4.0 release. At tise moment you will need to build the binary from the source code in the Git repository.

Installing X264 VFW

You can download X264 VFW from sourceforge

Note: X264 VFW licensed under GPL v2, you may need to seek professional advise if you are going to use it in a commercial product.

Once downloaded and installed, there should be two configuration program for you to setup x264vfw. One is for the 32-bit codec (Configure x264vfw) and the other is for the 64bit codec (Config x264vfw64). You can use the default configuration but here I am going to walk you through the 64-bit settings.

Once the configuration page is loaded, it will look like the screen below:

X264vfw64 configuration.png

Use the Ratefactor under the Rate control to set the video output quality. Higher quality means bigger file and lower quality means smaller file.

You would also want to check the "Create stats file" box and let the X264 VFW codec write out some statistic to a file on the local disk. It will be a good way to confirm Open CV is using X264 VFW to save video files later. Once that is done, click the "OK" button to finish the configuration.

Creating VideoWriter

To use the X364 VFW codec, you will need to make sure your video file has the ".avi" extension. e.g.

String fileName = String.Format("video_out.avi");

The FOURCC code has to be 'X264'. e.g.

int fourcc = VideoWriter.Fourcc('X', '2', '6', '4');

When creating the VideoWriter, we need to use the "VFW" backend. In Emgu CV v4.0, we have wrapped the Open CV functions that can be used to query the backend.

Backend[] backends = CvInvoke.WriterBackends;
int backend_idx = 0; //any backend;
foreach (Backend be in backends)
{
  if (be.Name.Equals("VFW"))
  {
    backend_idx = be.ID;
    break;
  }
}
//backend_idx should be the VFW backend now

Now we can create our VideoWriter and write videos

using (VideoWriter vw = new VideoWriter(fileName, backend_idx, fourcc, 30, new Size(width, height), true)
{
// call vw.Write() to write frames to VideoWriter
}

Congratulations! Now you should have your video written out using X264VFW. To confirm this, and if you have setup a log file in the previous step, go check the log file.

You can also review your video, get a feel of the quality, check the file size, and if needed, re-configure X264 VFW using the steps we listed earlier.

Hope this helps!