添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
首发于 OpenCV

VideoWriter

OpenCV提供了写入视频的接口类VideoWriter,VideoWriter是向文件中以指定的编码格式将每一帧图片写入到视频中。VideoWriter提供了常用的三种构造函数:

VideoWriter::VideoWriter()

VideoWriter::VideoWriter(const String &filename, int fourcc , double fps, Size frameSize, bool isColor=true)

VideoWriter::VideoWriter(const String &filename, int apiPreference, int fourcc , double fps, Size frameSize, bool isColor=true)

说明:

第一个为构造参数为默认构造参数

第二个和第三个的参数说明如下:

filename : 输出视频文件的路径名称

fourcc: 字符类型的编码,表示用于编码视频文件的编码器。其中 VideoWriter::fourcc(‘P’,’I’,’M’,’1’)表示MPEG-1 编码文件扩展名为avi; VideoWriter::fourcc('X','V','I','D')表示MPEG-4编码文件扩展名为avi; VideoWriter::fourcc('X',’2','6','4') 表示MPEG-4编码文件扩展名为mp4;

VideoWriter::fourcc('I',’4','2','0') 表示YUV编码,文件扩展名为avi;

VideoWriter::fourcc('M',’P','4','V') 表示旧的MPEG-4编码,文件扩展名为avi;

VideoWriter::fourcc('T',’H','E','O') 表示使用ogg vorbis,文件扩展名为ogv;

VideoWriter::fourcc('F','L','V','1') 表示flash video,文件扩展名为flv;

fps: 表示帧率

frameSize : 表示每一帧图像的大小

isColor : 灰度图像或者是彩色图像(仅仅在windows上支持)

apiPreference: 使用指定的API,例如可以使用cv::CAP_FFMPEG 或者 cv::CAP_GSTREAMER等。

VideoWriter类还提供了write方法并且使用了重载运算符<<写入视频的每一帧。其函数原型如下:

VideoWriter& VideoWriter::operator<<(Mat& frame);

void VideoWriter::write(const Mat &frame);

VideoCapture常用的一些函数如下:

函数 功能
VideoWriter::isOpened() Returns true if video writer has been initialized already
VideoWriter::getBackednName() Returns used backend API name
VideoWriter::open(const String &filename, int fourcc, double fps, Size frameSize, bool isColor=true);VideoWriter::open(const String &filename,int apiPreference,int fourcc,double fps,Size frameSize,bool isColor=true); Initializes or reinitializes video writer
VideoWriter::release() Closes the video writer
VideoWriter::get(int propId); Returns the specified VideoWriter property
VideoWriter::set(int propId,double value); Sets a property in the VideoWriter

示例代码如下:

#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <opencv2/opencv.hpp>
using std::sin;
using std::cos;
//使用CommandLineParser对输入的参数进行分析,获取输入的图片路径
std::string GetFileName(int argc,char* argv[])
    argc : the size of argv[]
    argv : the parameters of comdline
    const char* key = {
    "{help h usage? || usage information}"
    "{@picture || input picture}"
     cv::CommandLineParser parser(argc,argv,key);
    if (parser.has("help"))
        parser.printMessage();
        exit(0);
    //如果没有视频文件
    if (!parser.check())
        parser.printErrors();
        exit(-1);
    std::string fileName = parser.get<std::string>(0);
    return fileName;
int main(int argc,char* argv[])
    std::string outPut = "output.avi";
    std::string VideoFile = GetFileName(argc,argv);
    cv::VideoCapture cap(VideoFile);
    if (!cap.isOpened())
        std::cerr << "fail to open  "<< VideoFile << std::endl;
        return -1;
    cv::VideoWriter outputVideo;
    cv::Size videoSize = cv::Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH),(int)cap.get(CV_CAP_PROP_FRAME_HEIGHT));
    int fourcc = cv::VideoWriter::fourcc('M','J','P','G'); //flash video
    double fps = cap.get(CV_CAP_PROP_FPS);
    outputVideo.open(outPut,fourcc,25.0,videoSize,true);
    if (!outputVideo.isOpened())
        std::cerr << "fail to initializer the video writer" << std::endl;
        return -1;
    std::vector<cv::Mat> bgr; //rgb色彩通道
    cv::Mat frame;
    cv::Mat dstImage;
    while(true)
        cap >> frame;
        //分离颜色通道
       if (!frame.empty())
           cv::imshow("inputVideo",frame);
           //分离色彩通道
           cv::split(frame,bgr);
           for(int i=0;i<3;i++)
               int size = bgr.size();
               //提取绿色通道
               if (i!=1)
                   bgr[i] = cv::Mat::zeros(videoSize,bgr[1].type());
                cv::merge(bgr,dstImage);
           cv::imshow("dst",dstImage);
           outputVideo << dstImage;
           break;
       char c = cv::waitKey(30);
       if( c== 27)
            break;