Tuesday, January 11, 2022

Linux Security Cameras

Security Camera Management Programs

There are two decent Linux programs for managing security cameras: Motion and ZoneMinder.

For test purposes, I set a camera up inside my living room.  Once working, I'll install two outside, to watch the driveway and backyard.

 Streaming Camera with Fish Eye Lens

Motion is the simpler one of the two programs, but with almost 300 configurable options, it can do anything worth doing.  It works just as well as ZoneMinder, but it simply doesn't do things that are not needed.  For example, Motion supports a database, but Linux filesystem storage is usually good enough.  It doesn't have a fancy GUI, since it is intended to run on a server, stashed on a shelf in a dark corner.  There is a GUI which can be accessed with a web browser locally, or over the LAN, but I never used it.  One can also view real-time streams with a web browser.  Notifications can be sent and files uploaded to another machine with external programs such as mutt, wput and others.

While one can watch a TP Link security camera with a cell phone App,  I want to monitor what is going on when not at home, without spending money on a subscription service.  The simplest way to do this, is to use a free email account as an archive. Wherever I travel, I can log into gmail with a web browser and look at the stored security videos, to confirm that my house hasn't burned down and that there isn't an old Baba Yaga with a black cat and a white goat staying there.

In this guide, I describe how to configure Motion to handle a TP-Link C100 RTSP streaming camera and email a short video clip whenever motion is detected, using mutt with a gmail account.

Install Motion and Mutt

Mutt is a simple command line mail client which is fully supported and updated by Ubuntu Linux.  It provides the ability to send attachments using a free mail account from gmail, yahoo or similar service.  The parameter syntax of mutt is a little wonky - do yourself a favour and read the man page at least 3 times.

  • $ sudo su -
  • password
  • # apt install motion mutt ffmpeg 

Streaming Cameras

First of all, get the TP-Link camera to work.  You need to install an App on your cell phone to do that.   Using this App, create a user account with a password to use for the RTSP stream login.  With the App, you can also find the camera IP address - or look in your router DHCP list for it.

Use ffplay to verify that you can log in and play the camera streams:

  • $ ffplay rtsp://USERNAME:PASSWORD@IP.ADD.RE.SS/stream1

The above is the EXACT string that you need to use later in the camera configuration.

Security Considerations

For improved security, I configured mutt and motion to run under my user account on an Ubuntu Linux server.

Configure Mutt

  • $ cd
  • $ mkdir .mutt

Create a new gmail account for the security system, then go to the account security settings and set Support for Less Secure Apps ON.  Otherwise mutt will be blocked by Google. Simply send the attachments to this gmail address and leave it all there. Don't clog up your regular email account with security cam spam.  When Google eventually complains that the mailbox is full (you get 15GB for free), you can simply select all and delete it, then let it fill up again.

Create a file called mutrc.

Replace USERNAME, REAL NAME and PASSWORD with the exact details used when creating the gmail account. Put SINGLE QUOTES around the password to prevent the expansion of any $ signs in the password.

set from = "USERNAME@gmail.com"
set realname = "REAL NAME"
set imap_user = "
USERNAME@gmail.com"
set imap_pass = 'PASSWORD'
set smtp_url = "smtps://
USERNAME@smtp.gmail.com"
set smtp_pass = '
PASSWORD'
set folder = "imaps://imap.gmail.com/"
set spoolfile = "+INBOX"
set postponed="+[Gmail]/Drafts"
set trash = "+[Gmail]/Trash"

Configure Motion

  • $ cd 
  • $ mkdir .motion
  • $ cd .motion
  • $ cp /etc/motion/motion.conf .
  • $ cp /etc/motion/camera* .

Motion.conf

Make a file called motion.conf.  In the below example change USERNAME to suit.

# Rename this distribution example file to motion.conf
#
# This config file was generated by motion 4.3.2
# Documentation:  /usr/share/doc/motion/motion_guide.html
#
# This file contains only the basic configuration options to get a
# system working.  There are many more options available.  Please
# consult the documentation for the complete list of all options.
#

############################################################
# System control configuration parameters
############################################################

# Start in daemon (background) mode and release terminal.
daemon off

# Start in Setup-Mode, daemon disabled.
setup_mode off

# File to store the process ID.
pid_file /home/USERNAME/.motion/pid

# File to write logs messages into.  If not defined stderr and syslog is used.
log_file /home/USERNAME/.motion/motion.log

# Level of log messages [1..9] (EMG, ALR, CRT, ERR, WRN, NTC, INF, DBG, ALL).
log_level 6

# Target directory for pictures, snapshots and movies
target_dir /home/USERNAME/Videos

# Video device (e.g. /dev/video0) to be used for capturing.
;videodevice /dev/video0

# Parameters to control video device.  See motion_guide.html
; vid_control_params value

# The full URL of the network camera stream.
; netcam_url value

# Name of mmal camera (e.g. vc.ril.camera for pi camera).
; mmalcam_name value

# Camera control parameters (see raspivid/raspistill tool documentation)
; mmalcam_control_params value

############################################################
# Image Processing configuration parameters
############################################################

# Image width in pixels.
width 1920

# Image height in pixels.
height 1080

# Maximum number of frames to be captured per second.
framerate 2

# Text to be overlayed in the lower left corner of images
text_left "CAM1"

# Text to be overlayed in the lower right corner of images.
text_right %Y-%m-%d\n%T-%q

