📜 ⬆️ ⬇️

Big Brother in Linux or do it yourself video surveillance

Big brother in Linux or video surveillance with their own hands.

After moving to a new office, in order not to relax the staff, it was decided
install video surveillance system. But, as usual, the main condition was:
cheap and cheerful :) After deliberation and calculations, it was decided to stop at
following:
IP cameras and computer as a recorder.
For starters, we tried the well-known ZoneMinder. But for some reason with 9 cameras, he
shipped the system mercilessly. After some struggle with him, it was decided to abandon
him Searching the Internet was found a program called
motion (http://www.lavrsen.dk/twiki/bin/view/Motion/WebHome). Reading a little
about her, it was decided to stop at her.
So, we had IP cameras, a computer as a recorder, motion in
the quality of software and the desire to make friends all this collective farm :)
And what can the motion of what we want? :)
1. Work with cameras connected via capture cards or with IP cameras.
2. Execute scripts at the onset and end of the event.

Well, let's get started. First, let's try with one camera :)
Install the motion.
Next, in the folder / etc / motion / copy the configuration file (motion.conf) to the file
for our camera, for example motion1.conf. And now we begin to rule it under
yourself:
1. comment out the line
videodevice / dev / video0
2. in the netcam_url line, we register the URL for access to our camera. we
used Axis IP cameras, model 207 (with built-in microphone).
For our case it looked like this:
netcam_url
http : //camera_ip/axis-cgi/mjpg/video.cgi? resolution = 640x480
where camera_ip is the IP address of the camera :)
3. Further we find the string target_dir - here we specify the path to the folder where we will be
store data from the camera.
4. And now the fun begins. Find the string on_event_start. Here
we prescribe a command that will be executed when an event occurs, then
there is when driving. What does this give us? And it gives us something that we will not
constantly keep a record, but only if necessary - there is movement, recording is in progress,
ended, and we stopped. This is how savings come about. For
comparison - we are recording with 10 cameras - color image 640x480 with
sound, records are stored for 21 days and the whole thing takes on disk an average of 135
GB, and vseste with photos, which makes the motion about 200 GB.
5. the next line: on_event_end is for the command that runs on
the end of the event.
6. And one more thing, motion raises a small http server, and since for
Each camera starts its own instance of the program with a separate config, then
It is important that he listened at different ports. To do this, check that the following
the lines differed in each file:
webcam_port and control_port
For example: for the first camera, set the values
webcam_port 8081
control_port 8080
for the second
webcam_port 8083
control_port 8082
and so on.
')
This is basically a major change. I think when you carefully read the config you
you will find the parameters you need, since everything is pretty good there
documented.

Now back to the scripts that work out at the beginning and end
developments.
Since we need to write video from the camera, for this I used the mencoder.
In the folder / root / bin / create scripts recordstart.sh and recordstop.sh, which
will be launched at the onset and end of the event, respectively.
Let's look inside recordstart.sh:

[root @ video bin] # cat /root/bin/recordstart.sh
#! / bin / bash

# We are using two variables
# 1 - camera name
# 2 - directory name (where file will be stored)

# Generate filename
start = "cam $ {1}"
dt = `date +% Y% m% d% H% M% S`
FILENAME = "$ {start} - $ {dt} .avi"
echo $ FILENAME

if ["$ 1" -eq 218]
then
`/ usr / bin / mencoder -ovc copy -oac pcm -delay 1.5 -mc 10 -o
/ video / motion / $ 2 / $ FILENAME rtsp: //192.168.95.218/mpeg4/media.amp> / dev / null`
elif ["$ 1" -eq 219]
then
`/ usr / bin / mencoder -ovc copy -oac pcm -delay 1.5 -mc 10 -o
/ video / motion / $ 2 / $ FILENAME rtsp: //192.168.95.219/mpeg4/media.amp> / dev / null`
else
`/ usr / bin / mencoder -ovc copy -oac pcm -mc 10 -o / video / motion / $ 2 / $ FILENAME
rtsp: //192.168.95.$1/mpeg4/media.amp> / dev / null`
fi

The script takes two parameters:
1 - the name of the camera, although the name is not quite true, since this is the last octet from the IP
camera addresses. That is, as in our case (we use the 95 subnet - 192.168.95.0/24). Thus, when transmitting the value of 211 - it will be clear that
we want to contact the camera with IP 192.168.95.211
2 - directory where the recorded file will be stored. Made to
it was easier to search. Again in our case: there is a folder / video / motion in
which records are stored, but in order not to interfere with everything in a heap, it contains
Subdirectories of zones: kuhnya, balkon, etc. This means that when transferring the balcon value - the recording will be made in the / video / motion / balkon directory.

So, when calling the script in the form /root/bin/recordstart.sh 211 balkon - script
will write from the camera with IP 192.168.95.211 to the directory / video / motion / balkon.
I hope it is more and more clear :)

