raspberrypi / picamera2

New libcamera based python library
BSD 2-Clause "Simplified" License
841 stars 179 forks source link

[HOW-TO] use the CircularBuffer with timelapse #782

Open blackTay opened 1 year ago

blackTay commented 1 year ago

I want to use the circular buffer with time lapse. Using the code base from

examples/capture_timelapse_video.py.

I have simply added the argument FrameDurationLimits as described in the documentation to get timelapse videos, here 10 FPS:

video_config = picam2.create_video_configuration(main={"size": (1280, 720), "format": "RGB888"}, lores={
                                                 "size": lsize, "format": "YUV420"},controls={"FrameDurationLimits": (100000, 100000)})

However, when playing the output file, I get "jumps" in the video stream and a few seconds in the middle of the video are just cut off.

A minimal working example based on examples/capture_timelapse_video.py but without the video processing is below.

I suspect that this might be an issue with the file output. I tried to solve this using the TimelapseOutput class from examples/capture_timelapse_video.py, however I did not succeed.

Many thanks for any hints.

import time
from picamera2 import Picamera2
from picamera2.encoders import H264Encoder
from picamera2.outputs import CircularOutput

lsize = (320, 240)
picam2 = Picamera2()
video_config = picam2.create_video_configuration(main={"size": (1280, 720), "format": "RGB888"}, lores={
                                                 "size": lsize, "format": "YUV420"},controls={"FrameDurationLimits": (100000, 100000)})
picam2.configure(video_config)
picam2.start_preview()
encoder = H264Encoder(1000000, repeat=True)
encoder.output = CircularOutput()
picam2.encoder = encoder
picam2.start()
picam2.start_encoder()

w, h = lsize
prev = None
encoding = False
ltime = 0

print("prerec ready")

time.sleep(5)
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
print("Recording starts now")

encoder.output.fileoutput = "test.h264"
encoder.output.start()
print("Starting recording")
time.sleep(10)
encoder.output.stop()
print("Recording stopped")
picam2.stop_encoder()
davidplowman commented 1 year ago

Could you just say how you're playing the video back? Thanks.