Skip to content

Instantly share code, notes, and snippets.

@jarpy
Last active July 29, 2016 14:42
Show Gist options
  • Save jarpy/868092c8f5ad301307b02ad9fcf735bc to your computer and use it in GitHub Desktop.
Save jarpy/868092c8f5ad301307b02ad9fcf735bc to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import RPi.GPIO as GPIO
import picamera
import subprocess
# Set up a pin on the GPIO connector for the "record" button.
# We'll use pin 18, which is here:
#
# ....X........
# .............
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#
# Because we set it to "PUD_UP", it's "pulled-up" to a high voltage most of
# the time. To activate it, we'll use a button to short it to ground, which
# is here (among other places):
#
# .X...........
# .............
#
# So wire a button between those two pins and you're all set.
print "Waiting for button push..."
while True:
# Check the voltage on pin 18 (it's high most of the time).
high_voltage_on_pin_18 = GPIO.input(18)
# But if someone pushes the button, pin 18 will be shorted to ground.
# If that happens the voltage will be low (zero) so that's how we know
# the button was pushed.
#
# In that case, go through the cycle of recording a new video and playing
# it.
if not high_voltage_on_pin_18:
# The first time through, we won't have any video playing, but every
# subsequent time, we'll already be playing the last video that we
# recorded. We should stop playing that before we do anything else.
print "Stopping video player (if any)."
# Rather than mess around with killing the video player in pure Python,
# let's cheat and just do the same thing we would do in the
# command-line shell.
subprocess.call('killall omxplayer.bin', shell=True)
print "Preparing camera."
# Squeeze as many frames-per-second as we can out of the little
# PiCam. Specifically, using "mode 7" lets us go up to 90 fps.
cam = picamera.PiCamera(framerate=90, sensor_mode=7)
# ...but everything is a trade-off. We can't have high resolution
# _and_ a high framerate. I'm thinking that temporal resolution is
# more valuable here than spatial resolution. It's easy to tweak if
# I'm wrong.
cam.resolution = (640, 480)
print "Recording..."
cam.start_recording("thevid.mjpeg")
cam.wait_recording(5)
# We should have 5 seconds of video now, in "thevid.mjpeg".
print "Done recording."
cam.stop_recording()
print "Resetting camera."
cam.close()
print "Playing..."
subprocess.Popen(['omxplayer', '--no-keys', '--loop', 'thevid.mjpeg'])
#
# There's some odd tricks here...
#
# "subprocess.Popen" let's us run the player in the background, so
# this program keeps running too, waiting for the next button press.
#
# '--no-keys' prevents the video player from expecting to have a
# keyboard to control it, which it won't because we put it in the
# background (see above :D ).
#
# '--loop' will play the video in a loop until we say otherwise.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment