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

Writing a video file using H.264 compression in OpenCV

本文关键字:压缩 视频 文件 OpenCV      更新时间:2023-10-16

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

您当然可以使用 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)

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

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

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

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 - 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对象时简单地执行此操作:

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

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


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

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

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

准备好帧并存储在frm中以写入文件后,您可以执行以下操作之一:

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 .

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

正如 rayryeng 所说,您需要将正确的 FourCC 代码传递给 VideoWriter

对于H.264,大多数人使用AVC,看起来像这样:

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

mp4v似乎也适用于许多人。如果它们都没有,请检查可用编解码器的列表。