Gilad Gilad - 6 months ago 20
Python Question

RPi2 - Taking images while recording video

In an effort to make a simple surveillance camera I want to record video as long as there is movement. For that I need to analyze an images every second or so. I would like to start capturing video and stop only after deciding that there is no more movement, for that I need to capture an image while video is recording.

What is the easiest way to do this on RPi2-Python3

Answer

Picamera has a 'built in' solution for this

import picamera

with picamera.PiCamera() as camera:
    camera.resolution = (800, 600)
    camera.start_preview()
    camera.start_recording('foo.h264')
    camera.wait_recording(10)
    camera.capture('foo.jpg', use_video_port=True)
    camera.wait_recording(10)
    camera.stop_recording()

taken from: http://picamera.readthedocs.io/en/release-1.10/recipes2.html#capturing-images-whilst-recording