-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconfigure.py
executable file
·147 lines (111 loc) · 4.72 KB
/
configure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python3
import configparser
import fileinput
from shutil import copyfile
import os
import sys
import stat
import time
def get_umask():
umask = os.umask(0)
os.umask(umask)
return umask
def chmod_plus_x(path):
os.chmod(
path,
os.stat(path).st_mode |
(
(
stat.S_IXUSR |
stat.S_IXGRP |
stat.S_IXOTH
)
& ~get_umask()
)
)
config = configparser.ConfigParser()
if len(sys.argv) != 2:
print("I need config file location as first paramater in command line")
sys.exit(1)
config_file=sys.argv[1]
config.read(config_file)
cwd = os.getcwd()
startup_amendments = open("start.sh", 'w')
startup_amendments.write("#!/bin/sh \n")
startup_amendments.write("#The content below was created by configure.py \n\n")
startup_amendments.write("./stop.sh \n\n")
image_storage_dir = config["general"]["image_storage_dir"]
image_storage_dir_percent = config["general"]["image_storage_dir_percent"]
if not os.path.exists(image_storage_dir):
os.makedirs(image_storage_dir)
video_storage_dir = config["general"]["video_storage_dir"]
video_storage_dir_percent = config["general"]["video_storage_dir_percent"]
if not os.path.exists(video_storage_dir):
os.makedirs(video_storage_dir)
startup_amendments.write("nohup ./limit-directory-size.sh " + video_storage_dir + " " + video_storage_dir_percent + " 10 > /dev/null 2>&1 < /dev/null &\n")
startup_amendments.write("nohup ./limit-directory-size.sh " + image_storage_dir + " " + image_storage_dir_percent + " 100 > /dev/null 2>&1 < /dev/null &\n\n")
global camerasAI
camerasAI = dict(config['camerasAI'])
global camerasREC
camerasREC = dict(config['camerasREC'])
copyfile("mediamtx/mediamtx.yml.original", "mediamtx/mediamtx.yml")
rtsp_amendments = open("mediamtx/mediamtx.yml", 'a')
for camera_details, uri in camerasAI.items():
friendly_name, camera_type = camera_details.split(',')
rtsp_amendments.write(" " + friendly_name + ":\n")
rtsp_amendments.write(" source: " + uri + "\n")
for camera_details, uri in camerasREC.items():
friendly_name, camera_type = camera_details.split(',')
rtsp_amendments.write(" " + friendly_name + ":\n")
rtsp_amendments.write(" source: " + uri + "\n")
rtsp_amendments.close()
using_rtsp_simple_proxy = int(config["general"]["using_rtsp_simple_proxy"])
if using_rtsp_simple_proxy:
for camera_details, uri in camerasAI.items():
friendly_name, camera_type = camera_details.split(',')
if using_rtsp_simple_proxy:
camerasAI[camera_details]="rtsp://127.0.0.1:8554/" + friendly_name
for camera_details, uri in camerasREC.items():
friendly_name, camera_type = camera_details.split(',')
if using_rtsp_simple_proxy:
camerasAI[camera_details]="rtsp://127.0.0.1:8554/" + friendly_name
startup_amendments.write("cd " + cwd + "/mediamtx\n")
startup_amendments.write("nohup ./mediamtx > /dev/null 2>&1 < /dev/null &\n")
startup_amendments.write("\n")
recorder_amendments = open("recorder.sh", 'w')
recorder_amendments.write("#The content below was created by configure.py \n\n")
recorder_amendments.write("#!/bin/sh\n\n")
recorder_amendments.write("sleep 10\n\n")
recorder_amendments.write("while true; do\n\n")
recorder_amendments.write("NOW=$(date +\"%d-%m-%Y-%H-%M-%S\")\n\n")
for camera_details, uri in camerasAI.items():
friendly_name, camera_type = camera_details.split(',')
directory = config["general"]["image_storage_dir"] + friendly_name + "/"
if not os.path.exists(directory):
os.mkdir(directory)
for camera_details, uri in camerasREC.items():
friendly_name, camera_type = camera_details.split(',')
directory = config["general"]["video_storage_dir"] + friendly_name + "/"
if not os.path.exists(directory):
os.mkdir(directory)
recorder_amendments.write("cd " + directory + " \n")
filename = time.strftime("%d-%m-%Y-%H-%M-%S") + ".mp4"
command_line = "nohup ffmpeg -i '" + uri + "' -acodec copy -vcodec copy $NOW.mp4 > /dev/null 2>&1 < /dev/null &"
recorder_amendments.write(command_line + "\n\n")
recorder_amendments.write("sleep " + str(int(config["general"]["video_storage_slice_minutes"]) * 60) + "\n\n")
recorder_amendments.write("killall ffmpeg\n\n")
recorder_amendments.write("done\n\n")
recorder_amendments.close()
startup_amendments.write("cd " + cwd + "\n\n")
recording_all_video = int(config["general"]["recording_all_video"])
if recording_all_video:
startup_amendments.write("nohup ./recorder.sh > /dev/null 2>&1 < /dev/null & \n\n")
mutelist_reminder_folder = config["general"]["mutelist_reminder_folder"]
if not os.path.exists(mutelist_reminder_folder):
os.mkdir(mutelist_reminder_folder)
startup_amendments.write("nohup python3 CudaCam.py " + config_file + " > /dev/null 2>&1 < /dev/null &\n")
startup_amendments.close()
chmod_plus_x("start.sh")
chmod_plus_x("stop.sh")
chmod_plus_x("recorder.sh")
chmod_plus_x("limit-directory-size.sh")