As you can see, at first we generate a unique name for the file, in which we will
write, which consists of the following parts:
start - the camera number, or rather the last octet from its IP with the preceding cam.
dt is the current date in the format YYYYMMDDHHmmss.
Example of the file name: cam218-20090924131847.avi - that is, it is written from the camera with IP
192.168.95.218, start date: 2009, 09 month, 24 number, 13 hours, 18 minutes,
47 seconds.

A small explanation: it turned out that we have different camera models that
provide different ways to access video data stream, because of this
had to type in the validation script.
Now go through the mencoder:
-ovc copy - means that the video sequence is copied, since it immediately goes from the camera to
mpeg4
-oac pcm - which codec to use for the audio track, if the camera allows
write sound.
-mc 10 - Maximum value of AV sync adjustment per frame (in
seconds)
-delay 1.5 - Delay in ms, which must be made in each channel

If you have problems with synchronizing video and audio series, then you need to change
the values ​​for the last two parameters are mc and delay.
For the test, you can use mplayer.

Well, now when an event occurs, we can run this script.
Thus, in the config for motion for our camera, we write in the line
on_event_start is something similar:
on_event_start "/root/bin/recordstart.sh 210 koridor1"

And he will start writing :)))
But it still needs to be stopped :)))
To do this, use the second script /root/bin/recordstop.sh.

[root @ video bin] # cat /root/bin/recordstop.sh
#! / bin / bash

# We are using one variable
# 1 - camera name

# Get string with mencoder process PID
MPID = `ps ax | grep mencoder | grep cam $ {1} | awk '{print $ 1}' `

# Kill process
`kill -15 $ MPID`

The script takes one parameter - the same last octet from the IP address.
cameras.

Correspondingly, in the on_event_end line of the configuration file, we write something
like:
on_event_end "/root/bin/recordstop.sh 210"

Well, that's all about the configuration.
We now turn to routine operations :)
I use two, which work on the crown.
1. Removes obsolete files that are more than 21 days old.
2. Combines all the files for the day in one.

Consider a cleanup script. I have it located in the / root / sbin directory
and,
in order that enemies did not guess, is called clean.sh

[root @ video ~] # cat /root/sbin/clean.sh
#! / bin / sh

/ bin / find / video / balkon -name "*. *" -mtime +21 -delete
/ bin / find / video / motion / balkon -name "*. *" -mtime +21 -delete

The first directory stores photos taken by motion - this is an option from
configuration file in the target_dir line.
The second directory is where the video launched from the recordstart.sh script is written

But the second script is more complicated.
[root @ video ~] # cat /root/sbin/concatfiles.py
import os, stat, types, commands

workDir = '/ video / motion /'
dirs = os.listdir (workDir)
oldDate = summaryFile = summaryFileTmp = ''
for dir in dirs:
filesList = os.listdir (workDir + dir)
filesList.sort ()
for file in filesList:
tmpN = file.split ('.')
nm = tmpN [0]
tmpN = nm.split ('-')
if tmpN [0]! = 'S':
nm = tmpN [1]
year = nm [0: 4]
month = nm [4: 6]
day = nm [6: 8]
date = year + month + day
if oldDate! = date:
summaryFile = workDir + dir + '/ S-' + tmpN [0] + '-' + year +
month + day + '.avi'
summaryFileTmp = workDir + dir + '/ S-' + tmpN [0] + '-' + year +
month + day + '-tmp.avi'
#conactenate files
command = '/ usr / bin / mencoder -oac copy -ovc copy' + workDir +
dir + '/' + tmpN [0] + '-' + date + '* .avi -o' + summaryFileTmp
os.system (command)
#fix key frame
command = 'ffmpeg -i' + summaryFileTmp + '-ss 0.04 -vcodec
copy -acodec copy -vcodec copy -acodec copy '+ summaryFile
os.system (command)
#remove tmp file
command = '/ bin / rm -f' + summaryFileTmp
os.system (command)
#remove old files
command = '/ bin / rm -f' + workDir + dir + '/' + tmpN [0] + '-' +
date + '* .avi'
os.system (command)
oldDate = date

This is to combine short clips per day into one total. (For those who do not
in the course - he is on python).
The only thing that needs to be changed is the workDir variable - the path where mencoder
writes its files, all from the same recordstart.sh

Register them in crowns to be performed once a day, preferably at night, until the carriage
will not turn into a pumpkin :)

Run motion as follows:
motion -c / path / to / config / file

Where
/ path / to / config / file - the path to our file with the config :)

And then run them when the system boots.

Well, that's probably all :)
If you have questions - ask :)

UPD: be careful with the script on the python, since the whole got lost
tabulation, which is very critical for him.

Source: https://habr.com/ru/post/72491/


All Articles