generating thumbnails from video to use for email alerts

I’m using motion to watch an RTSP camera I obtained from Aliexpress for about $25. It has wifi and ethernet connections.

With the on_event_end parameter and a script, I was able to get motion to send emails of nicely generated thumbnails:

#!/bin/bash

# Set this to your email address.
export EMAILADDRESS="PLACE@YOUREMAIL.HERE"

export FILEPATH="${@}"
if ! [ -f ${FILEPATH} ]; then
        logger -t motion_email "filepath ${FILEPATH} does not exist"
        exit 0
fi
export MOVIE=$(basename "$FILEPATH")
if [ -z "${MOVIE}" ]; then
		# sometimes motion runs an event without the argument. let's not run in that case.
        exit 0
fi
logger -t motion_email Begin email thumbnail processing..
# this command will produce a single image that contains nine keyframes in 3x3 tile
ffmpeg -ss 1 -skip_frame nokey -i $FILEPATH -vf 'scale=128:72,tile=3x3' -an -vsync 0 $FILEPATH.best.%03d.jpg
logger -t motion_email Thumbnail generated.

# Build list of attachments for Mutt.
export ATTACHMENTS=""
for i in $FILEPATH.best.*.jpg ; do
        export ATTACHMENTS="${ATTACHMENTS} -a $i" # mutt
done

echo 'Security alert detected.' | mutt -F /etc/muttrc -s "Alert!" ${EMAILADDRESS} -a "$FILEPATH" ${ATTACHMENTS}
for i in $FILEPATH.best.*.jpg ; do
        rm -v ${i}
done

## delete videos older than 30d
find /camera/ffmpeg/cam1 -maxdepth 1 -name "*.avi" -type f -mtime +30 -delete

In /etc/muttrc we need the following:

# do not store local copy. this allows the use of /dev/null as homedir for motion  user.
set copy = no
# use starttls
set ssl_starttls=yes
set ssl_force_tls=yes
# set from address
set from = "REPLACE@FROMADDRESS.HERE"
set realname = "Inigo Montoya"
set smtp_url = "smtp://USERNAME@DOMAIN.TLD@mail.server.com:587/"
set smtp_pass = "examplepassword"
# do not verify certificate
set ssl_verify_dates = "no"
# use plaintext login
set smtp_authenticators="plain"
# trusted certificate path
set certificate_file="/etc/mutt_certificates"

I manually used mutt to login to my mailserver and accepted the certificate. Then I copied the .mutt_certificates file to /etc/mutt_certificates.