By all means, this is pretty rough and somewhat tailored to a specific need, but I'm sure that it can be adapted to work. I have a few notes in there which hopefully will help. I currently have a keyboard trigger in there, ultimately I plan to use a laser-break or something similar instead, but this is a stand-in for now. The test video thing is simply a place-holder for slow-mo if I can figure that out, my intention being to replay in real speed, then slow motion, then real speed again (the test video is part of a different project where I want it to sync up with a 'perfect' example. It's all stolen from one place or another and cobbled together, if you find any good fixes please let me know and if I can be of any help feel free to contact directly
gdowney@ku.edu:
from __future__ import print_function
try:
input = raw_input
except NameError:
pass
import io
import sys
import picamera
from select import select
import subprocess
from subprocess import call
from subprocess import Popen
import os
import time
#The various /media/test1/… files are on a jump drive, used to test importing and exporting files for view through the pi and for not overly writing to and erasing the SD Card. Test Repo is #output for video capture, Test Videos is stock footage.
cmd = “MP4Box –add /media/test1/TestRepo/wtest.h264 /media/test1/TestRepo/wtest.mp4”
omx = “omxplayer /media/test1/TestRepo/wtest.mp4”
omxs = “omxplayer /media/test1/TestVideos/Lindberg.avi”
#Parameters for recording – 1080P for 25-30fps being primary objective; start circular stream, start recording and overwriting from circular stream. 9 seconds seems about proper amount of #recording time, that will need to be adjusted eventually.
#keyboard inputs (q and w) used temporarily to simulate triggered event
with picamera.PiCamera() as camera:
camera.resolution = (1920,1080)
camera.vflip = True
camera.hflip = True
camera.exposure_mode = ‘sports’
camera.start_preview()
stream = picamera.PiCameraCircularIO(camera, seconds = 9)
camera.start_recording(stream, format=’h264’)
while camera.recording:
while True:
camera.wait_recording(0.5)
r, w, x = select([sys.stdin], [], [], 0.5)
if r:
break
c = input()
if c == ‘q’:
camera.stop_recording()
camera.stop_preview()
elif c == ‘w’:
time.sleep(5)
with stream.lock:
for frame in stream.frame:
if frame.header:
stream.seek(frame.position)
break
with io.open(‘/media/test1/TestRepo/wtest.h264’, ‘wb’) as output:
while True:
buf = stream.read()
if not buf:
break
output.write(buf)
#Convert video file from h264 to mp4 via MP4Box
call([cmd],shell=True)
camera.stop_preview()
#Play video via omxplayer
call([omx],shell=True)
#Play video in slow motion (incomplete). Lindberg video is used simply as a different file to better recognize slow motion attempts, eventually this will be the same video replayed.
call([omxs],shell=True)
camera.start_preview()
os.remove(“/media/test1/TestRepo/wtest.h264”)
os.rename(“/media/test1/TestRepo/wtest.mp4”, time.strftime(“/media/test1/TestRepo/%y%m%d%H%M%S.mp4”)