- #!/usr/bin/python
- import os
- import re
- import subprocess
- import RPi.GPIO as GPIO
- from twython import Twython
- #setup GPIO using Broadcom SOC channel numbering
- GPIO.setmode(GPIO.BCM)
- BUTTON = 12
- SYSTEM_READY_LED = 26 # light when program is ready to be run
- CAPTURE_IMAGES_LED = 13 # light when program is capturing JPEGs
- CREATE_VIDEO_LED = 21 # light when program is capturing JPEGs
- UPLOAD_YOUTUBE_LED = 20 # light when program is capturing JPEGs
- GPIO.setup(SYSTEM_READY_LED, GPIO.OUT)
- GPIO.setup(CAPTURE_IMAGES_LED, GPIO.OUT)
- GPIO.setup(CREATE_VIDEO_LED, GPIO.OUT)
- GPIO.setup(UPLOAD_YOUTUBE_LED, GPIO.OUT)
- GPIO.output(SYSTEM_READY_LED, True)
- # set to pull-up (normally closed position for a door sensor)
- GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- WIDTH = "1920"
- HEIGHT = "1080"
- DURATION = "15000" # duration in milliseconds. E.G 30000 = 30 seconds
- FREQUENCY = "1000" # images to capture per milliseconds E.G 1000 = 1 frame per second
- PIC_DIR = "/home/pi/time-lapse/pics/"
- WORKING_DIR = "/home/pi/time-lapse/"
- IMG_NAME = "image%04d.jpg"
- FRAMERATE = "1"
- MOVIE_FILE = "movie.avi"
- YTUBE_TITLE = "Raspberry Pi YouTube Upload"
- # Twitter Setup
- apiKey = 'IAtqr3o6uyzcGoEFtYqouQpac'
- apiSecret = 'iPPu2rJM5kMyWcHIJSFyj0MlMnUc3H1EcB6AVsCfc8POMfkAf3'
- accessToken = '2307324697-OrCI0j0cBB8OYIhpQYEIOHDjl4Lt2cwayEtzz9I'
- accessTokenSecret = 'waRB1iuV1c2sWzfizlR3S7xwJgjefav42UIjdL5d5qSCC'
- api = Twython(apiKey,apiSecret,accessToken,accessTokenSecret)
- snapCommand = "raspistill -t " + DURATION + " -tl " + FREQUENCY + " -w " + WIDTH + " -h " + HEIGHT + " -o " + PIC_DIR + IMG_NAME
- 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
- 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"
- try:
- while True:
- GPIO.wait_for_edge(BUTTON, GPIO.RISING)
- GPIO.output(SYSTEM_READY_LED, False) # signal program not ready
- GPIO.output(CAPTURE_IMAGES_LED, True)
- ret = subprocess.call(snapCommand, shell=True)
- GPIO.output(CAPTURE_IMAGES_LED, False)
- print "Snapshots Done\n"
- os.chdir(PIC_DIR)
- GPIO.output(CREATE_VIDEO_LED, True) # signal video being created
- ret = subprocess.call(vidCommand, shell=True)
- GPIO.output(CREATE_VIDEO_LED, False) # signal video being created
- print "Video Done\n"
- os.chdir(WORKING_DIR)
- GPIO.output(UPLOAD_YOUTUBE_LED, True) # signal youtube upload
- try:
- ret = subprocess.check_output([ytubeCommand, r'temp'], shell=True)
- print ret
- matchObj = re.match( r"[^\(]*\(video id\: ([0-9a-zA-Z_-]*)\)", ret, re.M|re.I|re.MULTILINE)
- if matchObj:
- youtubeID = matchObj.group(1)
- print "I got the ID! " + youtubeID
- myTweet = "New Video Uploaded! https://www.youtube.com/watch?v=" + youtubeID
- api.update_status(status=myTweet)
- print "Tweeted: " + myTweet
- else:
- print "YouTube Upload may have failed. No ID detected!\n"
- print "No tweet sent\n"
- except subprocess.CallProcessError:
- exec_info = sys.exc_info()
- return_code = exc_info[1].returncode
- GPIO.output(UPLOAD_YOUTUBE_LED, False) # signal youtube complete
- print "Video Uploaded to YouTube\n"
- GPIO.output(CAPTURE_IMAGES_LED, False)
- GPIO.output(CREATE_VIDEO_LED, False) # signal done
- GPIO.output(UPLOAD_YOUTUBE_LED, False) # signal done
- GPIO.output(SYSTEM_READY_LED, True) # signal program ready
- print "System Ready\n"
- except KeyboardInterrupt:
- GPIO.output(SYSTEM_READY_LED,False) ## Turn on GPIO pin 7
- GPIO.cleanup()
- finally:
- GPIO.cleanup() # ensures a clean exit
Raw Paste