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:


# Set this to your email address.

export FILEPATH="${@}"
if ! [ -f ${FILEPATH} ]; then
        logger -t motion_email "filepath ${FILEPATH} does not exist"
        exit 0
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
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 $
logger -t motion_email Thumbnail generated.

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

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

## 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 realname = "Inigo Montoya"
set smtp_url = "smtp://"
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.