知识点:

1、VideoCapture 视频文件读取、摄像头读取、视频流读取
2、获取视频的相关属性
- CAP_PROP_FRAME_HEIGHT // 高
- CAP_PROP_FRAME_WIDTH // 宽
- CAP_PROP_FRAME_COUNT // 总帧数
- CAP_PROP_FPS // 帧率
3、VideoWriter 视频写出、文件保存

注意事项:

OpenCV 不支持音频编码与解码保存,不是一个音视频处理的库!主要是分析与解析视频内容。保存文件最大支持单个文件为2G。

C++代码

  1. #include <opencv2/opencv.hpp>
  2. #include <iostream>
  3. using namespace std;
  4. using namespace cv;
  5. void day12() {
  6. VideoCapture capture;
  7. // 打开本地的视频文件
  8. //capture.open("G:\\opencvTest\\video.mp4");
  9. // 打开摄像头,0是电脑自带的摄像头,序号依次递增为外接摄像头
  10. capture.open(1);
  11. if (!capture.isOpened()) {
  12. cout << "could not open this capture.." << endl;
  13. }
  14. int width = static_cast<int>(capture.get(CAP_PROP_FRAME_WIDTH));
  15. int height = static_cast<int>(capture.get(CAP_PROP_FRAME_HEIGHT));
  16. int count = static_cast<int>(capture.get(CAP_PROP_FRAME_COUNT));
  17. int fps = static_cast<int>(capture.get(CAP_PROP_FPS));
  18. cout << "分辨率:(" << width << "x" << height << ") " << endl;
  19. cout << "总帧数:" << count << endl;
  20. cout << "帧率:" << fps << endl;
  21. int type = static_cast<int>(capture.get(CAP_PROP_FOURCC));
  22. VideoWriter writer("G:\\opencvTest\\video.mp4", type, fps, Size(width, height), true);
  23. Mat frame;
  24. while (capture.read(frame)) {
  25. imshow("capture_video", frame);
  26. writer.write(frame);
  27. // 监听键盘事件,按Esc退出
  28. char c = waitKey(50);
  29. if (c == 27) {
  30. break;
  31. }
  32. }
  33. // 释放资源
  34. writer.release();
  35. capture.release();
  36. }

Python代码

Javascript代码