Home > Back-end >  CV2.VideoWriter save new video with 5s interval
CV2.VideoWriter save new video with 5s interval

Time:01-05

I was trying to save video from an streaming URL for every 1 min duration and the filename will increment. so the cam is streaming I have loaded the video with cv2 and start writing the first file ,after 5s the file will save and another file will create and the frame will write on this file, file name should increment ex. "filename1.avi", "filename2.avi" etc. But the video is save on same file and didn't create another video file.

def show_video_stream(rtsp_address: str):
    """Visualize stream given an rtsp address.

    Args:
        rtsp_address (str): IP address with rtsp protocol.
                            Example: 'rtsp://{user}:{password}@{IP}:{port}'
    """
    current_time_in_second = int(time.time())
    cap = cv2.VideoCapture(rtsp_address)
    frame_width = int(cap.get(3))
    frame_height = int(cap.get(4))

    size = (frame_width, frame_height)
    frame_name = rtsp_address.split('@')[1]
    cv2.namedWindow(frame_name, cv2.WINDOW_NORMAL)
    video_count = 0 #here result is should be dynamic so new file should initiate after every 5s.
    result = cv2.VideoWriter('video_live_feed'   str(video_count)   ".avi",
                             cv2.VideoWriter_fourcc(*'MJPG'),
                             5, size)

    while (True):
        ret, frame = cap.read()
        cv2.imshow(frame_name, frame)
        # save_one_second_file(frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        result.write(frame)
        after_time_in_second = int(time.time())
        print(current_time_in_second)
        print(after_time_in_second)
        #calculating the time of the video for 5 s
        if after_time_in_second - current_time_in_second == 5:
            current_time_in_second = after_time_in_second
            video_count = video_count   1
            print("release korlam")
            result.release()

    cap.release()
    cv2.destroyAllWindows()


if __name__ == "__main__":
    with concurrent.futures.ProcessPoolExecutor() as executor:
        executor.map(show_video_stream, ip_pool)

I have used result.release() to release the write of the cap, and increment the value of the video_count variable.

I want to save video file duration 5s from an streaming video. if the video file is 15s long i will have 3 video file with 5s duration.

CodePudding user response:

At the moment you only open 1 video file, with a name corresponding to video_count == 0, just before the main capture loop. When you detect that 5 seconds have passed you increment the video_count but do not open a new video file.

I advise you to add a function to do that, and call it whenever you need to open a new file:

def open_writer(video_count):
   return cv2.VideoWriter('video_live_feed'   str(video_count)   ".avi",
                             cv2.VideoWriter_fourcc(*'MJPG'),
                             5, size) 

You can use it before the capture loop:

result = open_writer(video_count)

As well as after releasing the previous writer every 5 seconds:

video_count = video_count   1
result.release()
result = open_writer(video_count)   # reopen the writer with a new filename

In addition you can consider to change the 5 seconds condition to:

#------------------------------------------------vv---
if after_time_in_second - current_time_in_second >= 5:

This will make your code more robust to missing the exact second (even though at reasonable capture FPS it will probably happen rarely if at all).

  • Related