zoukankan      html  css  js  c++  java
  • ubuntu下motion的安装调试

    环境:ubuntu12.04

    版本:motion 3.2.12

    最近刚好弄视频抓拍移动监测这一块,听说motion,是这调试了一下,记录下来。我是在pc虚拟机下面调试的,有关开发板哪一方面的可以参考这个博客里面的内容,其实大致差不多,(http://blog.csdn.net/guozhiyuan20095318/article/details/7310486)。

    1.首先是获取源码,我已经上传了,也可以google下载,ubuntu下可以直接运行sudo apt-get install motion安装。

    2.配置文件。我也是根据网上的资料来配置的,首先配置的是motion.conf,具体的内容我贴上网上的资料。建议大家原始的conf文件做一个备份,另外在自动发送邮件配置的时候肯呢个会出问题,我的配置文件如下:

    #!/bin/bash


    echo "111111111111111on_motion_end1111111111111111"


    DIRC="/root/motion/snapshots/"


    VIDEOTIME="/root/tmp/videotime"


    TIME=$(cat $VIDEOTIME)
    echo $TIME
    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 $DIRC*$TIME
    echo $MAILBODY
    #echo $MAILBODY | mutt -s $TIME -a $DIRC*$TIME.avi xxx@gmail.com
    echo $MAILBODY | mutt -s $TIME -a "$DIRC*$TIME.avi" -- xxx@gmail.com < "/root/aaa.txt"


     


    #second trying of sending the avi video


    TIME=$(expr $TIME - 1)
    #TIME=$(expr $TIME)
    echo $TIME
    echo $MAILBODY | mutt -s $TIME -a $DIRC*$TIME.avi xxx@gmail.com

    运行前,先修改这个文件/root/motion.conf,我的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

    on_event_end /root/motion/on_motion_end

    gap 10

    关于这些配置的具体意义,参见motion官方wikihttp://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当探测到运动时,执行所设定目录里的文件,这里设定为文件/root/motion/on_motion_detected,该文件可以是一个程序,可以是一段脚本,只要是能执行的就可以。

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

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

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

    这里我的on_motion_detected和on_motion_end都是shell脚本。

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

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

     

    #second trying of sending the avi video

    TIME=$(expr $TIME - 1)

    echo $MAILBODY | mutt -s $TIME -a $DIRC*$TIME.avi skpsun@163.com

    当然,在能成功自动发送邮件前,先要确认是否正确安装配置了mailx和ssmtp。否则,需要先安装这两个程序。

    # apt-get install mailx ssmtp

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

    In /etc/ssmtp/ssmtp.conf, set the following:

    root=youraccount@gmail.com

    mailhub=smtp.gmail.com:587

    rewriteDomain=

    hostname=smtp.gmail.com:587

    UseSTARTTLS=YES

    AuthUser=youraccount

    AuthPass=yourpasword

    FromLineOverride=YES # optional

    In /etc/ssmtp/revaliases:

    root:youraccount@gmail.com:smtp.gmail.com:587

    mainuser:youraccount@gmail.com:smtp.gmail.com:587

    并在命令行里输入

    # mail youraccount@gmail.com进行测试,写完邮件正文后按ctrl+D发送,

  • 相关阅读:
    supervisor使用小记
    linux新增定时脚本
    page_fault_in_nonpaged_area异常解决方案(已解决)
    和安卓对接老是ping不通?试试内网映射
    github文件下载加速器
    mybatis新增账号并且返回主键id
    arraylist源码解析
    MySQL安装教程
    通过get方法的方式获取配置项信息
    @Inject注解
  • 原文地址:https://www.cnblogs.com/shulianghe/p/3724145.html
Copyright © 2011-2022 走看看