一、opencv保存视频打不开
保存的视频打不开可能是因为文件编码格式不对,或者版本不兼容等原因。
检查文件编码格式,如果视频保存为AVI格式,在保存的时候应该指定编码器,并确保选择的编码器在本地系统上可用。如果视频格式为mp4,需要检查mp4格式的插件是否已经安装好,并且扩展名保存为.mp4才能兼容。
示例代码:
``` #include
#include "opencv2/core.hpp" #include "opencv2/highgui.hpp" #include "opencv2/videoio.hpp" using namespace std; using namespace cv; int main() { VideoCapture cap(0); if (!cap.isOpened()) { return 0; } int fourcc = VideoWriter::fourcc('X', 'V', 'I', 'D'); int fps = 30; Size size(int(cap.get(CAP_PROP_FRAME_WIDTH)), int(cap.get(CAP_PROP_FRAME_HEIGHT))); VideoWriter writer("output.avi", fourcc, fps, size); if (!writer.isOpened()) { return 0; } Mat frame; while (true) { cap >> frame; if (frame.empty()) { break; } writer.write(frame); imshow("frame", frame); if (waitKey(10) == 27) { break; } } cap.release(); writer.release(); destroyAllWindows(); return 0; } ```
二、opencv保存视频mp4
mp4是一种常见的视频格式,OpenCV同样可以保存为mp4格式。mp4格式需要保证编码器可用,推荐使用H264编码器。
示例代码:
``` #include
#include "opencv2/core.hpp" #include "opencv2/highgui.hpp" #include "opencv2/videoio.hpp" using namespace std; using namespace cv; int main() { VideoCapture cap(0); if (!cap.isOpened()) { return 0; } int fourcc = VideoWriter::fourcc('a', 'v', 'c', '1'); int fps = 30; Size size(int(cap.get(CAP_PROP_FRAME_WIDTH)), int(cap.get(CAP_PROP_FRAME_HEIGHT))); VideoWriter writer("output.mp4", fourcc, fps, size, true); if (!writer.isOpened()) { return 0; } Mat frame; while (true) { cap >> frame; if (frame.empty()) { break; } writer.write(frame); imshow("frame", frame); if (waitKey(10) == 27) { break; } } cap.release(); writer.release(); destroyAllWindows(); return 0; } ```
三、opencv保存视频变快
保存视频变快可以通过控制帧率调整,适当提高帧率。
示例代码:
``` #include
#include "opencv2/core.hpp" #include "opencv2/highgui.hpp" #include "opencv2/videoio.hpp" using namespace std; using namespace cv; int main() { VideoCapture cap(0); if (!cap.isOpened()) { return 0; } int fourcc = VideoWriter::fourcc('X', 'V', 'I', 'D'); int fps = 60; Size size(int(cap.get(CAP_PROP_FRAME_WIDTH)), int(cap.get(CAP_PROP_FRAME_HEIGHT))); VideoWriter writer("output.avi", fourcc, fps, size); if (!writer.isOpened()) { return 0; } Mat frame; while (true) { cap >> frame; if (frame.empty()) { break; } writer.write(frame); imshow("frame", frame); if (waitKey(1) == 27) { break; } } cap.release(); writer.release(); destroyAllWindows(); return 0; } ```
四、opencv保存视频每一帧
可以通过遍历每一帧,保存每一帧的方式将视频保存成一个序列帧。
示例代码:
``` #include
#include
#include "opencv2/core.hpp" #include "opencv2/highgui.hpp" #include "opencv2/videoio.hpp" using namespace std; using namespace cv; int main() { VideoCapture cap(0); if (!cap.isOpened()) { return 0; } int frameCount = 0; Mat frame; while (true) { cap >> frame; if (frame.empty()) { break; } stringstream ss; ss << "frame_" << frameCount << ".png"; imwrite(ss.str(), frame); frameCount++; imshow("frame", frame); if (waitKey(10) == 27) { break; } } cap.release(); destroyAllWindows(); return 0; } ```
五、Opencv保存视频常见问题
问题一:保存视频过大
可以通过调整帧率和分辨率降低视频的大小。
示例代码:
``` #include
#include "opencv2/core.hpp" #include "opencv2/highgui.hpp" #include "opencv2/videoio.hpp" using namespace std; using namespace cv; int main() { VideoCapture cap(0); if (!cap.isOpened()) { return 0; } int fourcc = VideoWriter::fourcc('X', 'V', 'I', 'D'); int fps = 15; Size size(int(cap.get(CAP_PROP_FRAME_WIDTH)) / 2, int(cap.get(CAP_PROP_FRAME_HEIGHT)) / 2); VideoWriter writer("output.avi", fourcc, fps, size); if (!writer.isOpened()) { return 0; } Mat frame; while (true) { cap >> frame; if (frame.empty()) { break; } resize(frame, frame, size); writer.write(frame); imshow("frame", frame); if (waitKey(10) == 27) { break; } } cap.release(); writer.release(); destroyAllWindows(); return 0; } ```
问题二:保存的视频显示是蓝色的
保存的视频显示成蓝色的场景可能是因为视频的BGR三通道颜色顺序和某些视频播放器不一致导致的,建议使用其他播放器尝试。
六、opencv保存视频只有6kb
视频只有6kb可能是保存格式不正确导致的。
示例代码:
``` #include
#include "opencv2/core.hpp" #include "opencv2/highgui.hpp" #include "opencv2/videoio.hpp" using namespace std; using namespace cv; int main() { VideoCapture cap(0); if (!cap.isOpened()) { return 0; } int fourcc = VideoWriter::fourcc('X', 'V', 'I', 'D'); int fps = 30; Size size(int(cap.get(CAP_PROP_FRAME_WIDTH)), int(cap.get(CAP_PROP_FRAME_HEIGHT))); VideoWriter writer("output.avi", fourcc, fps, size); if (!writer.isOpened()) { return 0; } Mat frame; while (true) { cap >> frame; if (frame.empty()) { break; } writer.write(frame); imshow("frame", frame); if (waitKey(10) == 27) { break; } } int frameCount = writer.get(CAP_PROP_FRAME_COUNT); if (frameCount == 0) { return 0; } return 0; } ```
七、opencv保存视频python
OpenCV同样可以使用Python进行视频保存,类似的API接口,只是函数名不同。
示例代码:
```Python import cv2 cap = cv2.VideoCapture(0) fourcc = cv2.VideoWriter_fourcc(*'XVID') fps = 30 size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) out = cv2.VideoWriter('output.avi', fourcc, fps, size) while True: ret, frame = cap.read() if ret: out.write(frame) cv2.imshow('frame', frame) if cv2.waitKey(1) == 27: break cap.release() out.release() cv2.destroyAllWindows() ```
八、Opencv保存视频 buffer
为了避免保存视频时出现buffer溢出,可以设置参数,提高缓冲区大小。
示例代码:
``` #include
#include "opencv2/core.hpp" #include "opencv2/highgui.hpp" #include "opencv2/videoio.hpp" using namespace std; using namespace cv; int main() { VideoCapture cap(0); if (!cap.isOpened()) { return 0; } int fourcc = VideoWriter::fourcc('X', 'V', 'I', 'D'); int fps = 30; Size size(int(cap.get(CAP_PROP_FRAME_WIDTH)), int(cap.get(CAP_PROP_FRAME_HEIGHT))); VideoWriter writer("output.avi", fourcc, fps, size, true); //需要增加的代码 int buffersize = 50; int framesize = writer.get(CAP_PROP_FRAME_WIDTH) * writer.get(CAP_PROP_FRAME_HEIGHT); int bufferlength = buffersize * framesize; writer.set(CAP_PROP_BUFFERSIZE, bufferlength); //增加代码结束 if (!writer.isOpened()) { return 0; } Mat frame; while (true) { cap >> frame; if (frame.empty()) { break; } writer.write(frame); imshow("frame", frame); if (waitKey(10) == 27) { break; } } cap.release(); writer.release(); destroyAllWindows(); return 0; } ```
九、opencv保存视频后大小只有1kb
还是因为保存格式不正确导致的,可以尝试更换编码器。
示例代码:
``` #include
#include "opencv2/core.hpp" #include "opencv2/highgui.hpp" #include "opencv2/videoio.hpp" using namespace std; using namespace cv; int main() { VideoCapture cap(0); if (!cap.isOpened()) { return 0; } int fourcc = VideoWriter::fourcc('M', 'J', 'P', 'G'); //换成MJPEG编码器 int fps = 30; Size size(int(cap.get(CAP_PROP_FRAME_WIDTH)), int(cap.get(CAP_PROP_FRAME_HEIGHT))); VideoWriter writer("output.avi", fourcc, fps, size); if (!writer.isOpened()) { return 0; } Mat frame; while (true) { cap >> frame; if (frame.empty()) { break; } writer.write(frame); imshow("frame", frame); if (waitKey(10) == 27) { break; } } cap.release(); writer.release(); destroyAllWindows(); return 0; } ```