我试图保存一个视频使用开放的简历,其想法是交替视频帧从彩色到灰度为几秒钟。当我保存时,视频只保存彩色帧。
import cv2 def saving(cap): width=cap.get(cv2.CAP_PROP_FRAME_WIDTH) height=cap.get(cv2.CAP_PROP_FRAME_HEIGHT) fps= cap.get(cv2.CAP_PROP_FPS) fourcc=cv2.VideoWriter_fourcc(*'XVID') out=cv2.VideoWriter('Project11.mp4',fourcc,fps,(int(width),int(height))) return (out) def first4seconds(video): c=saving(video) while(video.isOpened()): print(video.get(cv2.CAP_PROP_POS_MSEC)) ret, frame = video.read() if ret==True: if cv2.waitKey(25) & 0xFF == ord('q'): break else: break if(500<=int(video.get(cv2.CAP_PROP_POS_MSEC))<1000 or 2000<=int(video.get(cv2.CAP_PROP_POS_MSEC))<3000): gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) c.write(gray) cv2.imshow('frame',gray) else: c.write(frame) cv2.imshow('frame',frame) video.release() cv2.destroyAllWindows() cap = cv2.VideoCapture('test.mp4') first4seconds(cap)
if(500<=int(video.get(cv2.CAP_PROP_POS_MSEC))<1000 or 2000<=int(video.get(cv2.CAP_PROP_POS_MSEC))<3000): gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) **gray_2=cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR)** **cv2.imshow('frame',gray_2)** **c.write(gray_2)** else: c.write(frame) cv2.imshow('frame',frame)