import cv2
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
vw = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
cv2.namedWindow('video', cv2.WINDOW_NORMAL)
cv2.resizeWindow('video', 640, 480)
cap = cv2.VideoCapture(1)
if not cap.isOpened():
print("错误:无法打开摄像头")
exit()
print(ord('q'))
while True:
ret, frame = cap.read()
if not ret:
print("错误:无法读取视频帧")
break
frame = cv2.resize(frame, (640, 480))
vw.write(frame)
cv2.imshow('video', frame)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
elif key != -1:
print(f"按键码: {key}")
cap.release()
vw.release()
cv2.destroyAllWindows()