【原创】Ubuntu监测动态环境(发送视频到邮箱、NAT模式下端口映射)

来源:互联网 发布:淘宝商城女装朵以 编辑:程序博客网 时间:2024/05/17 17:43


【原创】Ubuntu监测动态环境(发送视频到邮箱、NAT模式下端口映射)


本文章中的操作均在root权限下进行

条件:

1.摄像头可被Ubuntu识别并连接

2.Ubuntu可以和外网顺利通信


Step1 安装motion

# apt-get install motion

安装后,可执行文件motion在目录/usr/bin下。


Step2 修改motion配置文件

找到/etc/motion/motion.conf。copy一份放到/root下,以后均在/root下运行motion。motion.conf是motion程序与操作者的接口文件。通过修改motion.conf里的配置,来控制motion的运行。当在终端里运行#motion时,会先在终端所示当前目录下寻找motion.conf,没有找到时,去寻找/etc/motion/motion.conf。

修改之处如下:

  # Minimal motion example config file provided by the 

  # Debian motion package - for basic webcam operation. 

  # 

  # You most certainly want to investigate 

  # /usr/share/doc/motion/examples/motion-dist.conf.gz 

  # for further configuration options. Also, refer to the 

  # motion man page and /usr/share/doc/motion/motion_guide.html 

  # for detailed information on configuration options. 

  daemon off 

  quiet on 

  locate on 

  # You may very well need to change this (check with 'dmesg' 

  # after plugging in your webcam). 

  videodevice /dev/video0 

  # Image size in pixels (valid range is camera dependent). 

  width 320 

  height 240 

  framerate 25 

  quality 85 

  auto_brightness off 

  # General threshold level and noise threshold 

  # level (for distinguishing between noise and motion). 

  threshold_tune off 

  threshold 4500 

  noise_level 64 

  # Initial brightness, contrast, hue (NTSC), and saturation. 

  # 0 = disabled (valid range 0-255). 

  brightness 0 

  contrast 0 

  saturation 0 

  hue 0 

  # Encode movies in real-time (install ffmpeg before enabling). 

  ffmpeg_cap_new on 

  # Codec to be used by ffmpeg for the video compression. 

  # Supported formats: mpeg4, msmpeg4. 

  ffmpeg_video_codec msmpeg4 

  # Target base directory for pictures and films (you may need 

  # to change this (or change its permissions) depending on 

  # which system user runs motion). 

  target_dir /root/motion/snapshots 

  # Define a port number (e.g. 8000) to enable the mini-http server. 

  # 0 = disabled. 

  webcam_port 8081 

  # Set to 'off' to allow anybody (not just localhost) to view the 

  # webcam via the mini-http server (http://hostname:port). 

  webcam_localhost off 

  snapshot_interval 1 

  snapshot_filename snapshot 

  webcam_quality 50 

  webcam_maxrate 8 

  on_event_start "/root/motion/on_motion_detected.sh" 


  on_event_end "/root/motion/on_motion_end.sh" 


  gap 10 


  配置的具体意义,参见motion官方wiki http://www.lavrsen.dk/twiki/bin/view/Motion/WebHome中的Config options。

      这里,我只对其中几个解释一下。 

   daemon off,关掉deamon模式。最好这项还是选off,否则运行motion后,就会直接在后台运行,需要用top命令查看出motion的进程号(pid),然后再手动kill掉这个进程。 

  locate on设置探测到图像中有运动时,把运动区域用矩形框起来。 

  videodevice /dev/video0 设置加载USB摄像头的设备,一般都是这个video0,当使用network webcam时,需要设置netcam_url,此时,videodevice选项自动失效。 

  threshold_tune off设置是否使用motion detection阈值自动调节。当设置为on时,下一个设置threshold 4500自动失效。设置off时,可以由threshold指定当探测到多少像素变化时,判断为图像中有运动。 

  ffmpeg_cap_new on这个选项是指,在detect到运动时,用视频纪录下来。 

  ffmpeg_video_codec msmpeg4 设定视频的编码器 

  target_dir /root/motion/snapshots当探测到运动时,图片和视频的保存路径,默认时为/var/lib/motion/snapshots。 

  snapshot_interval 1设定自动采集图片的周期,当有运动被检测到时,采集频率会自动变高。 

  on_event_start /root/motion/on_motion_detected.sh当探测到运动时,执行所设定目录里的文件,这里设定为文件/root/motion/on_motion_detected.sh,该文件可以是一个程序,可以是一段脚本,只要是能执行的就可以。 

  on_event_end /root/motion/on_motion_end.sh当on_event_start开始后,即检测到运动后, 若有连续10秒不再能检测到运动时,执行该选项设定的文件。10秒参数是由以下gap 10语句设置而来。 

  以上两个设置参见http://www.lavrsen.dk/twiki/bin/view/Motion/ExternalCommands 

  gap 10设置,在探测到运动后,多长时间没有运动的话就触发运动结束指令on_event_end。 


