Skip to content

Instantly share code, notes, and snippets.

@ghunti
Created July 7, 2016 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghunti/58372b01650916f539d133a96a35efe8 to your computer and use it in GitHub Desktop.
Save ghunti/58372b01650916f539d133a96a35efe8 to your computer and use it in GitHub Desktop.
picamera Copy_to test
import random
import picamera
import sys
from picamera.frames import PiVideoFrameType
def find_seconds(stream, seconds, first_frame=PiVideoFrameType.sps_header):
pos = None
last = None
seconds = int(seconds * 1000000)
for frame in reversed(stream.frames):
print('Frame position: {}'.format(frame.position))
if first_frame in (None, frame.frame_type):
print('First frame found: {}'.format(frame.position))
pos = frame.position
if frame.timestamp is not None:
print('Frame timestamp: {}'.format(frame.timestamp))
if last is None:
last = frame.timestamp
elif last - frame.timestamp >= seconds:
print('Elapsed: {}'.format(last - frame.timestamp))
break
return pos
camera = picamera.PiCamera(resolution="1080p", framerate=30)
stream = picamera.PiCameraCircularIO(camera, seconds=20)
camera.start_recording(stream, format='h264')
try:
while True:
camera.wait_recording(10)
pos = find_seconds(stream, seconds=5)
print('Pos: {}'.format(pos))
stream.copy_to('motion.h264', seconds=5)
sys.exit(0)
finally:
camera.stop_recording()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment