If you don't need any custom logic, then you could install motion
(sudo apt install motion
), tweak its config file at /etc/motion/motion.conf
, and have it record videos in your desired format (mp4, avi, swf, flv, etc..) when a 'motion event' occurs.
If you want movies to be saved to files, then make sure you set:
ffmpeg_output_movies on
ffmpeg_video_codec mp4 # can also use other formats
stream_localhost off # otherwise only localhost connections are allowed
output_pictures off
It includes an HTTP MJPG streaming server which you can watch from your browser or hook to specialized webcam apps (see stream_port
in the config).
You can run your own scripts or commands on certain event hooks. The useful ones are:
on_event_start
— fires when motion is detectedon_movie_end
— fires after a moviesmovie has been created, receives the path of the created video file, meaning you can e.g. automatically email it to yourself or send to a telegram chat.on_camera_lost
— a hook you can use to take action in case something fails, i.e. restart themotion
service or reboot your rpi.
Getting it to work the way you want involves getting your config right, and googling will return enough results for different scenarios, e.g. like this one.