-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
87 lines (62 loc) · 2.76 KB
/
camera.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
from picamera import PiCamera
import logging
import string
import config as cfg
import utils
class Camera():
''' #TODO
'''
def __init__(self):
''' #TODO
'''
#: PiCam setup
self.cam = PiCamera()
self.cam.resolution = (1280, 720)
#: init logger
self.logger = logging.getLogger(__name__)
#: init variables
self.is_recording = False
self.rec_extension = cfg.CAMERA_RECORDING_FORMAT
self.convert_extension = cfg.CAMERA_CONVERTING_FORMAT
self.video_dir = '{path}/{directory}'.format(path = cfg.FILE_PATH, directory = cfg.VIDEO_DIR)
def start_recording(self) -> None:
''' #TODO
'''
#: Only start if camera is currently not active
if not self.is_recording:
#: Set video name
self.video_name = utils.timestring()
#: Start PiCamera recording
self.is_recording = True
self.cam.start_recording(self._get_video_path(self.video_name, self.rec_extension))
self.logger.debug('recording started: {}'.format(self.video_name + self.rec_extension))
def stop_recording(self) -> str:
''' #TODO
'''
if self.is_recording:
#: save name in case a new video with different name starts during conversion
self.converting_name = self.video_name
#: Stop PiCamera recording
self.cam.stop_recording()
self.logger.debug('recording stopped: {}'.format(self.converting_name + self.convert_extension))
self.is_recording = False
self._convert()
return self._get_video_path(self.converting_name, self.convert_extension)
return None
def _get_video_path(self, name: str, extension: str) -> str:
''' #TODO
'''
return '{folder}/{name}{extension}'.format(folder = self.video_dir, name = name, extension = extension)
def _convert(self) -> None:
''' Converts video captured by PiCamera in known format and deletes old video
'''
#: Get current filename, and filename after converting
before = self._get_video_path(self.converting_name, self.rec_extension)
after = self._get_video_path(self.converting_name, self.convert_extension)
#: Convert command (with semicolon to concatenate)
convert = 'MP4Box -add {before} {after};'.format(before = before, after = after)
#: Delete command
delete = ' rm {file}'.format(file=before)
#: Execute both
utils.shell_cmd(convert + delete)
self.logger.debug('convert and delete video')