Skip to content

Commit 66a655c

Browse files
committed
switches approach to utilize systemd service templates and environment files
1 parent 0479d0e commit 66a655c

File tree

10 files changed

+47
-45
lines changed

10 files changed

+47
-45
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
*.iml

LICENSE.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2023 Path Variable & Ivan Šarić
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1-
# recording-scripts
1+
# recording-scripts v2
22
This repository holds the scripts and a systemd service template I have used when making my bash-based NVR system. You can find a link to the presentation
33
outlining the project [here](https://bit.ly/3n8DRRl).
44

55
The scripts are written in bash.
66

7-
I will outline their purpose here in short.
7+
This is version 2 of the scripts. The second version uses the systemd service template concept as well as the environment file concept
8+
to make deployment of each camera service simpler. All the variable parts of the recording script are stored in an environment file.
9+
The user will create one environment file per camera. The file should specify the camera properties, the base path for the recordings
10+
and the path to record.sh.
811

9-
1. cleanup.sh - This will remove any video files in X path after the given X days that are specified as a command line argument
10-
2. location-room.service - this system service will execute the recording script and make sure it restarts in case of system reboot
11-
3. record-location-room.sh - this is the main recording script. it calls ffmpeg and creates an apendable video file for the whole day
12-
4. remux.sh - this script will convert the previous days recording into an mp4 file extension
13-
5. watchdog.sh - this script serves a watchdog function that will check if the ffmpeg recording has stalled and restart it
12+
There is only one recording script - record.sh. The user should read it to get familiar with the variables and their role. There is no need
13+
to modify the recording script.
1414

15-
Another possible approach when recording is to use the built-in segmentation feature of ffmpeg and have it record in smaller chunks with
16-
a non-appendable format. That way you can still recover from errors and use less space. The drawback is that you will have to concatenate
17-
these small chunks at one point. The operation is not resource intensive but you need to have additional space available during that time,
18-
write a script for it and schedule it with crontab.
15+
There is only one systemd service file - [email protected]. The user needs to edit the file before copying to the systemd service folder and specify
16+
the base path to the environment files and the path to the watchdog script.
17+
18+
19+
In order to deploy a script (once copied to the systemd folder) the user will execute the following command template:
20+
21+
sudo systemd daemon-reload
22+
sudo systemd start [email protected]
23+
sudo systemd enable [email protected]
24+
25+
As a result of this command systemd will look for an environment file named camera.env located in the folder specified in the service file.
26+
It will load all the variables contained within and run watchdog.sh. Watchdog will then run the recording script and poll for liveness every
27+
16 seconds.
28+
29+
The recordings will be written in TS format to allow for appending in case of service failure. There will be only one recording per day.
30+
Each recording will have the format of YYYY_MM_DD.ts
31+
32+
If you have any questions or think you have encountered a bug, please open an issue on [Github](https://github.com/Path-Variable/recording-scripts/issues).

cleanup.sh

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
1-
#/bin/sh
2-
3-
## delete anything older than 7 days
4-
## assumes folder structure like /base/cameras/location/room
5-
find /base/cameras/*/*/* -type f -mtime +7 -exec rm -rf {} \;
6-
1+
find /base/path/to/recordings/*/*/*/ -type d -mtime +3 -exec rm -rf {} \;
72

example.env

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
REC_SCRIPT=/path/to/record.sh
2+
BASE_PATH=/path/to/your/recordings
3+
CAMERA_IP=127.0.0.1
4+
CAMERA_PASSWORD=campass123
5+
CAMERA_LOCATION=zagreb
6+
CAMERA_NAME=hallway

record.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
TS_DAY=$(date +%Y_%m_%d)
2+
mkdir -p -- ${BASE_PATH}/${CAMERA_LOCATION}/${CAMERA_NAME}/
3+
ffmpeg -rtsp_transport tcp -i rtsp://${CAMERA_IP}/user=admin_password=${CAMERA_PASSWORD}_channel=1_stream=0.sdp -c copy -acodec aac -f mpegts - >> ${BASE_PATH}/${CAMERA_LOCATION}/${CAMERA_NAME}/${TS_DAY}.ts
4+
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ After=network.target
44

55
[Service]
66
Type=simple
7-
Environment="REC_SCRIPT=/path/to/script/record_location_room.sh"
7+
EnvironmentFile=path/to/env_files/%i.env
88
Restart=always
99
RestartSec=3
1010
User=your-user
1111
Group=your-group
12-
ExecStart=/bin/bash /path/to/script/watchdog.sh
12+
ExecStart=/bin/bash /path/to/watchdog.sh
1313
TimeoutStartSec=0
1414
WatchdogSec=16
1515
NotifyAccess=all

record_location_room.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

remux_previous_day.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

watchdog.sh

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)