PYTHON   37

time lapse2

Guest on 25th August 2022 04:43:57 AM

  1. #!/usr/bin/python
  2. import os
  3. import re
  4. import subprocess
  5. import RPi.GPIO as GPIO
  6. from twython import Twython
  7.  
  8. #setup GPIO using Broadcom SOC channel numbering
  9. GPIO.setmode(GPIO.BCM)
  10. BUTTON = 12
  11. SYSTEM_READY_LED = 26 # light when program is ready to be run
  12. CAPTURE_IMAGES_LED = 13 # light when program is capturing JPEGs
  13. CREATE_VIDEO_LED = 21 # light when program is capturing JPEGs
  14. UPLOAD_YOUTUBE_LED = 20 # light when program is capturing JPEGs
  15.  
  16. GPIO.setup(SYSTEM_READY_LED, GPIO.OUT)
  17. GPIO.setup(CAPTURE_IMAGES_LED, GPIO.OUT)
  18. GPIO.setup(CREATE_VIDEO_LED, GPIO.OUT)
  19. GPIO.setup(UPLOAD_YOUTUBE_LED, GPIO.OUT)
  20.  
  21. GPIO.output(SYSTEM_READY_LED, True)
  22.  
  23. # set to pull-up (normally closed position for a door sensor)
  24. GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  25.  
  26. WIDTH = "1920"
  27. HEIGHT = "1080"
  28. DURATION = "15000" # duration in milliseconds. E.G 30000 = 30 seconds
  29. FREQUENCY = "1000" # images to capture per milliseconds E.G 1000 = 1 frame per second
  30. PIC_DIR = "/home/pi/time-lapse/pics/"
  31. WORKING_DIR = "/home/pi/time-lapse/"
  32. IMG_NAME = "image%04d.jpg"
  33.  
  34. FRAMERATE = "1"
  35. MOVIE_FILE = "movie.avi"
  36. YTUBE_TITLE = "Raspberry Pi YouTube Upload"
  37.  
  38. # Twitter Setup
  39. apiKey = 'IAtqr3o6uyzcGoEFtYqouQpac'
  40. apiSecret = 'iPPu2rJM5kMyWcHIJSFyj0MlMnUc3H1EcB6AVsCfc8POMfkAf3'
  41. accessToken = '2307324697-OrCI0j0cBB8OYIhpQYEIOHDjl4Lt2cwayEtzz9I'
  42. accessTokenSecret = 'waRB1iuV1c2sWzfizlR3S7xwJgjefav42UIjdL5d5qSCC'
  43.  
  44. api = Twython(apiKey,apiSecret,accessToken,accessTokenSecret)
  45.  
  46.  
  47. snapCommand = "raspistill -t " + DURATION + " -tl " +  FREQUENCY + " -w " + WIDTH +  " -h " + HEIGHT + " -o " + PIC_DIR + IMG_NAME
  48.  
  49. 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
  50.  
  51. 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=unlisted"
  52.  
  53. try:
  54.         while True:
  55.  
  56.                 GPIO.wait_for_edge(BUTTON, GPIO.RISING)
  57.                 GPIO.output(SYSTEM_READY_LED, False) # signal program not ready
  58.  
  59.                 GPIO.output(CAPTURE_IMAGES_LED, True)
  60.                 ret = subprocess.call(snapCommand, shell=True)
  61.                 GPIO.output(CAPTURE_IMAGES_LED, False)
  62.                 print "Snapshots Done\n"
  63.  
  64.                 os.chdir(PIC_DIR)
  65.  
  66.                 GPIO.output(CREATE_VIDEO_LED, True) # signal video being created
  67.                 ret = subprocess.call(vidCommand, shell=True)
  68.                 GPIO.output(CREATE_VIDEO_LED, False) # signal video being created
  69.                 print "Video Done\n"
  70.  
  71.                 os.chdir(WORKING_DIR)
  72.  
  73.                 GPIO.output(UPLOAD_YOUTUBE_LED, True) # signal youtube upload
  74.                 try:
  75.                         ret = subprocess.check_output([ytubeCommand, r'temp'], shell=True)
  76.                         print ret
  77.                         matchObj = re.match( r"[^\(]*\(video id\: ([0-9a-zA-Z_-]*)\)", ret, re.M|re.I|re.MULTILINE)
  78.                         if matchObj:
  79.                                 youtubeID = matchObj.group(1)
  80.                                 print "I got the ID! " + youtubeID
  81.                                 myTweet = "New Video Uploaded! https://www.youtube.com/watch?v=" + youtubeID
  82.                                 api.update_status(status=myTweet)
  83.                                 print "Tweeted: " + myTweet
  84.                         else:
  85.                                 print "YouTube Upload may have failed. No ID detected!\n"
  86.                                 print "No tweet sent\n"
  87.                 except subprocess.CallProcessError:
  88.                         exec_info = sys.exc_info()
  89.                         return_code = exc_info[1].returncode
  90.                 GPIO.output(UPLOAD_YOUTUBE_LED, False) # signal youtube complete
  91.                 print "Video Uploaded to YouTube\n"
  92.  
  93.                 GPIO.output(CAPTURE_IMAGES_LED, False)
  94.                 GPIO.output(CREATE_VIDEO_LED, False) # signal done
  95.                 GPIO.output(UPLOAD_YOUTUBE_LED, False) # signal done
  96.                 GPIO.output(SYSTEM_READY_LED, True) # signal program ready
  97.                 print "System Ready\n"
  98.  
  99. except KeyboardInterrupt:
  100.         GPIO.output(SYSTEM_READY_LED,False) ## Turn on GPIO pin 7
  101.         GPIO.cleanup()
  102.  
  103. finally:
  104.         GPIO.cleanup() # ensures a clean exit

Raw Paste


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