Created
October 6, 2016 06:10
-
-
Save rbray89/46a701c66c70f6dd8af85de65e6b738c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import socket | |
import time | |
import datetime as dt | |
import picamera | |
import io | |
import os | |
import signal | |
import sys | |
from threading import Thread | |
def openNext(cam): | |
today = dt.datetime.today().day | |
if cam.recday != today: | |
print 'starting recording for '+str(today) | |
cam.recday = today | |
oldrec = cam.recordings[today] | |
print 'old recording: '+oldrec | |
if oldrec != '': | |
cam.output_file.close() | |
os.remove(oldrec) | |
newfile = cam.filepath+dt.datetime.now().strftime('%Y-%m-%d')+'.h264' | |
cam.recordings[today] = newfile | |
print 'opening ' + newfile | |
cam.output_file = io.open(newfile, 'wb') | |
class PyCamOutput(object): | |
def __init__(self, fp, pycam): | |
self.filepath = fp | |
self.connections = pycam.connections | |
self.recday = -1 | |
self.recordings = ['']*32 | |
openNext(self) | |
def write(self, buf): | |
openNext(self) | |
self.output_file.write(buf) | |
for conn in self.connections: | |
try: | |
conn.write(buf) | |
except: | |
self.connections.remove(conn); | |
def flush(self): | |
self.output_file.flush() | |
for conn in self.connections: | |
try: | |
conn.flush() | |
except: | |
self.connections.remove(conn); | |
def close(self): | |
self.output_file.close() | |
for conn in self.connections: | |
try: | |
conn.close() | |
except: | |
self.connections.remove(conn); | |
def threadedOutputFunction(pyCam): | |
while True: | |
try: | |
pyCam.connections.append(pyCam.serverSocket.accept()[0].makefile('wb')) | |
except Exception as e: | |
print e | |
class PyCam(): | |
def __init__(self): | |
self.connections = [ ] | |
def run(self): | |
self.serverSocket = socket.socket() | |
self.serverSocket.bind(('localhost', 8500)) | |
self.serverSocket.listen(3) | |
thread = Thread(target = threadedOutputFunction, args = (self,)) | |
thread.start() | |
with picamera.PiCamera() as camera: | |
camera.resolution = (640, 480) | |
camera.framerate = 24 | |
camera.hflip = camera.vflip = True | |
camera.annotate_text = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S') | |
pyCamOut = PyCamOutput('/mnt/nas/video-', self) | |
camera.start_recording(pyCamOut, format='h264') | |
while True: | |
try: | |
camera.wait_recording(.2) | |
camera.annotate_text = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S') | |
except Exception as e: | |
print e | |
camera.stop_recording() | |
app = PyCam() | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment