在 OpenCV 中使用 H.264 压缩编写视频文件 [英] Writing a video file using H.264 compression in OpenCV

查看:118
本文介绍了在 OpenCV 中使用 H.264 压缩编写视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 H.264 压缩和 OpenCV 中的 VideoWriter 类编写视频?我基本上想从网络摄像头获取视频并在按下字符后保存.使用 MPEG4 Part 2 压缩时,输出视频文件很大.

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.

推荐答案

你当然可以使用VideoWriter类,但是你需要使用代表 H264 标准的正确 FourCC 代码.FourCC 代表 Four Character Code,它是媒体文件中使用的视频编解码器、压缩格式、颜色或像素格式的标识符.

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.

具体来说,当你创建一个 VideoWriter 对象时,你在构造它的时候指定了 FourCC 代码.有关详细信息,请参阅 OpenCV 文档:http://docs.opencv.org/trunk/modules/highgui/doc/reading_and_writing_images_and_video.html#videowriter-videowriter

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

我假设你使用的是 C++,所以 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 是视频文件的输出,fourcc 是您希望使用的代码的 FourCC 代码,fps 是想要的帧速率,frameSize 是所需的视频尺寸,isColor 指定您是否希望视频是彩色的.即使 FourCC 使用四个字符,OpenCV 也有一个实用程序可以解析 FourCC 并输出一个整数 ID,该 ID 用作查找以便能够将正确的视频格式写入文件.您使用 CV_FOURCC 函数,并指定四个单个字符 - 每个字符对应于所需编解码器的 FourCC 代码中的单个字符.请注意,CV_FOURCC 适用于 OpenCV 2.x.对于 OpenCV 3.x 及更高版本,建议您使用 cv::Videowriter::fourcc.

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');

X 替换为属于 FourCC 的每个字符(按顺序).因为你想要 H264 标准,你会像这样创建一个 VideoWriter 对象:

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;
}

或者,您可以在声明 VideoWriter 对象时简单地执行此操作:

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

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

如果您使用上述方法,则不需要调用 open,因为这会自动打开写入器以将帧写入文件.

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.

如果您不确定您的计算机是否支持 H.264,请将 -1 指定为 FourCC 代码,当您运行显示所有计算机上可用的视频编解码器.我想提一下,这仅适用于 Windows.当您指定 -1 时,Linux 或 Mac OS 不会弹出此窗口.换句话说:

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);

如果您的计算机上不存在 H.264,您可以选择最适合的一种.完成后,OpenCV 将创建正确的 FourCC 代码以输入到 VideoWriter 构造函数中,这样您将获得一个 VideoWriter 实例,该实例表示将写入该类型的 VideoWriter视频文件.

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.

一旦你准备好框架,存储在 frm 中以写入文件,你可以执行以下任一操作:

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

outputVideo << frm; 

outputVideo.write(frm);

<小时>

作为奖励,这里有一个关于如何在 OpenCV 中读/写视频的教程:http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html - 但是,它是为 Python 编写的,但有什么好知道的在链接底部附近,有一个已知适用于每个操作系统的 FourCC 代码列表.顺便说一句,他们为 H264 标准指定的 FourCC 代码实际上是 'X','2','6','4',所以如果 'H','2','6','4' 不起作用,将 H 替换为 X.


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.

另一个小笔记.如果您使用的是 Mac OS,那么您需要使用的是 'A','V','C','1''M','P','4','V'.根据经验,'H','2','6','4''X','2','6','4'尝试时指定 FourCC 代码似乎不起作用.

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.

这篇关于在 OpenCV 中使用 H.264 压缩编写视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