PYTHON   35

time lapse3 py

Guest on 25th August 2022 04:44:48 AM

  1. #!/usr/bin/python
  2. import os
  3. import subprocess
  4. import RPi.GPIO as GPIO
  5.  
  6. #setup GPIO using Broadcom SOC channel numbering
  7. GPIO.setmode(GPIO.BCM)
  8. BUTTON = 12
  9. SYSTEM_READY_LED = 26 # light when program is ready to be run
  10. CAPTURE_IMAGES_LED = 13 # light when program is capturing JPEGs
  11. CREATE_VIDEO_LED = 21 # light when program is capturing JPEGs
  12. UPLOAD_YOUTUBE_LED = 20 # light when program is capturing JPEGs
  13.  
  14. GPIO.setup(SYSTEM_READY_LED, GPIO.OUT)
  15. GPIO.setup(CAPTURE_IMAGES_LED, GPIO.OUT)
  16. GPIO.setup(CREATE_VIDEO_LED, GPIO.OUT)
  17. GPIO.setup(UPLOAD_YOUTUBE_LED, GPIO.OUT)
  18.  
  19. GPIO.output(SYSTEM_READY_LED, True)
  20.  
  21. # set to pull-up (normally closed position for a door sensor)
  22. GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  23.  
  24. WIDTH = "1280"
  25. HEIGHT = "720"
  26. DURATION = "3000" # duration in milliseconds. E.G 30000 = 30 seconds
  27. FREQUENCY = "2000" # images to capture per milliseconds E.G 1000 = 1 frame per second
  28. PIC_DIR = "/home/pi/time-lapse/pics/"
  29. WORKING_DIR = "/home/pi/time-lapse/"
  30. IMG_NAME = "image%04d.jpg"
  31.  
  32. FRAMERATE = "1"
  33. MOVIE_FILE = "movie.avi"
  34. YTUBE_TITLE = "Raspberry Pi YouTube Upload"
  35.  
  36. snapCommand = "raspistill -t " + DURATION + " -tl " +  FREQUENCY + " -w " + WIDTH +  " -h " + HEIGHT + " -o " + PIC_DIR + IMG_NAME
  37.  
  38. vidCommand = "mencoder \"mf://*.jpg\"  -mf fps=" + FRAMERATE + ":type=jpg -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell:vbitrate=3000 -vf scale=" + WIDTH + ":" + HEIGHT +" -oac copy -o " + MOVIE_FILE
  39.  
  40. ytubeCommand = "sudo python upload_video.py --file=" + PIC_DIR + MOVIE_FILE + " --title=\"Raspberry Pi YouTube Upload\" --description=\"This video was uploaded by my Raspberry Pi\" --category=28 --keywords=\"Raspberry Pi,Python,camera\" --privacyStatus=public"
  41.  
  42. try:
  43.         while True:
  44.  
  45.                 GPIO.wait_for_edge(BUTTON, GPIO.RISING)
  46.                 GPIO.output(SYSTEM_READY_LED, False) # signal program not ready
  47.  
  48.                 GPIO.output(CAPTURE_IMAGES_LED, True)
  49.                 ret = subprocess.call(snapCommand, shell=True)
  50.                 GPIO.output(CAPTURE_IMAGES_LED, False)
  51.                 print "Snapshots Done\n"
  52.  
  53.                 os.chdir(PIC_DIR)
  54.  
  55.                 GPIO.output(CREATE_VIDEO_LED, True) # signal video being created
  56.                 ret = subprocess.call(vidCommand, shell=True)
  57.                 GPIO.output(CREATE_VIDEO_LED, False) # signal video being created
  58.                 print "Video Done\n"
  59.  
  60.                 os.chdir(WORKING_DIR)
  61.  
  62.                 GPIO.output(UPLOAD_YOUTUBE_LED, True) # signal youtube upload
  63.                 proc = subprocess.Popen(ytubeCommand, stdout = subprocess.PIPE, stderr= subprocess.PIPE)
  64.                 out, err = proc.communicate()
  65.                 exitcode = proc.returncode     
  66.  
  67.                 print out
  68.                
  69.                 GPIO.output(UPLOAD_YOUTUBE_LED, False) # signal youtube complete
  70.                 print "Video Uploaded to YouTube\n"
  71.  
  72.                 GPIO.output(CAPTURE_IMAGES_LED, False)
  73.                 GPIO.output(CREATE_VIDEO_LED, False) # signal done
  74.                 GPIO.output(UPLOAD_YOUTUBE_LED, False) # signal done
  75.                 GPIO.output(SYSTEM_READY_LED, True) # signal program ready
  76.                 print "System Ready\n"
  77.  
  78. except KeyboardInterrupt:
  79.         GPIO.output(SYSTEM_READY_LED,False) ## Turn on GPIO pin 7
  80.         GPIO.cleanup()
  81.  
  82. finally:
  83.         GPIO.cleanup() # ensures a clean exit

Raw Paste


Login or Register to edit or fork this paste. It's free.