-
Notifications
You must be signed in to change notification settings - Fork 1
/
pymedia.py
150 lines (117 loc) · 4.25 KB
/
pymedia.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
#!/usr/bin/env python
#title :pymedia.py
#description :Script for controlling raspberry pi camera and audio
#author :G. Rozzo
#date :20150621
#version :1.2
#usage :
#notes :
#python_version :2.7
#==============================================================================
import time
import os
import datetime
import picamera
import subprocess
import signal
import multiprocessing
from multiprocessing import Queue
class pycamera :
def __init__(self, path, rotate, frame, resolution, bitrate) :
# define path for videos and camera properties
self.VIDEO_PATH = path
self.VIDEO_FRAMERATE = frame
if rotate == True :
self.VIDEO_ROTATE = 180
else :
self.VIDEO_ROTATE = 0
self.RESOLUTIONS = [( 640, 360),(1280, 720),(1920, 1080)]
if resolution == 0 :
self.VIDEO_RESOLUTION = self.RESOLUTIONS[0]
elif resolution == 1 :
self.VIDEO_RESOLUTION = self.RESOLUTIONS[1]
elif resolution == 2 :
self.VIDEO_RESOLUTION = self.RESOLUTIONS[2]
self.VIDEO_BITRATE = bitrate
# Create camera object and setup
self.camera = picamera.PiCamera()
self.camera.framerate = self.VIDEO_FRAMERATE
self.camera.resolution = self.VIDEO_RESOLUTION
self.camera.rotation = self.VIDEO_ROTATE
#camera.start_preview()
def getCamProperties(self) :
properties = [self.VIDEO_RESOLUTION, self.VIDEO_FRAMERATE, self.VIDEO_BITRATE, self.VIDEO_PATH]
return properties
#Get state of camera, Returns true if camera object open, false if camera closed.
def getCamState(self) :
state = True
try :
self.camera._check_camera_open()
except :
state = False
return state
#Check if camera is recording, if it is return True, if not return False
def getCamRecord(self) :
state = False
try :
self.camera._check_recording_stopped()
except :
state = True
return state
# Generates a filename with timestamp
def __GetFileName(self):
filename = time.strftime("%Y%m%d_%H%M%S", time.gmtime())
return filename
def __getTimeStamp(self) :
start_time = time.time()
timestamp = datetime.datetime.fromtimestamp(start_time).strftime('%H:%M:%S')
return timestamp
def startCamRec(self) :
filename = self.__GetFileName()
# get new name for audio file
wavname = self.VIDEO_PATH + filename + ".wav"
# define command to execute for recording via subprocess
record = ["arecord", "-f", "cd", "-D", "plughw:0,0", wavname]
# Start recording
stamp = self.__getTimeStamp()
print('Start recording : ' + stamp + ' : ' + filename + '.h264')
# command to actually start recording
self.camera.start_recording(os.path.join(self.VIDEO_PATH,filename + '.h264'), format='h264', bitrate=self.VIDEO_BITRATE)
# kick off subprocess for recording audio
self.p = subprocess.Popen(record, stdout=subprocess.PIPE)
state = True
return state
def stopCamRec(self) :
print('End Recording: ' + self.__getTimeStamp())
os.kill(self.p.pid, signal.SIGTERM)
self.camera.stop_recording()
return True
#calls wait_recording method, if an error occurs during recording, returns false.
def waitRecording(self, secs) :
state = True
try :
self.camera.wait_recording(secs)
except :
state = False
return state
def rotateCamRec(self, val) :
if val == 0 :
VIDEO_ROTATE = 0
if val == 1 :
VIDEO_ROTATE = 90
if val == 2 :
VIDEO_ROTATE = 180
self.camera.rotation = VIDEO_ROTATE
#cam = pycamera('/home/pi/camera',True,25,2,8000000)
#print(cam.getCamRecord())
#cam.getCamProperties()
#print("state " + str(cam.getCamState()))
#print("recording " + str(cam.getCamRecord()))
#val = cam.startCamRec()
#print(val)
#print("recording " + str(cam.getCamRecord()))
#time.sleep(10)
#val = cam.stopCamRec()
#print(val)
#time.sleep(5)
#exit()