Home > Software engineering >  Save edited video with python CV2
Save edited video with python CV2

Time:03-20

I'm using the following code to create a video with rectangles in all its frames. However, the video is not being saved after it is created. How can I edit the code to have the video save in one of my folders.

 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()

CodePudding user response:

Code creates file for me when I set manually codec instead of -1

#fourcc = -1
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
output = cv2.VideoWriter("output.mp4", fourcc, fps,(int(width),int(height)))

I never saw code with -1. Maybe it doesn't work.

  • Related