如果保存的帧大小不正确,则打开文件时遇到问题。
如果相机提供的帧大小为IE。
(800, 600)
那你就得用同样大小的字
(800, 600)
或者您必须使用cv来调整框架的大小
(640, 480)
在你保存它之前。
frame = cv2.resize(frame, (640, 480))
全码
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'avc1') #(*'MP42')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
while cap.isOpened():
ret, frame = cap.read()
if ret:
frame = cv2.resize(frame, (640, 480))
out.write(frame)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
GitHub上的示例:
furas/python-examples/cv2/record-file