Step3 编写自动运行脚本

     这里on_motion_detected.sh和on_motion_end.sh都是shell脚本。

on_motion_detected.sh脚本的作用是,记录下探测到运动时的时间,即拍摄的监控视频文件的文件名的一部分。把这个时间存到/root/tmp/videotime文件中。on_motion_detected.sh文件如下: 


  #!/bin/bash 

  echo "111111111111111on_motion_detected1111111111111111" 

  DATE=$(date +"%Y%m%d%H%M%S") 

  #DATE=$(date -d "-1 sec" +%Y%m%d%H%M%S) 

  ALARM_TIME="/root/tmp/videotime" 

  echo "$DATE" > $ALARM_TIME 


on_motion_end.sh就是用来发送邮件的。它会在检测到的运动结束后,讲拍下来的运动的avi视频发送到指定邮箱里。avi视频的文件名为一个序号+检测到运动的时间+.avi,而检测到运动的时间,根据on_motion_detected.sh脚本,存在/root/tmp/videotime里,理论上说只要从文件里读出时间,然后补全文件名(该序号由*号替代),便能发出邮件。但是,由于程序运动效率原因,有时会出现,记录的时间同开始录avi的时间差1秒的情况,虽然只有一秒,但是足以导致找不到avi文件,无法正确发出监控视频。由于我们设置了gap为10,即10秒内最多只有一个视频。所以,解决这个问题的办法可以是,去寻找videotime中所记录时间及其上一秒,连续两秒的视频,找到哪个发哪个。当然,结果永远是只会找到一个。on_motion_end.sh这个shell脚本文件如下: 


  #!/bin/bash 

  echo "111111111111111on_motion_end1111111111111111" 

  DIRC="/root/motion/snapshots/" 

  VIDEOTIME="/root/tmp/videotime" 

  TIME=$(cat $VIDEOTIME) 

  ALARM_EMAIL="/root/tmp/myalarm.txt" 

  echo "Subject: Motion detected - $TIME - $DIRC" > $ALARM_EMAIL 

  echo "">> $ALARM_EMAIL 

  echo "Motion detected - check $TIME.avi">>$ALARM_EMAIL 

  MAILBODY=$(cat $ALARM_EMAIL) 

  #first trying of sending the avi video 

  echo $MAILBODY | mail -s $TIME -a $DIRC*$TIME.avi xxx@163.com 

  #second trying of sending the avi video 

  TIME=$(expr $TIME - 1) 

  echo $MAILBODY | mail -s $TIME -a $DIRC*$TIME.avi xxx@163.com 


Step 4 邮件发送文件配置

安装配置mailx和ssmtp :

  # apt-get install heirloom-mailx  ssmtp 

  然后在/etc/ssmtp/ssmtp.conf里如下进行配置: 

  root=youraccount@gmail.com 

  mailhub=smtp.gmail.com:587 

  rewriteDomain= 

  hostname=smtp.gmail.com:587 

  UseSTARTTLS=YES 

  AuthUser=youraccount 

  AuthPass=yourpasword 

  FromLineOverride=YES 


Step 5  运行

切换到/root目录下:

#cd /root

运行motion:

#motion


参考连接:http://blog.csdn.net/tiantang46800/article/details/6658446


在局域网内的浏览器上查看视频:

运行motion后,在浏览器中输入  http://192.168.0.103:8081/  即可查看。

其中192.168.0.103:为Ubuntu的ip,8081为motion设置的端口


其他说明:

目前我虚拟机中的Ubuntu联网方式为桥接模式(因为我有一个路由器,这样用浏览器查看视频方便些),所以其ip地址和宿主机ip(我的是win8)在同一个网段中。当然也可以设置为NAT模式,在此模式下Ubuntu的ip极有可能和宿主机不在一个网段,因此如果想在局域网中用其他PC机的浏览器查看视频,则需要进行端口映射,例如我这里端口为8081,那我就可以将端口8081映射到宿主机的ip上。


不管是那种模式,目的都是让虚拟机和局域网内的其他PC机能够ping通(包括宿主机)。


NAT模式下,浏览器查看视频方式

http://192.168.0.100:8081/  

其中192.168.0.100为宿主机ip,8081为映射的端口。


NAT模式下,端口映射方法,如图所示:

1



2



3



4



配置完成后,点击确定、应用即可。

另外,如果正确配置后发现还是ping不通,可以把Ubuntu网络断开一下,然后再重新连接试试,如果还是不行,那很有可能是配置有问题或者其他问题,可以去网络上搜索一下相关问题。


展望:

目前已经可以实现监测动态环境,并在环境发生变化时将保存的动态视频发送到指定邮箱中,但是通过浏览器监控视频只能在局域网内进行,所以下一步计划实现外网通过浏览器实时查看视频。




0 0