我尝试读取视频,翻转帧然后保存它,但输出视频窗口说它已损坏且不会播放。
import cv2 as cv
cap = cv.VideoCapture('wtf.avi')
fourcc = cv.VideoWriter_fourcc(*'DIVX')
out= cv.VideoWriter('output.avi', fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
frame = cv.flip(frame, 0)
# write the flipped frame
out.write(frame)
cv.imshow('frame', frame)
if cv.waitKey(35) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv.destroyAllWindows()
根据我的经验,如果您的操作系统中没有安装编解码器或不同版本的编解码器,那么您尝试保存视频时可能会导致崩溃。
即使这段代码是用C ++编写的,它也可以帮助你:
// Load input video or camera stream
cv::VideoCapture cap(0);
if (!cap.isOpened())
{
std::cout << "!!! Input video could not be opened" << std::endl;
return 1;
}
float fps = cap.get(CV_CAP_PROP_FPS);
int cols = cap.get(CV_CAP_PROP_FRAME_WIDTH);
int rows = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
// Setup output video
cv::VideoWriter video("output.avi", cap.get(CV_CAP_PROP_FOURCC), fps, cv::Size(cols,rows),true)