【opencv入门教程】10.保存实时视频

文章选自:

一、VideoWriter类

复制代码
该类提供了用于编写视频文件或图像序列的
cpp 复制代码
CV_WRAP VideoWriter();
 @brief 默认构造函数


CV_WRAP VideoWriter(const String& filename, int fourcc, double fps,
            Size frameSize, bool isColor = true);
@param filename 输出视频文件的名称。
@param fourcc 用于压缩帧的编解码器的 4 字符代码。例如,VideoWriter::fourcc('P','I','M','1') 是 MPEG-1 编解码器,VideoWriter::fourcc('M','J','P','G')
是动态 JPEG 编解码器等。
@param fps 创建的视频流的帧速率。
@param frameSize 视频帧的大小。
@param isColor 如果不为零,则编码器将期望并编码彩色帧,否则它将使用灰度帧。

@b 提示:
- 对于某些后端,`fourcc=-1` 将弹出系统中的编解码器选择对话框。
- 要保存图像序列,请使用适当的文件名(例如 `img_%02d.jpg`)和 `fourcc=0` 或 `fps=0`。
  使用未压缩的图像格式(例如 `img_%02d.BMP`)来保存原始帧。
- 大多数编解码器都是有损的。如果要创建无损视频文件,则需要使用无损编解码器
  (例如 FFMPEG FFV1、Huffman HFYU、Lagarith LAGS 等)。
- 如果启用了 FFMPEG,使用 `codec=0; fps=0;` 可以创建一个未压缩的(原始的)视频文件。


CV_WRAP VideoWriter(const String& filename, int apiPreference, int fourcc, double fps,
            Size frameSize, bool isColor = true);
@param `apiPreference` 参数允许指定要使用的 API 后端。可用于强制执行特定的读取器实现
如果多个可用:例如 cv::CAP_FFMPEG 或 cv::CAP_GSTREAMER。
@param filename、apiPreference、fourcc、fps、frameSize同上


CV_WRAP VideoWriter(const String& filename, int fourcc, double fps, const Size& frameSize,
                    const std::vector<int>& params);
@param `params` 参数允许指定额外的编码器参数,编码为成对`(paramId_1, paramValue_1, paramId_2, paramValue_2, ...)`。


CV_WRAP VideoWriter(const String& filename, int apiPreference, int fourcc, double fps,
                    const Size& frameSize, const std::vector<int>& params);
@param 同上所解释

二、测试例程

cpp 复制代码
void Samples::VideoSaveFunc()
{
  Mat src;
  // use default camera as video source
  VideoCapture cap(0);
  // check if we succeeded
  if (!cap.isOpened()) {
    cerr << "ERROR! Unable to open camera\n";
    //return -1;
  }
  // get one frame from camera to know frame size and type
  cap >> src;
  // check if we succeeded
  if (src.empty()) {
    cerr << "ERROR! blank frame grabbed\n";
    //return -1;
  }
  bool isColor = (src.type() == CV_8UC3);

  //--- INITIALIZE VIDEOWRITER
  VideoWriter writer;
  int codec = VideoWriter::fourcc('M', 'J', 'P', 'G');  // select desired codec (must be available at runtime)
  double fps = 25.0;                          // framerate of the created video stream
  string filename = "./live.avi";             // name of the output video file
  writer.open(filename, codec, fps, src.size(), isColor);
  // check if we succeeded
  if (!writer.isOpened()) {
    cerr << "Could not open the output video file for write\n";
    //return -1;
  }

  //--- GRAB AND WRITE LOOP
  cout << "Writing videofile: " << filename << endl
    << "Press any key to terminate" << endl;
  for (;;)
  {
    // check if we succeeded
    if (!cap.read(src)) {
      cerr << "ERROR! blank frame grabbed\n";
      break;
    }
    // encode the frame into the videofile stream
    writer.write(src);
    // show live and wait for a key with timeout long enough to show images
    imshow("Live", src);
    if (waitKey(5) >= 0)
      break;
  }
  // the videofile will be closed and released automatically in VideoWriter destructor
}

三、结果

会保存在当前程序目录下

相关推荐
H3C-Navigator12 分钟前
【AI高性能网络解析】第一期:面向GPU算力纵向扩展的Scale-up网络技术研究
网络·人工智能·gpu算力·ai-native
Danceful_YJ25 分钟前
18.Kaggle竞赛--使用ResNet-50网络进行树叶分类
人工智能·pytorch·深度学习·卷积神经网络
点云SLAM39 分钟前
TensorFlow 和PyTorch的全方位对比和选择建议
人工智能·pytorch·计算机视觉·tensorflow·深度学习框架·ai部署·ai环境平台
oil欧哟40 分钟前
🧐 AI 批量检查数千份技术文档,如何实现高效文档纠错?
前端·人工智能·ai编程
lishaoan7742 分钟前
用TensorFlow进行逻辑回归(六)
人工智能·tensorflow·逻辑回归
Jamence1 小时前
多模态大语言模型arxiv论文略读(157)
论文阅读·人工智能·语言模型·自然语言处理·论文笔记
DogDaoDao1 小时前
Rembg开源项目全面解析:从原理到实践应用
人工智能·深度学习·开源·github·图像分割·背景检测·rembg
汀、人工智能1 小时前
AI-Compass LLM训练框架生态:整合ms-swift、Unsloth、Megatron-LM等核心框架,涵盖全参数/PEFT训练与分布式优化
人工智能·分布式·sft·swift·大模型训练
ATM0061 小时前
开源AI Agent开发平台Dify源码剖析系列(二)
人工智能·开源·dify·源码剖析
开开心心_Every1 小时前
可增添功能的鼠标右键优化工具
开发语言·pdf·c#·计算机外设·电脑·音视频·symfony