继续监控!使用树莓派+Motion实现实时视频监控并通过浏览器查看

之前我们已经实现了使用树莓派实现定时拍照监控并发送邮件到邮箱,但是定时拍照有两个缺点:

1.拍照每5min才进行一次,这中间会错过很多东西;提高拍照频率又会占用太多存储空间

2.不具有即时性。有时候你想查看这个时候的监控,却得等一小时后才能看,因为邮件还没发送。

所以我们还需要视频监控

首先确保手上有下列东西:

1.树莓派(以下简称RPI)一个

2.一个USB免驱摄像头,支持YUY2或者JPEG均可(JPEG最佳)。RPICamera没试过

博主的配置: RPI3b 一个废弃的YUY2摄像头


开工!

1.打开终端,切换成root,若无法切换,请先为root设置密码(passwd root)

su

2.插上摄像头,然后输入以下命令检查是否识别了摄像头

ls /dev/video*

如果不是提示没有文件,那么就表示我们的摄像头已经被识别了,接下来就可以安装拍摄软件了:

3.安装motion

apt install motion

4.修改motion配置

nano /etc/motion/motion.conf

修改以下内容:(只列出需要改的设置,其他的自行研究)

# Start in daemon (background) mode and release terminal (default: off)
# 在后台运行。设置为off将在前台运行
daemon on

# Videodevice to be used for capturing  (default /dev/video0)
# for FreeBSD default is /dev/bktr0
# 视频设备,刚才ls看到的
videodevice /dev/video0

# Image width (pixels). Valid range: Camera dependent, default: 352
# 图像宽
width 320

# Image height (pixels). Valid range: Camera dependent, default: 288
# 图像高
height 240

# The setting for keep-alive of network socket, should improve performance on compatible net cameras.
# off: The historical implementation using HTTP/1.0, closing the socket after each http request.
# force: Use HTTP/1.0 requests with keep alive header to reuse the same connection.
# on: Use HTTP/1.1 requests that support keep alive as default.
# Default: off
# 开启KeepAlive功能
netcam_keepalive on

# Output 'normal' pictures when motion is detected (default: on)
# Valid values: on, off, first, best, center
# When set to 'first', only the first picture of an event is saved.
# Picture with most motion of an event is saved when set to 'best'.
# Picture with motion nearest center of picture is saved when set to 'center'.
# Can be used as preview shot for the corresponding movie.
# 禁用自动拍照保存的功能
output_pictures off

# Use ffmpeg to encode movies in realtime (default: off)
# 禁用自动拍摄视频保存的功能
ffmpeg_output_movies off

# The mini-http server listens to this port for requests (default: 0 = disabled)
# 视频监听的端口,默认8081
stream_port 1001

# Quality of the jpeg (in percent) images produced (default: 50)
# 图像质量
stream_quality 50

# Output frames at 1 fps when no motion is detected and increase to the
# rate given by stream_maxrate when motion is detected (default: off)
stream_motion on

# Maximum framerate for stream streams (default: 1)
# 帧数8,需要先把上面的选项改成on
stream_maxrate 8

# Set the authentication method (default: 0)
# 0 = disabled
# 1 = Basic authentication
# 2 = MD5 digest (the safer authentication)
# 改成1,增加授权验证,访问需要输入密码
stream_auth_method 1

# Authentication for the stream. Syntax username:password
# Default: not defined (Disabled)
# 设置用户名username和密码password
stream_authentication username:password

# Restrict stream connections to localhost only (default: on)
# 改成off允许外网访问视频
stream_localhost off

# TCP/IP port for the http server to listen on (default: 0 = disabled)
# WEB控制台监听的端口,默认8080
webcontrol_port 1000

# 改成off允许外网访问web控制台
webcontrol_localhost off

5.试运行:

motion

6.浏览器访问 http://用户名:密码@树莓派IP地址:1001

例如:http://username:password@192.168.1.2:1001/

如果没有设置密码则是 http://树莓派IP地址:1001

要访问Web控制台,把1001改成1000即可

7.成功!


关于CPU资源占用问题:

即使没有人访问监控地址,motion也会占用CPU资源(我这里占用2.5%左右)

如果要解决这个问题,我们可以借助其他程序来开关motion,在需要时打开,不需要时关闭

比如使用PHP来实现:(前提是motion以后台模式运行)

<?php
set_time_limit(5);
//检测motion是否在运行
$run = `ps -x |grep motion | grep -v grep`;
if(empty($run)) {
    passthru('motion'); //不在运行则启动
} else {
    echo $run;
    passthru('pkill motion'); //在运行则杀死
}

这样,浏览器访问这个php就可以开关motion了


为什么不使用mjpg-streamer

它实在是太老了。apt找不到它的身影,就连编译还需要改源代码来兼容

目前它只适合给OPENWRT用


正在加载评论。你可能需要科学上网才能正常加载评论区