cv2.Viderowriter_fourcc(*"XIVD")
cv2.VIdeowriter("路径",编码,帧率,(宽度,高度))
import cv2 capture=cv2.VideoCapture(0) width=capture.get(cv2.CAP_PROP_FRAME_WIDTH) height=capture.get(cv2.CAP_PROP_FRAME_HEIGHT) fps=capture.get(cv2.CAP_PROP_FPS) fourcc=cv2.VideoWriter_fourcc(*"XIVD") output_gray=cv2.VideoWriter("C:/Users/PengY/Desktop/video_20220711_155201.mp4",fourcc,int(fps),(int(width),int(height))) while capture.isOpened(): ret,frame=capture.read() if ret: gray_frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) output_gray.write(gray_frame) cv2.imshow("frame",frame) if cv2.waitKey(0)&0XFF==ord("q"): break