× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Writing a video file using H.264 compression in OpenCV

Ray Phan
Feb 02, 2015
<p>You can certainly use the <code>VideoWriter</code> class, but you need to use <a href="http://www.fourcc.org/" rel="nofollow">the correct FourCC code</a> that represents the the H264 standard. FourCC stands for Four Character Code, which is an identifier for a video codec, compression format, colour or pixel format used in media files.</p> <p>Specifically, when you create a <code>VideoWriter</code> object, you specify the FourCC code when constructing it. Consult the OpenCV docs for more details: <a href="http://docs.opencv.org/trunk/modules/highgui/doc/reading_and_writing_images_and_video.html#videowriter-videowriter" rel="nofollow">http://docs.opencv.org/trunk/modules/highgui/doc/reading_and_writing_images_and_video.html#videowriter-videowriter</a></p> <p>I'm assuming you're using C++, and so the definition of the <code>VideoWriter</code> constructor is:</p> <pre><code>VideoWriter::VideoWriter(const String&amp; filename, int fourcc, double fps, Size frameSize, bool isColor=true) </code></pre> <p><code>filename</code> is the output of the video file, <code>fourcc</code> is the FourCC code for the code you wish to use, <code>fps</code> is the desired frame rate, <code>frameSize</code> is the desired dimensions of the video, and <code>isColor</code> specifies whether or not you want the video to be in colour. 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. You use the <code>CV_FOURCC</code> function, and specify four single characters - each corresponding to a single character in the FourCC code of the codec you want.</p> <p>Specifically, you would call it like this:</p> <pre><code>int fourcc = CV_FOURCC('X', 'X', 'X', 'X'); </code></pre> <p>Replace <code>X</code> with each character that belongs to the FourCC (in order). Because you want the H264 standard, you would create a <code>VideoWriter</code> object like so:</p> <pre><code>#include &lt;iostream&gt; // for standard I/O #include &lt;string&gt; // for strings #include &lt;opencv2/core/core.hpp&gt; // Basic OpenCV structures (cv::Mat) #include &lt;opencv2/highgui/highgui.hpp&gt; // Video write using namespace std; using namespace cv; VideoWriter outputVideo; // For writing the video int width = ...; // Declare width here int height = ...; // Declare height here Size S = Size(width, height); // Declare Size structure // Open up the video for writing const string filename = ...; // Declare name of file here // Declare FourCC code int fourcc = CV_FOURCC('H','2','6','4'); // Declare FPS here int fps = ...; outputVideo.open(filename, fourcc, fps, S); </code></pre> <p>Alternatively, you could simply do this:</p> <pre><code>VideoWriter outputVideo(filename, fourcc, fps, S); </code></pre> <hr> <p>If you're not sure if H.264 is supported on your computer, specify <code>-1</code> as the FourCC code, and a window should pop up when you run the code that displays all of the available video codecs that are on your computer. In other words:</p> <pre><code>VideoWriter outputVideo(filename, -1, fps, S); // OR // VideoWriter.open(filename, -1, fps, 5); </code></pre> <p>You can choose which one is most suitable should H.264 not exist on your computer. Once that is done, OpenCV will create the right FourCC code to be input into the <code>VideoWriter</code> constructor so that you will get a VideoWriter instance that represents a <code>VideoWriter</code> that will write that type of video to file.</p> <p>Once you have a frame ready, stored in <code>frm</code> for writing to the file, you can do either:</p> <pre><code>outputVideo &lt;&lt; frm; </code></pre> <p>OR</p> <pre><code>outputVideo.write(frm); </code></pre> <hr> <p>As a bonus, here's a tutorial on how to read/write videos in OpenCV: <a href="http://docs.opencv.org/trunk/doc/py_tutorials/py_gui/py_video_display/py_video_display.html" rel="nofollow">http://docs.opencv.org/trunk/doc/py_tutorials/py_gui/py_video_display/py_video_display.html</a> - However, it's written for Python, but what is good to know is near the bottom of the link, there is a list of FourCC codes that are known to work for each operating system. BTW, the FourCC code they specify for the H264 standard is actually <code>'X','2','6','4'</code>, so if <code>'H','2','6','4'</code> doesn't work, replace <code>H</code> with <code>X</code>. </p> <p>Another small note. If you are using Mac OS, then what you need to use is <code>'A','V','C','1'</code> or <code>'M','P','4','V'</code>. From experience, <code>'H','2','6','4'</code>or <code>'X','2','6','4'</code>when trying to specify the FourCC code doesn't seem to work.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/28163201/Writing%20a%20video%20file%20using%20H.264%20compression%20in%20OpenCV/28188331">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus