Skip to content

Instantly share code, notes, and snippets.

@parzel
Last active November 22, 2018 21:17
Show Gist options
  • Save parzel/3ba01d76c92dff24475b27045099a126 to your computer and use it in GitHub Desktop.
Save parzel/3ba01d76c92dff24475b27045099a126 to your computer and use it in GitHub Desktop.
Record last n seconds of picamera in ring buffer and replay with vnc from correct position (all in memory)
#!/usr/bin/python3 -u
"""
Record last n seconds of picamera in ring buffer and replay with vnc from correct position (all in memory)
"""
import picamera
from time import sleep
import sys
import subprocess
def record_and_replay(n):
recording_time = n
try:
# Initialize camera
camera = picamera.PiCamera()
camera.resolution = (1600, 1200)
# Start vlc in fullscreen and save piperef
cmdline = ['vlc', '--demux','h264', '--fullscreen', '-']
player = subprocess.Popen(cmdline, stdin=subprocess.PIPE)
# Wait till everything is ready and start preview
sleep(1)
camera.start_preview()
# Start recording
ring_buffer = picamera.PiCameraCircularIO(camera, seconds=recording_time+1)
camera.start_recording(ring_buffer,'h264')
while True:
camera.wait_recording(5)
# Stop recording
camera.stop_recording()
# Write to vlc stdin
ring_buffer.copy_to(player.stdin, seconds=recording_time)
# Now stop preview
sleep(1)
camera.stop_preview()
# Clear the ring buffer
ring_buffer.clear()
# Wait for recording to be played
sleep(recording_time)
# Restart recording and preview
camera.start_preview()
camera.start_recording(ring_buffer,'h264')
except:
camera.stop_recording()
camera.stop_preview()
player.kill()
if __name__ == "__main__":
record_and_replay(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment