Send EoS to save camera feed

I work with DeepStream 5.0 on nano jetson. I would like to save about 30 seconds of the camera feed into a file.
I have the pipeline working and close it with ctrl+c. However this cause the file to be unplayable.
I saw I was supposed to send an EoS signal to the filesink before closing. Therefore, I tried the following:

 /* Wait till pipeline encounters an error or EOS */
 g_print ("Running...\n");
 g_main_loop_run (loop);

 gst_element_send_event (sink, gst_event_new_eos ());

It is however not working. How can I make it work?
Thanks in advance,

You can not send EOS in this place. g_main_loop_run() is the pipeline loop, or you can think it is a wait, it will not return until the pipeline stops. So your “gst_element_send_event” is evoked after the pipeline finished, it is of no use.
Please refer to

I used the solution in to exit the main loop and following that sent an EOS signal to the filesink and it now works well!
Thanks for your help,

Google helps!!