cap = cv2.VideoCapture('TestVideo.mp4')
count = 0
while cap.isOpened():
    ret, frame = cap.read()
if not ret:
    break
else:
    cv2.imshow('frame', frame)
name = 'rec_frame' + str(count) + '.jpg'
cv2.imwrite(os.path.join(dirname, name), frame)
count += 1
if cv2.waitKey(20) & 255 == ord('q'):
    break
cap.release()
cv2.destroyAllWindows()
Read Frames from RTSP Stream in Python