-
Notifications
You must be signed in to change notification settings - Fork 0
/
reosnap.py
executable file
·317 lines (230 loc) · 9.15 KB
/
reosnap.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/bin/python3
#https://github.com/andrea-varesio/ReoSnap
#version = 20230103.02
'''Save live snapshots of Reolink cameras'''
import argparse
import asyncio
import datetime
import os
import pathlib
import sys
import time
from shutil import which
import requests
try:
from PIL import Image
NO_PIL = False
except ModuleNotFoundError:
NO_PIL = True
cwd = os.path.dirname(os.path.realpath(__file__))
def show_license():
'''Show License'''
print('\n**************************************************')
print('"ReoSnap": Save live snapshots of Reolink camera feeds')
print('Copyright (C) 2022-2023 Andrea Varesio (https://www.andreavaresio.com/).')
print('This program comes with ABSOLUTELY NO WARRANTY')
print('This is free software, and you are welcome to redistribute it under certain conditions')
print('Full license available at https://github.com/andrea-varesio/ReoSnap')
print('**************************************************\n\n')
def parse_arguments():
'''Parse arguments'''
arg = argparse.ArgumentParser()
res_group = arg.add_mutually_exclusive_group()
time_group = arg.add_mutually_exclusive_group()
res_group.add_argument('-r', '--resolution', help='[low/medium/high/max]', type=str)
res_group.add_argument('--width', help='Width', type=int)
res_group.add_argument('--height', help='Height', type=int)
arg.add_argument('-o', '--optimize', help='Optimize image', action='store_true')
arg.add_argument('-q', '--quality', help='[low/medium/high/max/0-100]', type=str)
arg.add_argument('-k', '--keep-og', help='Keep original files', action='store_true')
time_group.add_argument('-H', '--hours', help='Hours', type=int)
time_group.add_argument('-m', '--minutes', help='Minutes', type=int)
time_group.add_argument('-s', '--seconds', help='Seconds', type=int)
arg.add_argument('-i', '--interval', help='Snapshot interval (default: 4s)', type=int)
arg.add_argument('-O', '--output', help='Path to output directory', type=str)
arg.add_argument('-t', '--tmux', help='Run in tmux detached session', action='store_true')
arg.add_argument('-v', '--verbose', help='Enable verbosity', action='store_true')
arg.add_argument('-l', '--license', help='Show License', action='store_true')
return arg.parse_args()
def verbose(text):
'''Print text if verbosity is enabled'''
args = parse_arguments()
if args.verbose:
print(text)
def get_date():
'''Get current date'''
return datetime.datetime.now().strftime('%Y%m%d')
def get_timestamp():
'''Get current timestamp'''
return datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
def get_file_res():
'''Get snapshot resolution'''
args = parse_arguments()
res_levels = ['low', 'medium', 'high', 'max']
if args.resolution and args.resolution not in res_levels:
print('Invalid resolution')
sys.exit(1)
if args.resolution in [res_levels[3], 'M']:
resolution = [2560, 1920]
elif args.resolution in [res_levels[2], 'h']:
resolution = [2048, 1536]
elif args.resolution in [res_levels[1], 'm']:
resolution = [1856, 1392]
elif args.resolution in [res_levels[0], 'l']:
resolution = [1600, 1200]
elif args.width:
resolution = [args.width, args.width * 3 / 4]
elif args.height:
resolution = [args.height * 4 / 3, args.height]
else:
resolution = [1856, 1392]
return resolution
def get_url(cam_ip, username, password):
'''Get API url'''
if not cam_ip.startswith('http'):
cam_ip = f'http://{cam_ip}'
url_base = 'cgi-bin/api.cgi?cmd=Snap&channel=0'
res_param = f'width={get_file_res()[0]}&height={get_file_res()[1]}'
now = get_timestamp()
return f'{cam_ip}/{url_base}&{res_param}&rs={now}&user={username}&password={password}'
def get_output_dir():
'''Get output directory'''
args = parse_arguments()
if not args.output:
return os.path.join(pathlib.Path.home(), 'Surveillance')
if os.path.isdir(args.output):
if args.output.startswith('./'):
output_dir_root = os.path.join(os.getcwd(), args.output.replace('./', '', 1))
elif args.output == '.':
output_dir_root = args.output.replace('.', os.getcwd())
else:
output_dir_root = args.output
return os.path.join(output_dir_root, 'Surveillance')
print('Invalid output path')
sys.exit(1)
def get_filepath(cam_name):
'''Get camera variables'''
cam_dir = os.path.join(get_output_dir(), get_date(), cam_name)
filename = f'{get_timestamp()}_{cam_name}_snapshot.jpg'
if not os.path.isdir(cam_dir):
os.makedirs(cam_dir)
return os.path.join(cam_dir, filename)
def get_file_quality():
'''Get file quality'''
args = parse_arguments()
quality_levels = ['low', 'medium', 'high', 'max']
if args.quality in [quality_levels[3], 'M']:
return 100
if args.quality in [quality_levels[2], 'h']:
return 75
if args.quality in [quality_levels[1], 'm']:
return 50
if args.quality in [quality_levels[0], 'l']:
return 25
if args.quality:
return int(args.quality)
return 40
async def save_snapshot(cam_no, cam_ip, username, password):
'''Save optimized snapshot of camera feed'''
args = parse_arguments()
cam_name = f'cam_{cam_no}'
file_path = get_filepath(cam_name)
url = get_url(cam_ip, username, password)
with open(file_path, 'wb') as snapshot_raw:
snapshot_raw.write(requests.get(url).content)
if args.optimize:
with Image.open(file_path) as snapshot:
snapshot.save( file_path.replace('.jpg', '_optimized.jpg'),
optimize=True, quality=get_file_quality())
if not args.keep_og:
os.remove(file_path)
def get_cam_feed():
'''Get camera feed'''
cam_no = 0
with open(os.path.join(cwd, 'credentials.txt'), 'r', encoding='utf-8') as creds:
for line in creds.readlines():
if not line.startswith('#'):
cam_no += 1
cam_ip = line.split(',')[0]
username = line.split(',')[1]
password = line.split(',')[2].rstrip()
asyncio.run(save_snapshot(cam_no, cam_ip, username, password))
def get_interval():
'''Get snapshots interval'''
args = parse_arguments()
if args.interval:
return args.interval
return 4
def get_rec_period():
'''Get number of files to keep'''
args = parse_arguments()
if args.hours:
return args.hours * 3600 / get_interval()
if args.minutes:
return args.minutes * 60 / get_interval()
if args.seconds:
return args.seconds / get_interval()
return 12 * 3600 / get_interval()
def run_checks():
'''Run required checks'''
args = parse_arguments()
if args.license:
show_license()
sys.exit(0)
if args.optimize and NO_PIL:
print('Enabled image optimization but missing required package: "Pillow"')
print('Install required package ("pip install Pillow"), or disable image optimization')
sys.exit(1)
if args.tmux:
if which('tmux') is None:
print('Requested tmux session but missing required package: "tmux"')
print('Install required package "tmux", or remove [-t, --tmux] argument')
sys.exit(1)
argv = sys.argv
argv.pop(0)
while '--tmux' in argv:
argv.remove('--tmux')
while '-t' in argv:
argv.remove('-t')
cmd = f'{os.path.realpath(__file__)} {" ".join(argv)}'
os.system(f'tmux new-session -d "python3 {cmd}"')
print('Launched script in new tmux session')
print('Type "tmux attach" to view progress')
sys.exit(0)
def main():
'''Main function'''
run_checks()
interval = get_interval()
rec_period = get_rec_period()
output_dir = get_output_dir()
i = 0
while True:
get_cam_feed()
i += 1
verbose(f'Saved snapshot(s): {get_timestamp()} | #{i}')
date_dir = os.path.join(output_dir, min(os.listdir(output_dir)))
for cam_dir in os.listdir(date_dir):
oldest_dir = os.listdir(os.path.join(date_dir, cam_dir))
if not oldest_dir:
try:
os.rmdir(os.path.join(date_dir, cam_dir))
except FileNotFoundError:
print('FileNotFoundError')
if len(os.listdir(date_dir)) == 0:
try:
os.rmdir(date_dir)
except FileNotFoundError:
print('FileNotFoundError')
finally:
date_dir = os.path.join(output_dir, min(os.listdir(output_dir)))
if i > rec_period:
for cam_dir in os.listdir(date_dir):
oldest_dir = os.listdir(os.path.join(date_dir, cam_dir))
oldest_file = min(oldest_dir)
try:
os.remove(os.path.join(date_dir, cam_dir, oldest_file))
except FileNotFoundError:
print(f'FileNotFoundError: {os.path.join(date_dir, cam_dir, oldest_file)}')
time.sleep(interval)
if __name__ == '__main__':
main()