平台vs2015 opencv4.3.0 网上用的fourcc('M','J','P','G')旧版本的opencv会报错或写入不进,可能是版本不一样,下面是采集视频每一帧保存到另外一个mp4文件里,可以扩展其他项目视频图像处理后保存效果视频
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
void main()
{
VideoCapture capture("5.mp4");
double fps = capture.get(CAP_PROP_FPS);
int width = capture.get(CAP_PROP_FRAME_WIDTH);
int height = capture.get(CAP_PROP_FRAME_HEIGHT);
VideoWriter writer("1.mp4", VideoWriter::fourcc('m','p','4','v'), 25.0, Size(width, height));
Mat frame;
while (capture.isOpened())
{
capture >> frame;
if (frame.empty())
break;
writer << frame;
imshow("video", frame);
if (waitKey(20) == 27)
{
break;
}
}
}
图片序列保存到视频里 很简单
#define _CRT_SECURE_NO_WARNINGS //可以去除sprintf的警告
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
void main()
{
VideoWriter writer("1.mp4", VideoWriter::fourcc('m', 'p', '4', 'v'), 40.0, Size(1280, 720));//图片大小和帧率根据图片像素来调,这里设置成了固定值
vector<String> fn;
char filename[100];
glob("E://Videodata//Savebrid//*.bmp", fn, false);
size_t count = fn.size(); //number of png files in images folder
for (size_t i = 0; i < count; i++)
{
sprintf(filename, "E:/Videodata/Savebrid/%d.bmp", i + 1);//图片序列编号从1.bmp开始
Mat image1 = imread(filename);
writer << image1;
imshow("image", image1);
if (waitKey(20) == 27)
{
break;
}
}
}