fanfuhan OpenCV 教學012 ~ OpenCV的-012視頻讀寫[抓取影片畫面大小、影片拷貝(複製)]
資料來源: https://fanfuhan.github.io/
https://fanfuhan.github.io/2019/03/26/opencv-012/
GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV
C++
#include#include using namespace std; using namespace cv; /* * 视频读写 */ int main() { // 打开摄像头 // VideoCapture capture(0); // 打开视频文件 VideoCapture capture; capture.open("../images/vtest.avi"); if (!capture.isOpened()) { cout << "could not load video.." << endl; return -1; } Size S = Size((int) capture.get(CAP_PROP_FRAME_WIDTH), (int) capture.get(CAP_PROP_FRAME_HEIGHT)); int fps = capture.get(CAP_PROP_FPS); cout << "capture fps: " << fps << endl; VideoWriter writer("D:/test.mp4", cv::VideoWriter::fourcc('D', 'I','V','X'), fps, S, true); Mat frame; while(capture.read(frame)){ imshow("input", frame); writer.write(frame); char c = waitKey(50); if(c == 27){ break; } } capture.release(); writer.release(); waitKey(0); return 0; }
Python
import cv2 as cv
import numpy as np
capture = cv.VideoCapture("D:/vcprojects/images/768x576.avi")
# capture = cv.VideoCapture(0) 打开摄像头
height = capture.get(cv.CAP_PROP_FRAME_HEIGHT)
width = capture.get(cv.CAP_PROP_FRAME_WIDTH)
count = capture.get(cv.CAP_PROP_FRAME_COUNT)
fps = capture.get(cv.CAP_PROP_FPS)
print(height, width, count, fps)
out = cv.VideoWriter("D:/test.mp4", cv.VideoWriter_fourcc('D', 'I', 'V', 'X'), 15,
(np.int(width), np.int(height)), True)
while True:
ret, frame = capture.read()
if ret is True:
cv.imshow("video-input", frame)
out.write(frame)
c = cv.waitKey(50)
if c == 27: # ESC
break
else:
break
capture.release()
out.release()