############################################################
# Motion detection configuration parameters
############################################################

# Always save pictures and movies even if there was no motion.
emulate_motion off

# Threshold for number of changed pixels that triggers motion.
;threshold 1500 

threshold 10000

# Noise threshold for the motion detection.
noise_level 32

# Despeckle the image using (E/e)rode or (D/d)ilate or (l)abel.
despeckle_filter EedDl

# Number of images that must contain motion to trigger an event.
minimum_motion_frames 1
;minimum_motion_frames 5

# Gap in seconds of no motion detected that triggers the end of an event.
event_gap 10

# The number of pre-captured (buffered) pictures from before motion.
pre_capture 3

# Number of frames to capture after motion is no longer detected.
post_capture 0

############################################################
# Script execution configuration parameters
############################################################

# Command to be executed when an event starts.
; on_event_start value

# Command to be executed when an event ends.
; on_event_end value
 
# Command to be executed when a movie file is closed.
; on_movie_end value
on_movie_end "echo 'Security video'|mutt -i - -s 'Security Video' -a "/home/USERNAME/Videos/CAM1-%t-%v-%Y%m%d%H%M.mkv" -- USERNAME@gmail.com"

############################################################
# Picture output configuration parameters
############################################################

# Output pictures when motion is detected
picture_output off

# File name(without extension) for pictures relative to target directory
picture_filename %Y%m%d%H%M%S-%q
;picture_filename %Y%m%d%H%M-%q

############################################################
# Movie output configuration parameters
############################################################

# Create movies of motion events.
movie_output on

# Maximum length of movie in seconds.
movie_max_time 20

# The encoding quality of the movie. (0=use bitrate. 1=worst quality, 100=best)
movie_quality 45

# Container/Codec to used for the movie. See motion_guide.html
movie_codec mkv

# File name(without extension) for movies relative to target directory
movie_filename %t-%v-%Y%m%d%H%M%S
;movie_filename %t-%v-%Y%m%d%H%M

############################################################
# Webcontrol configuration parameters
############################################################

# Port number used for the webcontrol.
webcontrol_port 8080

# Restrict webcontrol connections to the localhost.
webcontrol_localhost on

# Type of configuration options to allow via the webcontrol.
webcontrol_parms 0

############################################################
# Live stream configuration parameters
############################################################

# The port number for the live stream.
stream_port 8081

# Restrict stream connections to the localhost.
stream_localhost on

##############################################################
# Camera config files - One for each camera.
##############################################################
camera /home/USERNAME/.motion/camera1.conf
; camera /usr/etc/motion/camera2.conf
; camera /usr/etc/motion/camera3.conf
; camera /usr/etc/motion/camera4.conf

##############################################################
# Directory to read '.conf' files for cameras.
##############################################################
; camera_dir /usr/etc/motion/conf.d

Camera1.conf

Make a file called camera1.conf.  Set the picture size according to your camera resolution.

Set the USERNAME and PASSWORD to the RTSP stream account that you created in the camera App.

# /usr/etc/motion/camera1.conf
#
# This config file was generated by motion 4.3.2

###########################################################
# Configuration options specific to camera 1
############################################################
# User defined name for the camera.
camera_name CAM1

# Numeric identifier for the camera.
camera_id 101

# The full URL of the network camera stream.
netcam_url rtsp://USERNAME:PASSWORD@10.10.10.78/stream1

# Image width in pixels.
;width 1280
width 1920

# Image height in pixels.
;height 720
height 1080

# Text to be overlayed in the lower left corner of images
text_left CAM1

# File name(without extension) for movies relative to target directory
;movie_filename CAM1_%t-%v-%Y%m%d%H%M%S
movie_filename CAM1-%t-%v-%Y%m%d%H%M 

Ditto for Camera 2.

Motion Configuration Tricks

Enable the Despeckle filter to reduce the amount of motion events. Set the maximum length of the movie clip to 20 seconds, to keep the file smallish and ensure that the email system will accept it.  

Remove the seconds parameter from the file names, since the system is already processing the next event when the current event is handled, which makes it impossible to attach the correct file down to the second.  Sometimes there will still be a problem when the minutes roll over, but most video files will get sent successfully.

Always specify the complete filename path.

Email Test

First test mutt interactively.  Run it from the console and play with it to ensure that you can send and receive email and that gmail is not blocking it.  

Motion Test

Run motion from the command line and play with it, to ensure that it works properly and will send emails with mutt.  If you get too many messages, increase the pixel change trigger level.

You need to find a happy medium where the system will not email a deluge of messages, since Google will block the account if you do. If you find that there is just too much movement in your situation, then you could also use the snapshot feature to email a picture once per hour or so and leave the movies on your home server.

Another option is to make the event gap much longer, so that it will detect motion, but not all motion.

You can view the real time video with a web browser on port 8081:

  • $ firefox http://localhost:8081/101 

Mailbox Cleanup

Deleting a few thousand messages manually using gmail with a webbrowser, would be extremely tedious.  When the mailbox gets full, it may be easier to delete the account and create a new one.

To clean the gmail box in one swell foop, you can try the following command with mutt:
  • mutt -e "push D~d>30d<enter>qy<enter>"
However - it may cause Google to disable your mail account for an hour, since this kind of mass mail delete with the IMAP protocol may look suspicious to their system.


La voila!

Herman