Code: Select all
import io import random import picamera def motion_detected(): # Randomly return True (like a fake motion detection routine) return random.randint(0, 10) == 0 camera = picamera.PiCamera() stream = picamera.PiCameraCircularIO(camera, seconds=20) camera.start_recording(stream, format='h264') try: while True: camera.wait_recording(1) if motion_detected(): # Keep recording for 10 seconds and only then write the # stream to disk camera.wait_recording(10) stream.copy_to('motion.h264') finally: camera.stop_recording()
Yes, I definitely meant I frame. Soz. Brain fart. Good point on the inline headers too. Most of the code to do this is also in the segmented mode in Raspivid which has to split at the right point and use inline headers.6by9 wrote:Er, james, I think you meant that a stream can only be started at an I-frame, plus you'll want inline headers set so as to always get the headers in advance of any I-frame - that is what the raspivid (not raspistill) circular buffer code does.
@Ace Rimmer: How accurate does your 5 second preroll have to be? As above, any H264 stream has to start with an I-Frame, and the number of frames between I-frames is a configurable parameter. The lower the number, the more of your bitrate is consumed by I-frames and the lower the overall quality of the video, but the smaller the segment size that you can chop the stream into.
You could go with a Intra-I-frame period of 1 second (30 frames), and then you would be able to do 5-seconds +1/-0 seconds. To do that your circular buffer would have to store the position of the last 6 header frames, and ditch the data from the oldest 1 second section.
All quite possible, but you need to understand a small amount about how H264 works.
It turns out I still need to record this video in 2 halves so I do need the "copy_to" but I always getAce Rimmer wrote:...... After an hour or so I had it doing what I wanted it to.......