see below for more detailsĪlternatively, you could simply do this when declaring your VideoWriter object: VideoWriter outputVideo(filename, fourcc, fps, S) OutputVideo.open(filename, fourcc, fps, S)
#Video codec h 264 code
Declare FourCC code - OpenCV 3.x and beyond Size S = Size(width, height) // Declare Size structureĬonst string filename =. VideoWriter outputVideo // For writing the video #include // Basic OpenCV structures (cv::Mat) Because you want the H264 standard, you would create a VideoWriter object like so: #include // for standard I/O Replace X with each character that belongs to the FourCC (in order). Int fourcc = VideoWriter::fourcc('X', 'X', 'X', 'X') Specifically, you would call it like this: int fourcc = CV_FOURCC('X', 'X', 'X', 'X') It is recommended you use cv::Videowriter::fourcc for OpenCV 3.x and beyond. You use the CV_FOURCC function, and specify four single characters - each corresponding to a single character in the FourCC code of the codec you want. Even though FourCC uses four characters, OpenCV has a utility that parses FourCC and outputs a single integer ID which is used as a lookup to be able to write the correct video format to file. I'm assuming you're using C++, and so the definition of the VideoWriter constructor is: VideoWriter::VideoWriter(const String& filename, int fourcc,ĭouble fps, Size frameSize, bool isColor=true)įilename is the output of the video file, fourcc is the FourCC code for the code you wish to use, fps is the desired frame rate, frameSize is the desired dimensions of the video, and isColor specifies whether or not you want the video to be in colour. Consult the OpenCV docs for more details: Specifically, when you create a VideoWriter object, you specify the FourCC code when constructing it. FourCC stands for Four Character Code, which is an identifier for a video codec, compression format, colour or pixel format used in media files. You can certainly use the VideoWriter class, but you need to use the correct FourCC code that represents the the H264 standard.