ReproNim / reprostim

Automated capture of audio-visual stimuli into BIDS datasets
5 stars 3 forks source link

Kill ffmpeg gracefully first (SIGINT, then may be SIGTERM) and only then -9 if it refuses to die #92

Closed yarikoptic closed 5 months ago

yarikoptic commented 6 months ago

and verify that according to mediainfo etc we have non truncated video

chagpt (yet to be verified) says

Yes, there is a way to send a signal to FFmpeg to make it close the file gracefully and complete the encoding process without abrupt termination. You can send the SIGINT signal, which is the same as pressing Ctrl+C in the terminal, or the SIGTERM signal. Both signals will allow FFmpeg to finish processing and exit cleanly.

If you are running FFmpeg from a command line and want to terminate it gracefully, you can simply press Ctrl+C. If you are using FFmpeg in a script or another program and want to terminate it programmatically, you can send these signals to the FFmpeg process. Here's how you can do it in different programming environments:

In a Unix/Linux Shell

You can use the kill command followed by the process ID (PID) of the FFmpeg process:

kill -SIGINT <PID>  # Using SIGINT

or

kill -SIGTERM <PID> # Using SIGTERM

In Python

You can use the os module to send the signal:

import os
import signal

os.kill(ffmpeg_process_pid, signal.SIGINT)  # Replace ffmpeg_process_pid with the actual PID

This will ensure that FFmpeg stops recording, finishes writing to the file, and closes the file properly, preserving the integrity of the media data.

vmdocua commented 6 months ago

Just for information, when ffmpeg terminated with command like kill -9, it produces truncated video which can be detected with command like:

mediainfo -i <video_file> | grep "IsTruncated"

And fixed with command like:

ffmpeg -i <video_file> -c copy <video_file_fixed>