""" 保存視頻 保存視頻接口:<VideoWriter object> = cv.VideoWriter( filename, fourcc, fps, frameSize[, isColor] ) 參數說明: filename:要保存的視頻名稱和路徑(data/outVideo.mp4) fourcc:視頻編碼器 fps:幀率 framesize:幀數大小 isColor:True彩色,False灰度視頻, 默認True """ import cv2 cap = cv2.VideoCapture('data/1.mp4') # 要讀取的視頻 0、1 本地相機或外接相機 # 建立VideoWriter類對象 fourcc = cv2.VideoWriter_fourcc(*'XVID') fps = cap.get(cv2.CAP_PROP_FPS) size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) out = cv2.VideoWriter('data/outVideo.mp4', fourcc, fps, size) # 讀取視頻流 while cap.isOpened(): ret, frame = cap.read() # 獲取一幀圖像 if ret: frame = cv2.flip(frame, 1) # 調整方向,可不寫 out.write(frame) # 寫入視頻對象 # 顯示讀取視頻 cv2.imshow('frame', frame) # q鍵關閉 if cv2.waitKey(1) & 0xFF == ord('q'): break else: break # 關閉流 cap.release() out.release() cv2.destroyAllWindows()