使用 python CV2 保存编辑后的视频

Save edited video with python CV2

我正在使用以下代码创建一个在所有帧中都包含矩形的视频。但是,视频创建后未保存。如何编辑代码以将视频保存在我的文件夹之一中。

 import cv2

 #Reads the video and collects it information
 cap = cv2.VideoCapture('20150326_060700_062957_themis_rank_fisheye.mp4')
 fps = cap.get(cv2.CAP_PROP_FPS)
 width  = cap.get(cv2.CAP_PROP_FRAME_WIDTH)   # float
 height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # float
 output = cv2.VideoWriter("output.mp4", -1, fps,(int(width),int(height)))

while (cap.isOpened()):
    ret, frame = cap.read()

       if (ret):

       # Adds the rectangles in all frames
        rect1 = cv2.rectangle(frame, (135, 510), (200,450), (255, 0, 0), 1)
        rect2 = cv2.rectangle(frame, (365, 365), (430, 430), (255, 0, 0),1)
    
        # writing the new frame in output
        output.write(frame)
        cv2.imshow("output", frame)
        if cv2.waitKey(1) & 0xFF == ord('s'):
          break
  else:
     break
cv2.destroyAllWindows()  
output.release()  
cap.release()

当我手动设置编解码器而不是 -1

时,代码会为我创建文件
#fourcc = -1
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
output = cv2.VideoWriter("output.mp4", fourcc, fps,(int(width),int(height)))

我从未见过 -1 的代码。可能不行。