This might be very late for you but might help someone else. I also went through the same problem recently.
Found out that the video dimensions of the output file should match the dimension of the frame
you are trying to write.
out = cv2.VideoWriter(‘output.avi’,fourcc, 20.0, (W,H))
where W, H are width and height of video frames respectively.
If you are reading the input frame with VideoCapture object to read frames from another video stream as
cap = cv2.VideoCapture(1)
W = cap.get(3)
H = cap.get(4)
If you are reading the frame from another source or method,
W = input_frame.shape
H = input_frame.shape
assuming input_frame is an numpy array.
Hope this helps.