-
Notifications
You must be signed in to change notification settings - Fork 3
/
record.py
executable file
·248 lines (229 loc) · 8.13 KB
/
record.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import datetime
import glob
import multiprocessing
import os
import pathlib
import queue
import re
import signal
import subprocess
import sys
import threading
import time
# Switch to script path
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
RECORDING_PATH = os.getcwd()
RECORDING_LENGTH_S = 300
MIN_FREE_DISK_KB = 200000
FOLDER_DATE_FORMAT = '%Y-%m-%d'
FILE_DATE_FORMAT = '%Y-%m-%d_%H-%M-%S'
"""
Config file format:
<camera1_name> <camera1_url>
<camera2_name> <camera2_url>
...
<cameraN_name> <cameraN_url>
"""
def get_free_space(path):
df = subprocess.Popen(['df', path], stdout=subprocess.PIPE)
output = df.communicate()[0].decode('utf-8')
_device, _size, _used, available, _percent, _mountpoint = output.split('\n')[1].split()
available = int(available)
return available
def record_stream(name, url):
file_name = '{}/{}_{}.mp4'.format(FOLDER_DATE_FORMAT, name, FILE_DATE_FORMAT)
recording_length = str(RECORDING_LENGTH_S)
args = ['nohup',
'ffmpeg',
# URL to record
'-i', url,
# Don't re-encode video, just copy raw data
'-c:v', 'copy',
# Re-encode audio into AAC codec, which is supported by MP4
'-c:a', 'aac',
# Select high-resolution stream
'-map', '0',
# Split recording into small files
'-f', 'segment',
# Set length of each segment
'-segment_time', recording_length,
# Write to mp4 format
'-segment_format', 'mp4',
# Start timestamp at 0 for each segment
'-reset_timestamps', '1',
# Use strftime function to name files
'-strftime', '1',
# Filename template
file_name]
print('Recording ' + name)
p = None
try:
def preexec():
# seems to increase probability of leaving
# behind an uncorrupted video, but not enough
# data to know if this is actually having an effect
os.setpgrp()
p = subprocess.Popen(args,
preexec_fn=preexec,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.DEVNULL,
universal_newlines=True)
def sigterm_handler(signum, frame):
os.kill(p.pid, signal.SIGINT)
signal.signal(signal.SIGTERM, sigterm_handler)
# Manage output using threads
def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()
q_stdout = queue.Queue()
t_stdout = threading.Thread(target=enqueue_output, args=(p.stdout, q_stdout))
t_stdout.daemon = True
t_stdout.start()
q_stderr = queue.Queue()
t_stderr = threading.Thread(target=enqueue_output, args=(p.stderr, q_stderr))
t_stderr.daemon = True
t_stderr.start()
running = True
while running:
try:
_line = q_stdout.get_nowait()
# Could print to a log
except queue.Empty:
# no output
pass
try:
_line = q_stderr.get_nowait()
# Could print to a log
except queue.Empty:
# no output
pass
try:
p.wait(timeout=3)
running = False
except subprocess.TimeoutExpired:
# still running
pass
except KeyboardInterrupt:
print('Interrupt in process ' + name)
os.kill(p.pid, signal.SIGINT)
finally:
print('Cleaning up in process ' + name)
def monitor_folders():
try:
while True:
now = datetime.datetime.today()
folder_name = now.strftime('%Y-%m-%d')
pathlib.Path(folder_name).mkdir(parents=True, exist_ok=True)
if now.hour == 23 and now.minute > 54:
# create folder for next day before ffmpeg has a chance to write
next_day = now + datetime.timedelta(days=1)
folder_name_next_day = next_day.strftime('%Y-%m-%d')
pathlib.Path(folder_name_next_day).mkdir(parents=True, exist_ok=True)
time.sleep(5)
except KeyboardInterrupt:
print('Interrupt in disk monitor process')
finally:
print('Cleaning up disk monitor process')
def get_oldest_recording():
p = re.compile('^.+_(\\d{4}-\\d{2}-\\d{2}_\\d{2}-\\d{2}-\\d{2}).mp4')
oldest_filename = None
oldest_timestamp = 9999999999999
for filename in glob.glob('**/*.mp4'):
result = p.search(filename)
if result is not None:
date_str = result.group(1)
timestamp = time.mktime(datetime.datetime.strptime(date_str, FILE_DATE_FORMAT).timetuple())
if timestamp < oldest_timestamp:
oldest_timestamp = timestamp
oldest_filename = filename
return oldest_filename
def monitor_disk_space():
try:
while True:
avail = get_free_space(RECORDING_PATH)
if avail < MIN_FREE_DISK_KB:
oldest_filename = get_oldest_recording()
print('Disk space low. Deleting oldest video recording: ' + oldest_filename)
os.remove(oldest_filename)
time.sleep(5)
except KeyboardInterrupt:
print('Interrupt in disk monitor process')
finally:
print('Cleaning up disk monitor process')
def checkfor(args):
"""Make sure that a program necessary for using this script is
available.
Arguments:
args -- list of commands to pass to subprocess.call.
"""
if isinstance(args, str):
args = args.split()
try:
with open(os.devnull, 'w') as f:
subprocess.call(args, stderr=subprocess.STDOUT, stdout=f)
except:
print('Required program "{}" not found! exiting.').format(args[0])
sys.exit(1)
def main(argv):
os.chdir(RECORDING_PATH)
checkfor(['ffmpeg', '-version'])
filepath = 'cameras.config'
processes = {}
cameras = {}
with open(filepath) as fp:
while True:
line = fp.readline()
if not line:
break
line = line.strip()
# ignore comments
if len(line) and line[0] == '#':
continue
name, url = line.split()
cameras[name] = url
p = multiprocessing.Process(target=monitor_disk_space)
p.start()
processes[p.sentinel] = 'monitor_disk_space'
p = multiprocessing.Process(target=monitor_folders)
p.start()
processes[p.sentinel] = 'monitor_folders'
for name, url in cameras.items():
p = multiprocessing.Process(target=record_stream, args=(name, url))
p.start()
processes[p.sentinel] = name
try:
from multiprocessing.connection import wait
while True:
sentinels = processes.keys()
exited_sentinels = wait(sentinels)
for sentinel in exited_sentinels:
if processes[sentinel] == 'monitor_disk_space':
del processes[sentinel]
p = multiprocessing.Process(target=monitor_disk_space)
p.start()
processes[p.sentinel] = 'monitor'
elif processes[sentinel] == 'monitor_folders':
del processes[sentinel]
p = multiprocessing.Process(target=monitor_folders)
p.start()
processes[p.sentinel] = 'monitor'
else:
name = processes[sentinel]
del processes[sentinel]
url = cameras[name]
p = multiprocessing.Process(target=record_stream, args=(name, url))
p.start()
processes[p.sentinel] = name
except KeyboardInterrupt:
print('Interrupt in main')
finally:
print('Cleaning up in main')
os.system('stty sane')
if __name__ == '__main__':
main(sys.argv)