打造全自动监控系统OpenWrt+Motion+ffmpeg+msmtp+Mutt

准备好基于OpenWrt系统的软路由,USB摄像头(uvc),OpenWrt编译环境。

编译OpenWrt可看另外一篇文章,本章主要定制编译软件包及应用配置

$make menuconfig
> Kernel modules > USB Support > kmod-usb-core
> Kernel modules > Video Support > kmod-video-core
> Kernel modules > Video Support > kmod-video-uvc
> Multimedia > ffmpeg
> Multimedia > motion
> Mail > msmtp
> Mail > mutt

编译系统,完成后系统会在<builddir>/bin/targets/YourPlatform/

$make -j1 V=s

既然是定制系统,我将以下调试好的配置文件放置在<builddir>\

files/etc/config/motion.conf
files/etc/crontabs/root
files/etc/init.sh
files/etc/msmtprc
files/etc/Muttrc
files/etc/rc.local

motion.conf配置文件需要根据实际情况改变

videodevice /dev/video0 #根据usb摄像头设备填写
v4l2_palette 8 # 'MJPEG'
width 640
height 480
framerate 15 #每秒15帧
threshold 3000 #像素差异触发
event_gap 60 #静止60秒后结束事件
quality 85 #输出jpg质量
#ffmpeg_output_movies on #由于OpenWrt平台缺少libx264故无法自动调用ffmpeg转换成MP4
target_dir /mnt/sda3/motion/jpg #输出目录
stream_port 8080 #web监控端口
stream_quality 50 #web监控质量,减少带宽占用
stream_auth_method 1 # 1 = Basic authentication
stream_authentication user:password
stream_localhost off #允许非本地访问
on_event_start echo %Y%m%d'_'%H%M%S > /mnt/sda3/motion/detectedTime 
#记录事件监控时间重定向到文件,事件结束后会调用
on_event_end 'ffmpeg -pattern_type glob -r 15 -i '/mnt/sda3/motion/jpg/%v'-'%Y%m%d%H*.jpg' -b:v 800k -s 640x480 /mnt/sda3/motion/mp4/%v'-'%C.mp4; cd /mnt/sda3/motion/mp4/; echo %v'-'%C.mp4 > /mnt/sda3/motion/latestMp4; cd /mnt/sda3/motion/jpg/; tar zcf /mnt/sda3/motion/tarball/%v'-'%C.tar.gz %v'-'%Y%m%d%H*.jpg; rm -f %v'-'%Y%m%d%H*.jpg; echo check file $(cat /mnt/sda3/motion/latestMp4) | mutt -s "Detected motion at $(cat /mnt/sda3/motion/detectedTime)" -a "/mnt/sda3/motion/mp4/$(cat /mnt/sda3/motion/latestMp4)" -- your@mail.com'
#监控事件结束后触发ffmpeg以每秒15帧,800k视频码率,640x480分辨率将事件的图片转换成视频,然后打包备份原始jpg,删除jpg零散文件,将MP4以附件的型式用mutt发送到指定邮箱。

files/etc/init.sh配置文件需要根据实际情况改变

#/bin/bash
#create mutt and msmtp folder
if [ ! -d "/tmp/mail/" ];then mkdir /tmp/mail;fi
if [ ! -d "/tmp/mail/sent/" ];then mkdir /tmp/mail/sent;fi
if [ ! -d "/tmp/mail/sent/tmp/" ];then mkdir /tmp/mail/sent/tmp;fi
if [ ! -d "/tmp/mail/sent/new/" ];then mkdir /tmp/mail/sent/new;fi
if [ ! -d "/tmp/mail/sent/cur/" ];then mkdir /tmp/mail/sent/cur;fi
if [ ! -f "/tmp/mail/inbox" ];then touch /tmp/mail/inbox;fi
#sent email when startup
echo "$(logread)" | /usr/bin/mutt -s "router is start running"  -- your@mail.com

files/etc/msmtprc 根据实际情况修改

# Set default values for all following accounts.
defaults
auth on
tls on
tls_starttls on
tls_certcheck off
logfile /tmp/mail/msmtp.log
account outlook
host smtp.office365.com
port 587
from your@mail.com
user your@mail.com
password password
account default : outlook
#如是微软outlook邮箱使用此配置即可

files/etc/Muttrc根据实际情况修改

set sendmail="/usr/bin/msmtp"
set use_from=yes
set from=your@mail.com
set envelope_from=yes
set send_charset="utf-8"
set folder​=/tmp/mail
set mbox=+mbox
set spoolfile=+inbox
set record=+sent
set postponed=+drafts
set mbox_type=Maildir

files/etc/rc.local 根据实际情况修改

# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.
/etc/init.sh &  #开机运行此脚本
/usr/bin/motion -c /etc/config/motion.conf -b & #开机后台运行motion

exit 0
图片无替代文字
发布于
归类为文章