赞
踩
调用opencv库来读取写入视频
语言:C++
视频格式:MP4
#include <iostream> #include <cstdlib> #include <ctime> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc.hpp> 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 <iostream> #include <cstdlib> #include <ctime> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc.hpp> 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
我还看到有人是这么写的
cv::VideoWriter::fourcc(‘M’, ‘J’, ‘P’, ‘G’),文件名后缀是.avi
在执行代码时,提示过几次错误
OpenCV: FFMPEG: tag 0x00000898/'???' is not found (format 'avi / AVI (Audio Video Interleaved)
和
OpenCV: FFMPEG: tag 0x47504a4d/'MJPG' is not supported with codec id 7 and format 'mp4 / MP4
最后排查,都是上面提到的视频编码格式的问题cv::VideoWriter::fourcc
,写代码时请注意你要的视频后缀需要和编码格式对应
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。