简体   繁体   中英

Writing a video file using H.264 compression in OpenCV

How do I write a video using H.264 compression with the VideoWriter class in OpenCV? I basically want to get a video from the webcam and save it after a character is pressed. The ouput video file is huge when using MPEG4 Part 2 compression.

You can certainly use the VideoWriter class, but you need to use the correct FourCC code 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.

Specifically, when you create a VideoWriter object, you specify the FourCC code when constructing it. Consult the OpenCV docs for more details: http://docs.opencv.org/trunk/modules/highgui/doc/reading_and_writing_images_and_video.html#videowriter-videowriter

I'm assuming you're using C++, and so the definition of the VideoWriter constructor is:

VideoWriter::VideoWriter(const String& filename, int fourcc, 
                         double fps, Size frameSize, bool isColor=true)

filename 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. 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 CV_FOURCC function, and specify four single characters - each corresponding to a single character in the FourCC code of the codec you want. Note that CV_FOURCC is for OpenCV 2.x. It is recommended you use cv::Videowriter::fourcc for OpenCV 3.x and beyond.

Specifically, you would call it like this:

int fourcc = CV_FOURCC('X', 'X', 'X', 'X');
int fourcc = VideoWriter::fourcc('X', 'X', 'X', 'X');

Replace X with each character that belongs to the FourCC (in order). Because you want the H264 standard, you would create a VideoWriter object like so:

#include <iostream> // for standard I/O
#include <string>   // for strings

#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat)
#include <opencv2/highgui/highgui.hpp>  // Video write

using namespace std;
using namespace cv;

int main()
{
    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 - OpenCV 2.x
    // int fourcc = CV_FOURCC('H','2','6','4');
    // Declare FourCC code - OpenCV 3.x and beyond
    int fourcc = VideoWriter::fourcc('H','2','6','4');

    // Declare FPS here
    double fps = ...;
    outputVideo.open(filename, fourcc, fps, S);

    // Put your processing code here
    // ...

    // Logic to write frames here... see below for more details
    // ...

    return 0;
}

Alternatively, you could simply do this when declaring your VideoWriter object:

VideoWriter outputVideo(filename, fourcc, fps, S);

If you use the above, it's not required that you call open as this will automatically open up the writer for writing frames to file.


If you're not sure if H.264 is supported on your computer, specify -1 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. I'd like to mention that this only works for Windows. Linux or Mac OS doesn't have this window popping out when you specify -1 . In other words:

VideoWriter outputVideo(filename, -1, fps, S);

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 VideoWriter constructor so that you will get a VideoWriter instance that represents a VideoWriter that will write that type of video to file.

Once you have a frame ready, stored in frm for writing to the file, you can do either:

outputVideo << frm; 

OR

outputVideo.write(frm);

As a bonus, here's a tutorial on how to read/write videos in OpenCV: http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html - 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 'X','2','6','4' , so if 'H','2','6','4' doesn't work, replace H with X .

Another small note. If you are using Mac OS, then what you need to use is 'A','V','C','1' or 'M','P','4','V' . From experience, 'H','2','6','4' or 'X','2','6','4' when trying to specify the FourCC code doesn't seem to work.

For H.264 most people use AVC, so

cv2.VideoWriter_fourcc('a','v','c','1')

mp4v also seems to work for many.

I'm Kind of late, But my powerful VidGear Python Library's WriteGear API that automates the process of pipelining OpenCV frames into FFmpeg on any platform in real-time with Hardware Encoders support and at the same time provides same opencv-python syntax . Here's a basic python example for encoding H.264 video with it:

# import libraries
from vidgear.gears import WriteGear
import cv2

output_params = {"-vcodec":"libx264", "-crf": 0, "-preset": "fast"} #define (Codec,CRF,preset) FFmpeg tweak parameters for writer

stream = cv2.VideoCapture(0) #Open live webcam video stream on first index(i.e. 0) device

writer = WriteGear(output_filename = 'Output.mp4', compression_mode = True, logging = True, **output_params) #Define writer with output filename 'Output.mp4' 

# infinite loop
while True:
    
    (grabbed, frame) = stream.read()
    # read frames

    # check if frame empty
    if not is grabbed:
        #if True break the infinite loop
        break
    

    # {do something with frame here}
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # write a modified frame to writer
    writer.write(gray) 
       
    # Show output window
    cv2.imshow("Output Frame", frame)

    key = cv2.waitKey(1) & 0xFF
    # check for 'q' key-press
    if key == ord("q"):
        #if 'q' key-pressed break out
        break

cv2.destroyAllWindows()
# close output window

stream.release()
# safely close video stream
writer.close()
# safely close writer

Source: https://abhitronix.github.io/vidgear/latest/gears/writegear/compression/usage/#using-compression-mode-with-opencv

You can check out VidGear Docs for more advanced applications and features.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM