在OpenCV中使用H.264压缩编写视频文件

Kau*_*kar 14 c++ opencv image-processing video-processing computer-vision

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

ray*_*ica 21

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

具体来说,在创建VideoWriter对象时,可以在构造时指定FourCC代码.有关更多详细信息,请参阅OpenCV文档:http://docs.opencv.org/trunk/modules/highgui/doc/reading_and_writing_images_and_video.html#videowriter-videowriter

我假设您正在使用C++,因此VideoWriter构造函数的定义是:

VideoWriter::VideoWriter(const String& filename, int fourcc, 
                         double fps, Size frameSize, bool isColor=true)
Run Code Online (Sandbox Code Playgroud)

filename是视频文件的输出,fourcc是您要使用的代码的FourCC代码,fps是所需的帧速率,frameSize是视频的所需尺寸,并isColor指定您是否希望视频是彩色的.即使FourCC使用四个字符,OpenCV也有一个实用程序,它解析FourCC并输出一个整数ID,用作查找,以便能够将正确的视频格式写入文件.您可以使用该CV_FOURCC函数,并指定四个单个字符 - 每个字符对应于所需编解码器的FourCC代码中的单个字符.

具体来说,你会这样称呼它:

int fourcc = CV_FOURCC('X', 'X', 'X', 'X');
Run Code Online (Sandbox Code Playgroud)

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

#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
    int fourcc = CV_FOURCC('H','2','6','4');

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

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

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

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

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

VideoWriter outputVideo(filename, fourcc, fps, S);
Run Code Online (Sandbox Code Playgroud)

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


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

VideoWriter outputVideo(filename, -1, fps, S);
Run Code Online (Sandbox Code Playgroud)

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

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

outputVideo << frm; 
Run Code Online (Sandbox Code Playgroud)

要么

outputVideo.write(frm);
Run Code Online (Sandbox Code Playgroud)

作为奖励,这里是关于如何在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'不起作用,请替换HX.

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