How to record playable video files in gstreamer even if recording is interrupted unexpectedly (e.g. power disconnects)?

I have been recording video streams on my Jetson Xavier NX using a simple gstreamer pipeline such as this

gst-launch-1.0 -v \
nvarguscamerasrc sensor-id=0 ! \
'video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)NV12, framerate=(fraction)30/1' ! \
nvv4l2h265enc ! 'video/x-h265, stream-format=(string)byte-stream' ! \
h265parse ! qtmux ! filesink location=video.mp4 -e

All is working if the recording is interrupted by keyboard interrupt Ctrl + C , but if the recording is interrupted unexpectedly (e.g. power gets disconnected) the resulting file has no playable stream, even if the file size is correct.

I know that mp4 recording needs to be stopped properly otherwise it won’t have the necessary information at the end of the file, but I was wondering if there was any other gstreamer pipelines or final file formats that would allow for an H265 encoded video file to be playable even if the recording is ended unexpectedly.

It would be good even if the recorded file needed to be converted before being playable (e.g. with ffmpeg), as long as the information can be recovered without having to go through non-free mp4 recovery tools.

For this use-case, suggest try matroskamux to get mkv files or mpegtsmux to get ts files.

I just tried matroskamux and works as expected, thanks a lot!