Send EoS to save camera feed

Hello,
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);

 g_usleep(300000000);
 gst_element_send_event (sink, gst_event_new_eos ());
 gst_bus_poll(bus,GST_MESSAGE_EOS,GST_CLOCK_TIME_NONE);

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

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 GLib – 2.0
GLib Event Loop

I used the solution in GLib Event Loop 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!!