PYTHON   26

time lapse webio

Guest on 25th August 2022 04:42:28 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.  
  12. # set to pull-up (normally closed position for a door sensor)
  13. GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  14.  
  15. WIDTH = "1920"
  16. HEIGHT = "1080"
  17. DURATION = "15000" # duration in milliseconds. E.G 30000 = 30 seconds
  18. FREQUENCY = "1000" # images to capture per milliseconds E.G 1000 = 1 frame per second
  19. PIC_DIR = "/home/pi/time-lapse/pics/"
  20. WORKING_DIR = "/home/pi/time-lapse/"
  21. IMG_NAME = "image%04d.jpg"
  22.  
  23. FRAMERATE = "1"
  24. MOVIE_FILE = "movie.avi"
  25. YTUBE_TITLE = "Raspberry Pi YouTube Upload"
  26.  
  27. # Twitter Setup
  28. apiKey = 'IAtqr3o6uyzcGoEFtYqouQpac'
  29. apiSecret = 'iPPu2rJM5kMyWcHIJSFyj0MlMnUc3H1EcB6AVsCfc8POMfkAf3'
  30. accessToken = '2307324697-OrCI0j0cBB8OYIhpQYEIOHDjl4Lt2cwayEtzz9I'
  31. accessTokenSecret = 'waRB1iuV1c2sWzfizlR3S7xwJgjefav42UIjdL5d5qSCC'
  32.  
  33. api = Twython(apiKey,apiSecret,accessToken,accessTokenSecret)
  34.  
  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=unlisted"
  41.  
  42. try:
  43.         while True:
  44.  
  45.                 GPIO.wait_for_edge(BUTTON, GPIO.RISING)
  46.  
  47.                 ret = subprocess.call(snapCommand, shell=True)
  48.                 print "Snapshots Done\n"
  49.  
  50.                 os.chdir(PIC_DIR)
  51.  
  52.                 ret = subprocess.call(vidCommand, shell=True)
  53.                 print "Video Done\n"
  54.  
  55.                 os.chdir(WORKING_DIR)
  56.  
  57.                 try:
  58.                         ret = subprocess.check_output([ytubeCommand, r'temp'], shell=True)
  59.                         print ret
  60.                         matchObj = re.match( r"[^\(]*\(video id\: ([0-9a-zA-Z_-]*)\)", ret, re.M|re.I|re.MULTILINE)
  61.                         if matchObj:
  62.                                 youtubeID = matchObj.group(1)
  63.                                 print "I got the ID! " + youtubeID
  64.                                 myTweet = "New Video Uploaded! https://www.youtube.com/watch?v=" + youtubeID
  65.                                 api.update_status(status=myTweet)
  66.                                 print "Tweeted: " + myTweet
  67.                         else:
  68.                                 print "YouTube Upload may have failed. No ID detected!\n"
  69.                                 print "No tweet sent\n"
  70.                 except subprocess.CallProcessError:
  71.                         exec_info = sys.exc_info()
  72.                         return_code = exc_info[1].returncode
  73.                 print "Video Uploaded to YouTube\n"
  74.  
  75.                 print "System Ready\n"
  76.  
  77. except KeyboardInterrupt:
  78.         GPIO.output(SYSTEM_READY_LED,False) ## Turn on GPIO pin 7
  79.         GPIO.cleanup()
  80.  
  81. finally:
  82.         GPIO.cleanup() # ensures a clean exit

Raw Paste


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