【问题标题】:Writing a video file using H.264 compression in OpenCV在 OpenCV 中使用 H.264 压缩编写视频文件
【发布时间】:2023-04-09 11:51:01
【问题描述】:

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

【问题讨论】:

    标签: c++ opencv image-processing computer-vision video-processing


    【解决方案1】:

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

    对于 H.264,大多数人使用 AVC,如下所示:

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

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

    【讨论】:

      【解决方案2】:

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

      具体来说,当你创建一个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');
      

      用属于 FourCC 的每个字符替换 X(按顺序)。因为您需要 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。当您指定-1 时,Linux 或 Mac OS 不会弹出此窗口。换句话说:

      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'。根据经验,'H','2','6','4''X','2','6','4'在尝试指定 FourCC 代码时似乎不起作用。

      【讨论】:

      • 我试过这个程序。首先,当我尝试将 Fourcc 参数用作 -1 时,没有弹出窗口。当我尝试使用您提到的 x264 时,出现“无法打开编解码器:libx264”错误和其他一些错误,例如“检测到损坏的 ffmpeg 默认设置”。顺便说一句,我应该怎么做我使用 ubuntu 14.04
      • 这意味着您没有 H264 库.... 这是另一篇文章。下班后,我会给你一个链接。顺便说一句,-1 仅适用于 Windows。也忘记提了。糟糕!
      • 谢谢 .. 当您安装 h264 lib 时请告诉我。 .我非常需要知道
      • 如果你这样做了,还要注意fps现在是double
      • @YunusTemurlenk 您可以使用cv::imwrite 并指定不同的图像格式和不同的压缩选项。上述方法仅适用于视频,因此您绝对不应该使用它。有关示例,请参见 stackoverflow.com/questions/7237144/…
      猜你喜欢
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 2018-02-06
      相关资源
      最近更新 更多