WildKarrde
Posts: 1
Joined: Tue Oct 10, 2017 2:55 am

Is There a Program w/ GUI That Simply Starts & Stops Recording When You Click a Button?

Tue Oct 10, 2017 3:18 am

I'm trying to set up my PiCamera to work like a GoPro camera. I just want to be able to hit a button to start recording, and hit a button to stop recording. The problem I have with the command line recording is that you must pre-set how long the video is supposed to be. To me it's like paying for a tank of gas before you fill up, there's no way to really know how much gas you need until you're done filling up. I'm not looking to stream video either, as I'm setting up my Raspberry to be independant of the internet. Does the program I'm looking for exist?

Currently, I'm using PiVision, set the camera timer to 0, and restart my Raspberry when I'm done recording... but I'd rather have a better solution.

User avatar
topguy
Posts: 7189
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Is There a Program w/ GUI That Simply Starts & Stops Recording When You Click a Button?

Tue Oct 10, 2017 11:33 am

This seems possible with the Python picamera module. https://picamera.readthedocs.io/en/release-1.13/

Code: Select all

import time
import picamera

with picamera.PiCamera() as camera:
    camera.start_preview()
    camera.start_recording('/home/pi/Desktop/video.h264')
    time.sleep(30)
    camera.stop_recording()
    camera.stop_preview()
Just replace the sleep with something that checks for button press.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 11494
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Is There a Program w/ GUI That Simply Starts & Stops Recording When You Click a Button?

Tue Oct 10, 2017 11:52 am

Not quite there, but most of the framework is:
raspivid --help

Code: Select all

-td, --timed       : Cycle between capture and pause. -cycle on,off where on is record time and off is pause time in ms
-s, --signal   : Cycle between capture and pause on Signal
-k, --keypress : Cycle between capture and pause on ENTER 
Easy enough to add another option to wait_for_next_change() where it polls a GPIO every 100ms waiting for a change (probably wants debouncing too).

What did you want the GUI to do?
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

tvjon
Posts: 810
Joined: Mon Jan 07, 2013 9:11 am

Re: Is There a Program w/ GUI That Simply Starts & Stops Recording When You Click a Button?

Tue Oct 10, 2017 12:44 pm

As 6by9 points out, pressing enter is handy...

So, you can buy:

https://www.amazon.co.uk/gp/aw/d/B00MAG ... fie+switch

which you can mount on yourself/ bicycle/ surfboard, micro-lite, etc..

which conveniently sends such when you press the button.

gordon77
Posts: 5790
Joined: Sun Aug 05, 2012 3:12 pm

Re: Is There a Program w/ GUI That Simply Starts & Stops Recording When You Click a Button?

Tue Oct 10, 2017 4:33 pm

No GUI but just connect a physical button (switch between pin 26 and gnd)..

Code: Select all

#!/usr/bin/env python3
import picamera
import datetime
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO_SWITCH = 7  # Physical pin 26, switch to GND
GPIO.setup(GPIO_SWITCH,GPIO.IN,pull_up_down = GPIO.PUD_UP)

print ("  Press Ctrl & C to Quit")

Record = 0

try:
   while True:
      with picamera.PiCamera() as camera:
         camera.resolution = (1280, 720)
         camera.framerate = 25
         while GPIO.input(GPIO_SWITCH) == 1 :
            time.sleep(0.25)
         Record = 1
         print ("Recording")
         now = datetime.datetime.now()
         timestamp = now.strftime("%y%m%d%H%M%S")
         camera.start_preview()
         camera.start_recording(str(timestamp) + '.h264')
         time.sleep(.5)
         while Record == 1:
            camera.wait_recording(.01)
            if GPIO.input(GPIO_SWITCH) == 0:
               print ("Stopped")
               camera.stop_recording()
               Record = 0
               while GPIO.input(GPIO_SWITCH) == 0:
                  time.sleep(0.1)
               time.sleep(.5)
 
except KeyboardInterrupt:
  print ("Quit")
  GPIO.cleanup() 
