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

 
前提条件

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

異常検知時録画ファイル保存ディレクトリの作成
 
 [root@centos]#
mkdir /home/****/camera    (****は任意の場所)
 
ディレクトリの作成場所はどこでも良いが、録画映像をインターネット経由で見られるように公開ディレクトリの中に作成
アクセス権の変更
 
 [root@centos]#
chmod 777 /home/****/camera    (****は任意の場所)
 
motion の設定
 
 [root@centos]#
vi /usr/local/etc/motion.conf
 
 
途中略

 ############################################################
 # Motion Detection Settings:
 ############################################################

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

 threshold
****   <--- カメラの性能や環境によって違ってくる 一般的には300ぐらいがベターとされている
 
 
途中略
 
 ############################################################
 # Image File Output
 ############################################################
 # 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
on   <--- 書換え( 画像ファイルを保存しない)
  

 output_pictures
off  
 
 # Output pictures with only the pixels moving object (ghost images) (default: off)
 output_debug_pictures off
 
 # The quality (in percent) to be used by the jpeg compression (default: 75)
 quality 75
 
 # Type of output images
 # Valid values: jpeg, ppm (default: jpeg)
 picture_type jpeg  
<--- コメント化
  

 
#picture_type jpeg 
 
 ############################################################
 # FFMPEG related options
 # Film (mpeg) file output, and deinterlacing of the video input
 # The options movie_filename and timelapse_filename are also used
 # by the ffmpeg feature
 ############################################################

 # Use ffmpeg to encode mpeg movies in realtime (default: off)
 ffmpeg_output_movies
on   <--- 確認(off の場合は動体検知した映像を保存しない)

 # Use ffmpeg to make movies with only the pixels moving
 # object (ghost images) (default: off)
 ffmpeg_output_debug_movies
off
   <--- 書き換え
  

 
ffmpeg_output_debug_movies on

 
途中略
 
 ############################################################
 # Text Display
 # %Y = year, %m = month, %d = date,
 # %H = hour, %M = minute, %S = second, %T = HH:MM:SS,
 # %v = event, %q = frame number, %t = thread (camera) number,# %D = changed pixels, %N =
 # %i and %J = width and height of motion area,                            noise level, \n = new line,
 # %K and %L = X and Y coordinates of motion center
 # %C = value defined by text_event - do not use with text_event!
 # You can put quotation marks around the text to allow
 # leading spaces
 ############################################################
 
 # Locate and draw a box around the moving object.
 # Valid values: on, off and preview (default: off)
 # Set to 'preview' will only draw a box in  preview_shot pictures.
 locate_motion_mode
