注意:本文公网远程监控部分需要借助有公网IP的云服务器进行ssh反向代理。

一、借助motion实现内网的视频监控

  • 准备
  • 插上摄像头,然后输入ls /dev/video*命令检查是否识别了摄像头
  • 安装motion
  • motion是linux开源的,在树莓派上使用sudo apt-get install motion命令安装,安装很方便。
  • 修改motion配置
  • 常用配置及含义
# 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 /dev/video*看到的
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
  • 必须要修改的有:daemon onwebcontrol_localhost off。默认配置可能有明显卡顿,这时自行调参数即可。最后效果可以十分流畅。
  • 使用sudo motion命令可以开启motion
  • 使用sudo killall -TERM motion可以关闭motion
  • 检验
  • sudo motion后,在浏览器输入ip:你设定的端口号(默认8081) 即可查看实时监控。如:192.168.137.30:8081。
  • 注意:此时只能在内网访问。

二、通过ssh反向代理树莓派实现公网远程视频监控

  • 树莓派上
  • ssh -CqTfnN -R 0.0.0.0:12345:localhost:8081 ubuntu@yourserver(test.server是云服务器的域名或者ip)
  • 这样就建立了一个本地pi的8081端口到yourserver的12345端口的连接,之后yourserver就可以利用这个端口和pi通信了。
  • 服务器上
  • ssh -fCNL "*:12345:localhost:12346" ubuntu@yourserver
  • 建立一个12346端口监听别的客户机的请求,当12346端口收到数据时候把数据转给12345端口,然后12345端口与树莓派的8081端口连接,实现了“代理”的效果。当浏览器访问云服务器的12346端口时,相当于访问树莓派的8081端口,从而实现了公网的实时监控。
  • 利用这种ssh反向代理的方法也可以实现公网远程控制树莓派。
  • 检验
  • 树莓派开启motion后,在浏览器输入服务器ip:服务器设置的监听端口(本例为12346)即可看到motion的视频。
  • 注意:由于网速的限制,视频可能有卡顿。此时修改motion配置,降低大小、帧率、画质等可以消除卡顿。