自宅サーバー ≫ Linux Server ≫ CentOS Stream 8 ≫ Webカメラサーバー(motion 監視カメラ)

 
ここでは基本的な設定のみを行うので詳細な設定はWebカメラサーバー(motion conf ファイル解説)を参照
前提条件

Webカメラサーバー(motion) と Webサーバー(apache) が稼働していることが前提条件

異常検知時録画ファイル保存ディレクトリの作成
 
 [root@centos]#
mkdir /home/****/camera    (****/cameraは任意の場所)
 
ディレクトリの作成場所はどこでも良いが、録画映像をインターネット経由で見られるように公開ディレクトリの中に作成 する場合はセキュリティに配慮してパスワード認証等の措置を講じること
アクセス権の変更
 
 [root@centos]#
chmod 777 /home/****/camera    (****/cameraは任意の場所)
 
motion の設定
 
 [root@centos]#
vi /etc/motion/motion.conf
 # Rename this distribution example file to motion.conf
 #
 # This config file was generated by motion 4.3.2
 # Documentation: /usr/share/doc/motion/motion_guide.html
 #
 # This file contains only the basic configuration options to get a
 # system working. There are many more options available. Please
 # consult the documentation for the complete list of all options.
 #
 
 ############################################################
 # System control configuration parameters
 ############################################################

 
途中略

 # Target directory for pictures, snapshots and movies
 ; target_dir value              <--- 書換え動体検知を録画したファイルの保存場所)
  

 
target_dir /home/*****/camera   <--- 上記で設定した保存場所ディレクトリ

 
途中略

 ############################################################
 # Motion detection configuration parameters
 ############################################################

 # Always save pictures and movies even if there was no motion.
 emulate_motion off

 # Threshold for number of changed pixels that triggers motion.
 threshold
1500   <--- 書換え動体検知を認知する値 数字が小さいほど敏感に認知する)
  

 threshold
****   <--- カメラの性能や環境によって違ってくる 
            
最初デフォルトで運用し様子を見ながら少しずつ変えていく
 
途中略
 
 # Noise threshold for the motion detection.
 ; noise_level 32  
<--- 書換え動体検知かノイズかを判断する数値
  

 ; noise_level
**   <--- カメラの性能や環境によって違ってくる 
             
最初デフォルトで運用し様子を見ながら少しずつ変えていく

 
以下略
 
motion 再起動
 
 [root@centos]#
systemctl restart motion
 
motionが異常検知し録画した場合にメール連絡
Motionが動体検知し録画した場合に通知メールをスマートフォンに送る
そのメールに録画した動画も添付する
録画映像形式の変更
  motion のデフォルトの録画映像は mkv 形式だが、通常スマートフォンでは拡張子が mp4 avi でないと再生できないため録画映像を mp4 形式に変更する
 
 [root@centos]#
vi /etc/motion/motion.conf
 
 
途中略

 ############################################################
 # Movie output configuration parameters
 ############################################################

 # Create movies of motion events.
 movie_output on

 # Maximum length of movie in seconds.
 movie_max_time 60

 # The encoding quality of the movie. (0=use bitrate. 1=worst quality, 100=best)
 movie_quality 45

 # Container/Codec to used for the movie. See motion_guide.html
 movie_codec
mkv        <---- 書き換え
  

 movie_codec
mp4
 
 
以下略
 
motion 再起動
 
 [root@centos]#
systemctl restart motion
 
コンソール用メール送信ソフト muttのインストール
 
 [root@centos]#
dnf -y install mutt
 
mutt用ディレクトリの作成
 
 [root@centos]#
mkdir /usr/local/mutt
 
設定ファイル mutt.conf の新規作成
 
 [root@centos]#
vi /usr/local/mutt/mutt.conf
 
set charset="utf-8"     <---- 文字化け対策
 set send_charset="us-ascii:iso-2022-jp"    
<---- 文字化け対策第

 set from = ********@ezweb.ne.jp    
<---- 送り先(スマートフォンのアドレス)
 set realname = " Web Camera Info "   
 <---- 送り元 任意の名前
 
mutt でメールの送信テスト
 
 [root@centos]#
echo "テスト" | mutt -F /usr/local/mutt/mutt.conf -s "test" ********@ezweb.ne.jp
 
設定したスマートフォンにメールが届けば成功
動体検知時にメール送信するシェルスクリプトの作成
 
 [root@centos]#
vi /usr/local/mutt/on_movie_end.sh
 #!/bin/sh
 AVI_PATH=$1

 EMAIL_TO=********@ezweb.ne.jp
 EMAIL_SUB=" 動体検知を録画しました! "
 EMAIL_BODY=" 録画映像を添付しました。 "
 MUTT_CONF=/usr/local/mutt/mutt.conf

 echo $EMAIL_BODY | mutt -F $MUTT_CONF -s $EMAIL_SUB $EMAIL_TO -a $AVI_PATH

 
シェルスクリプトに実行権限設定
 
 [root@centos]#
chmod +x /usr/local/mutt/on_movie_end.sh
 
シェルスクリプトの実行テスト
 
 [root@centos]#
bash /usr/local/mutt/on_movie_end.sh
 
スマートフォンにメールが届いたらテスト成功
※まだ、録画映像は無いので添付ファイルはない
Motionへの登録
 
 [root@centos]# vi /etc/motion/motion.conf

 
途中略

 ############################################################
 # Script execution configuration parameters
 ############################################################

 # Command to be executed when an event starts.
 ; on_event_start value

 # Command to be executed when an event ends.
 ; on_event_end value

 # Command to be executed when a movie file is closed.
 ; on_movie_end value
       <---- 書き換え
  

 on_movie_end
bash /usr/local/mutt/on_movie_end.sh %f
  
%f は /usr/local/mutt/on_movie_end.sh に録画データの保存先を引き渡すコマンドになるので必ず記入
 
 
以下略
 
motion 再起動
 
 [root@centos]#
systemctl restart motion
 
motionが異常検知し録画した場合のメール連絡例
実際にmotionが異常検知し録画した映像を添付したメールがスマートフォンに届いた画像
録画画像が添付されている

録画した映像を定期的に削除
シェルスクリプトの作成
 
 [root@centos]#
vi /usr/local/bin/motion_delete.sh
 #!/bin/bash
 find /home/*****/camera -name '*.mp4' -mtime +1 -delete

 
exit 0
 
/home/******/camera にある拡張子が mp4 のファイルで1日前(2日を超えない=47時間59分59秒)より古いものを削除 ※実質2日保存後削除
シェルスクリプトに実行権限設定
 
 [root@centos]#
chmod +x /usr/local/bin/motion_delete.sh
 
定期実行をcronに登録
 
 [root@centos]#
crontab -e

 
下記を追記

 
0 3 * * * bash /usr/local/bin/motion_delete.sh
 
上記の内容は毎日AM 3:00に『/usr/local/bin/motion_delete.sh』を実行
添付ファイルの容量について
録画時間によっては添付ファイルの容量が大きくなる可能性があり、その場合はプロバイダーの制限により容量オーバーでエラーになろ事がある
この場合は、録画画像を圧縮して添付する事によりエラーを回避しメールが届くようにする事ができる

戻る