off   <--- 変更(動きを検知したところを枠で囲む
  

 locate_motion_mode
on
 
 
途中略

 # Draw the number of changed pixed on the images (default: off)
 # Will normally be set to off except when you setup and adjust the motion settings
 # Text is placed in upper right corner
 text_changes
off   <--- 変更(変化があったピクセル数を表示
  

 text_changes
on

 
途中略

 ############################################################
 # Target Directories and filenames For Images And Films# For the options snapshot_, jpeg_,
 # you can use conversion specifiers                                  mpeg_ and timelapse_filename
 # %Y = year, %m = month, %d = date,# %H = hour, %M = minute, %S = second,
 # %v = event, %q = frame number, %t = thread (camera) number,
 # %D = changed pixels, %N = noise level,# %i and %J = width and height of motion area,
 # %K and %L = X and Y coordinates of motion center
 # %C = value defined by text_event# Quotation marks round string are allowed.
 ############################################################

 # Target base directory for pictures and films
 # Recommended to use absolute path. (Default: current working directory)
 
 target_dir
/usr/local/apache2/htdocs/cam1   <--- 変更(上記で作成した異常検知時録画ファイル保存ディレクトリ
  

 target_dir
/home/****/camera

 
以下略
 
ffmpeg の設定
設定ファイルのコピー
 
 [root@centos]#
cp ~/ffmpeg_sources/ffmpeg/doc/ffserver.conf /etc/
 
ffmpeg の設定
 
 [root@centos]#
vi /etc/ffserver.conf
 # Port on which the server is listening. You must select a different
 # port from your standard HTTP web server if it is running on the same
 # computer.
 HTTPPort
8090   <--- 確認 8090になっていること
 
 # Address on which the server is bound. Only useful if you have
 # several network interfaces.
 HTTPBindAddress
0.0.0.0   <--- 確認 0.0.0.0になっていること

 # Number of simultaneous HTTP connections that can be handled. It has
 # to be defined *before* the MaxClients parameter, since it defines the
 # MaxClients maximum limit.
 MaxHTTPConnections 2000

 # Number of simultaneous requests that can be handled. Since FFServer
 # is very fast, it is more likely that you will want to leave this high
 # and use MaxBandwidth, below.
 MaxClients
1000   <--- 確認 1000になっていること

 # This the maximum amount of kbit/sec that you are prepared to
 # consume when streaming to clients.
 MaxBandwidth
1000   <--- 確認 1000になっていること

 # Access log file (uses standard Apache log file format)
 # '-' is the standard output.
 CustomLog
-   <--- 確認 -になっていること
 
 ##################################################################
 # Definition of the live feeds. Each live feed contains one video
 # and/or audio sequence coming from an ffmpeg encoder or another
 # ffserver. This sequence may be encoded simultaneously with several
 # codecs at several resolutions.

 <Feed feed1.ffm>

 # You must use 'ffmpeg' to send a live feed to ffserver. In this
 # example, you can type:
 #
 # ffmpeg http://localhost:8090/feed1.ffm

 # ffserver can also do time shifting. It means that it can stream any
 # previously recorded live stream. The request should contain:
 # "http://xxxx?date=[YYYY-MM-DDT][[HH:]MM:]SS[.m...]".You must specify
 # a path where the feed is stored on disk. You also specify the
 # maximum size of the feed, where zero means unlimited. Default:
 # File=/tmp/feed_name.ffm FileMaxSize=5M
 File
/tmp/feed1.ffm   <--- 確認 /tmp/feed1.ffmになっていること
 FileMaxSize
200K   <--- 確認 200Kになっていること

 # You could specify
 # ReadOnlyFile /saved/specialvideo.ffm
 # This marks the file as readonly and it will not be deleted or updated.

 # Specify launch in order to start ffmpeg automatically.
 # First ffmpeg must be defined with an appropriate path if needed,
 # after that options can follow, but avoid adding the http:// field
 #Launch ffmpeg

 # Only allow connections from localhost to the feed.
 ACL allow
127.0.0.1   <--- 確認 自サーバーのカメラのみ可能
                                                
他のマシンから配信させるにはACLに追加が必要
 </Feed>

 
途中略

 ##################################################################
 # Example streams
 
 
途中略


 # Flash

 
#<Stream test.swf>   <--- 以下コメント解除  Flash 動画再生の場合
 
#Feed feed1.ffm
 
#Format swf
 
#VideoFrameRate 2
 
#VideoIntraOnly
 
#NoAudio
 
#</Stream>
  

 <Stream test.swf>
 Feed feed1.ffm
 Format swf
 VideoFrameRate 2
 VideoIntraOnly
 NoAudio
 </Stream>

 # ASF compatible

 <Stream
test.asf>
 
Feed feed1.ffm   <--- 以下のようになっている事を確認  Media Player 動画再生の場合
 
Format asf
 VideoFrameRate 15
 VideoSize 352x240
 VideoBitRate 256
 VideoBufferSize 40
 VideoGopSize 30
 AudioBitRate 64
 StartSendOnKey

 </Stream>
 
 
途中略>
 

 # Real with audio and video at 64 kbits  
 
 #<Stream test.rm>   <--- 以下コメント解除  RealPlayer 動画再生の場合
 
#Feed feed1.ffm
 #Format rm
 
#AudioBitRate 32
 
#VideoBitRate 128
 
#VideoFrameRate 25
 
#VideoGopSize 25
 
#NoAudio#
 
#</Stream>
  

 <Stream test.rm>
 Feed feed1.ffm

 Format rm
 AudioBitRate 32
 VideoBitRate 128
 VideoFrameRate 25
 VideoGopSize 25
 NoAudio
 </Stream>

 
 
途中略
 
 ##################################################################
 # A stream coming from a file: you only need to set the input
 # filename and optionally a new format. Supported conversions:
 # AVI -> ASF

 #<Stream file.rm>
 #File "/usr/local/httpd/htdocs/tlive.rm"
 #NoAudio
 #</Stream>

 
#<Stream file.asf>   <--- 以下コメント解除 & 変更(上記で作成した異常検知時録画ファイル保存ディレクトリ
 
#File "/usr/local/httpd/htdocs/test.asf"
 
#NoAudio
 
#Metadata author "Me"
 
#Metadata copyright "Super MegaCorp"
 
#Metadata title "Test stream from disk"
 
#Metadata comment "Test comment"
 
#</Stream>
  

 <Stream file.asf>
 File "
/home/****/camera/test.asf"
 NoAudio
 Metadata author "Me"
 Metadata copyright "Super MegaCorp"
 Metadata title "Test stream from disk"
 Metadata comment "Test comment"
 </Stream>

 
以下追記  ※swf 及び rm 形式で録画する場合
 
 
<Stream file.swf>
 File "/home/****/camera/test.swf"
 NoAudio
 Metadata author "Me"
 Metadata copyright "Super MegaCorp"
 Metadata title "Test stream from disk"
 Metadata comment "Test comment"
 </Stream>

 
 
<Stream file.rm>
 File "/home/****/camera/test.rm"
 NoAudio
 Metadata author "Me"
 Metadata copyright "Super MegaCorp"
 Metadata title "Test stream from disk"
 Metadata comment "Test comment"
 </Stream>

 
 
途中略
 
 ##################################################################
 # Special streams

 # Server status
 
 <Stream stat.html>

 Format status
   <--- 以下のようになっている事を確認
 
 # Only allow local people to get the status
 ACL allow localhost
 ACL allow 192.168.0.0 192.168.255.255

 #FaviconURL http://pond1.gladstonefamily.net:8080/favicon.ico
 </Stream>

 # Redirect index.html to the appropriate site

 <Redirect index.html>
 
URL http://www.ffmpeg.org/   <--- こののようになっている事を確認
 </Redirect>
 
ファイルが出来上がったらデーモンのリロード

 
 [root@centos]#
systemctl --system daemon-reload
 

ffmpeg の動作確認
motion が起動中の場合は ffmpeg が正常に起動できないので一旦 motion を終了する
 
 [root@centos]#
/usr/bin/killall motion
 
起動及び録画開始
 
 [root@centos]#
ffmpeg -f v4l2 -s 640x480 -i /dev/video0 /home/****/www/xxxx.mpg
 
※640x480 は動画画面のサイズ、 /home/****/www/xxxx.mpg は動画の保存先及び動画名
上記コマンドで録画が開始されるので適当な時間録画をしたら端末をアクティブした状態で『Ctrl』+『c』を押して録画を終了
/home/****/www/ に xxxx.mpg と言うファイルが出来上がるので再生して問題無く録画dsれている事を確認
ffserver の起動
 
 [root@centos]#
ffserver -f /etc/ffserver.conf &
 
起動ファイルの作成
ffserver の起動ファイルの作成

 
 [root@centos]#
vi /usr/lib/systemd/system/ffserver.service
 
以下新規記述
 
[Unit]
 Description=The FFServer Service
   <--- FFServer を記述

 [Service]
 Type=simple
 ExecStart=ffserver -f /etc/ffserver.conf & 
<--- ffserver の起動コマンド
 Execreload=/usr/bin/killall ffserver ~/bin/ffserver -HUP $MAINPID 
<--- ffserver の 再読込コマンド
 ExecStop=/usr/bin/killall
ffserver   <--- ffserver の終了コマンド
 PrivateTmp=true

 [Install]
 WantedBy=multi-user.target

 

ファイルが出来上がったらデーモンのリロード

 
 [root@centos]#
systemctl --system daemon-reload
 

ffserver・ffmpeg の起動

 
 [root@centos]#
systemctl start ffserver.service
 

自動起動設定

 
 [root@centos]#
systemctl enable ffserver.service
 

自動起動設定の確認

 
 [root@centos]#
systemctl list-unit-files | grep ffserver.service
 ffserver.service                                  enable
   <--- enable になっていればOK
 

ブラウザ確認
ブラウザを起動し上記で作成した異常検知時録画ファイル保存ディレクトリにアクセスする

motion が動体検知し録画されたファイルが列記されているので確認する

なお、apache の設定で index ファイルが無い場合にフォルダ内を閲覧できないように設定している場合は異常検知時録画ファイル保存ディレクトリのファイル名を直接指定する

サンプル映像は ここ
なお、デフォルトのプレーヤーが Windows Media Player の場合、環境・バージョンによっては対応コーディックがインストールされていないので再生できないので ここ より CCCP (コーディック)をダウンロードしてインストールする

戻る