Encoding time-based video chunks from encoded frames from NVEncoder recorded from camera

I am looking to create time-based video chunks encoded from NvVideoEncoder on Jetson TX2, the encoder gets frames from ethernet camera, my program runs fine while recording single video however when i am recording the second chunk of video by reinitialising the encoder the program crashes because the encoder thread isn’t finished yet. Please could someone guide me or any suggestions on my approach? Thanks.

We have the option in 01_video_encode:

-s <loop-count>       Stress test [Default = 1]

You may enable it and check how it terminate and re-initialize NvVideoEncoder.