Is there a way to write videos crash-safe in python?

Viewed 123

I am using raspberry pi to capture videos 24/7. I have been using picamera's recording function :

import picamera
camera = picamera.PiCamera()
camera.start_recording('output_file.h264')
camera.wait_recording(600)
camera.stop_recording()

This creates .h264 file.

Now, I am trying to do some things with captured frames (object detection etc.) and therefore need to use other methods to write videos, like opencv's VideoWriter.

Here's my main question: When using picamera, it does not care if the process was killed while recording. I.e. if the process was killed after 300 seconds(for any reasons), the output file still has 300 seconds of video, uncorrupted. However, when I used cv2.VideoWriter to save .mp4 file, when it was not properly released, the video file showed corrupted. What method can I use to write Videos that behaves like picamera?

Any other approaches are welcome too.

1 Answers

mp4 files have a structure in them that describes the length of the video, what audio/video streams are contained, and how they are interleaved (chopped into pieces and mixed up).

when you kill the writing process, that header does not get written. the video file has become nearly useless. if you have a single stream, it may be recoverable.

a simple ".h264" file does not contain such a header. it's a single contiguous stream of H.264. that can be decoded with no issues.

you can try telling VideoWriter to use a different container format. try ".ts" instead of ".mp4". you can separately tell it the FOURCC of your desired codec (MJPG maybe).

Related