发布时间:2022-08-18 18:49
调用opencv库来读取写入视频
语言:C++
视频格式:MP4
#include
#include
#include
#include
#include
#include
int main()
{
cv::VideoCapture capture;
cv::Mat frame;
frame= capture.open("/Users/admin/Desktop/CodeBase/c++_try/1.mp4");// 视频路径
if(!capture.isOpened())
{
printf("can not open ...\n");
return -1;
}
while (capture.read(frame))
{
int width = frame.size().width;
int height = frame.size().height;
cv::imshow("origin", frame);
cv::waitKey(100);
}
capture.release();
return 0;
}
#include
#include
#include
#include
#include
#include
int main()
{
cv::VideoCapture capture;
cv::Mat frame;
frame= capture.open("/Users/admin/Desktop/CodeBase/c++_try/4.mp4");
double fps = capture.get(cv::CAP_PROP_FPS);
cv::VideoWriter writer("/Users/admin/Desktop/CodeBase/c++_try/out.mp4", cv::VideoWriter::fourcc('m','p','4','v'), fps, cv::Size(1280, 720));
if(!capture.isOpened())
{
printf("can not open ...\n");
return -1;
}
while (capture.read(frame))
{
int width = frame.size().width;
int height = frame.size().height;
writer << frame;
cv::imshow("origin", frame);
cv::waitKey(100);
}
capture.release();
return 0;
}
代码中有一句话,定义了一个cv::VideoWriter负责写入视频
cv::VideoWriter writer("/Users/admin/Desktop/CodeBase/c++_try/out.mp4", cv::VideoWriter::fourcc('m','p','4','v'), fps, cv::Size(1280, 720));
参数也好理解:路径、编码格式、帧率、图像大小
我找了一些资料:
cv::VideoWriter::fourcc(‘I’, ‘4’, ‘2’, ‘0’),该参数是YUV编码类型,文件名后缀为.avi
cv::VideoWriter::fourcc(‘P’, ‘I’, ‘M’, ‘I’),该参数是MPEG-1编码类型,文件名后缀为.avi
cv::VideoWriter::fourcc(‘X’, ‘V’, ‘I’, ‘D’),该参数是MPEG-4编码类型,文件名后缀为.avi
cv::VideoWriter::fourcc(‘T’, ‘H’, ‘E’, ‘O’),该参数是Ogg Vorbis,文件名后缀为.ogv
cv::VideoWriter::fourcc(‘F’, ‘L’, ‘V’, ‘1’),该参数是Flash视频,文件名后缀为.flv