Last edited by gordon77 on Wed Oct 11, 2017 10:50 am, edited 1 time in total.

gordon77
Posts: 5790
Joined: Sun Aug 05, 2012 3:12 pm

Re: Is There a Program w/ GUI That Simply Starts & Stops Recording When You Click a Button?

Wed Oct 11, 2017 9:58 am

with a simple GUI...

Code: Select all

#!/usr/bin/env python3
import picamera
import datetime
import time
import pygame, sys
from pygame.locals import *

print ("  Press Ctrl & C to Quit")

global greyColor, dgryColor, whiteColor, greenColor, blackColor, redColor, button_width, button_height
greyColor  = pygame.Color(128,128,128)
dgryColor  = pygame.Color(64,64,64)
whiteColor = pygame.Color(255,255,255)
greenColor = pygame.Color(0,255,0)
blackColor = pygame.Color(0,0,0)
redColor   = pygame.Color(255,0,0)
button_width = 100
button_height = 50
Record = 0
pygame.init()
windowSurfaceObj = pygame.display.set_mode((button_width,button_height), pygame.NOFRAME,25)

def key(bcolor,b,msg,fcolor):
   fsize = button_height/3
   y = 0
   if b == 1:
      b = y = button_height/2
   colors = [greyColor,dgryColor, greenColor, blackColor, redColor]
   pygame.draw.rect(windowSurfaceObj,colors[bcolor], Rect(3, b-3, button_width-3, button_height-3))
   if bcolor == 0:
      wColor = whiteColor
      kColor = dgryColor
   else:
      kColor = whiteColor
      wColor = dgryColor
   pygame.draw.rect(windowSurfaceObj,wColor,Rect(0,0,button_width-2,button_height-2),2)
   pygame.draw.line(windowSurfaceObj,kColor,(0,button_height-1),(button_width-1,button_height-1))
   pygame.draw.line(windowSurfaceObj,kColor,(1,button_height-2),(button_width-2,button_height-2))
   pygame.draw.line(windowSurfaceObj,kColor,(button_width-1,0),(button_width-1,button_height))
   pygame.draw.line(windowSurfaceObj,kColor,(button_width-2,1),(button_width-2,button_height-1))
   pygame.draw.rect(windowSurfaceObj,blackColor,Rect(2,2,button_width-4,button_height-4),1)
   fontObj = pygame.font.Font('freesansbold.ttf',int(fsize))
   msgSurfaceObj = fontObj.render(msg,False,colors[fcolor])
   msgRectobj = msgSurfaceObj.get_rect()
   fx = button_width/2 - (len(msg)*fsize)/3
   msgRectobj.topleft = (fx,y+5)
   windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
   pygame.display.update()
   return

key(0,0,"RECORD",1)
key(0,1,"VIDEO",1)

try:
   while True:
      with picamera.PiCamera() as camera:
         camera.resolution = (1280, 720)
         camera.framerate = 25
         while Record == 0 :
            for event in pygame.event.get():
               if event.type == MOUSEBUTTONDOWN :
                  mousex, mousey = event.pos
                  if mousex < button_width and mousey < button_height:
                     Record = 1
                     time.sleep(0.25)
                     key(1,0,"RECORD",4)
         now = datetime.datetime.now()
         timestamp = now.strftime("%y%m%d%H%M%S")
         start = time.time()
         camera.start_recording(str(timestamp) + '.h264')
         while Record == 1:
            length = int(time.time()-start)
            key(1,1,str(length),4)
            camera.wait_recording(.01)
            for event in pygame.event.get():
               if event.type == MOUSEBUTTONDOWN :
                  mousex, mousey = event.pos
                  if mousex < button_width and mousey < button_height:
                     Record = 0
                     key(0,0,"RECORD",1)
                     key(0,1,"VIDEO",1)
                     camera.stop_recording()
              
except KeyboardInterrupt:
  print ("Quit")
 

Return to “Camera board”