ssh反向代理樹莓派+motion,實現公網遠程視頻監控

注意:本文公網遠程監控部分需要藉助有公網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配置,降低大小、幀率、畫質等可以消除卡頓。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章