OpenCV save video

Hi I’m learning opencv in python.
I was making object tracking and save video program
But the video was just created and nothing saved
Can someone help me?

here’s my code

import cv2
import numpy as np
import time

print('start in ' + str(time.strftime('%m/%d %H:%M:%S', time.localtime())))
cap = cv2.VideoCapture("rtsp://128.1.1.6/profile2/media.smp")

fgbg = cv2.createBackgroundSubtractorMOG2(varThreshold=200, detectShadows=0)

fps = 60
width = int(cap.get(3))
height = int(cap.get(4))
fcc = cv2.VideoWriter_fourcc(*'DIVX') 
recording = 0

while(1):

    ret, frame = cap.read()
    hms = time.strftime('%H:%M:%S', time.localtime())
    hmss = time.strftime('%H_%M_%S', time.localtime())

    fgmask = fgbg.apply(frame)
    k = cv2.waitKey(1) & 0xff 
    nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(fgmask)
    cv2.putText(frame, str(hms), (0, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255))

    for index, centroid in enumerate(centroids):

        if stats[index][0] == 0 and stats[index][1] == 0:
            continue
        if np.any(np.isnan(centroid)):
            continue

        x, y, width, height, area = stats[index]
        centerX, centerY = int(centroid[0]), int(centroid[1])

        if area > 50: 
            cv2.circle(frame, (centerX, centerY), 1, (0, 255, 0), 2) 
            cv2.rectangle(frame, (x, y), (x + width, y + height), (0, 0, 255))
            cv2.putText(frame, str(area), (centerX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255))

        if area > 50 and recording == 0:
            path = 'multiplesave\\test_' + str(hmss) + '.avi'
            out = cv2.VideoWriter(path, fcc, fps, (width, height))
            print('record start')
            recording = time.time()

    cv2.imshow('mask', fgmask)
    cv2.imshow('frame', frame)

    if recording > 0:
        out.write(frame)
        if time.time() - recording >= 10:
            recording = 0
            out.release()
            print('record end')

cap.release()
out.release()
cv2.destroyAllWindows()