Lucas Teske revised this gist 11 years ago. Go to revision
1 file changed, 148 insertions
shutter.py(file created)
| @@ -0,0 +1,148 @@ | |||
| 1 | + | #!/usr/bin/env python | |
| 2 | + | import urllib | |
| 3 | + | from urllib import urlencode | |
| 4 | + | import urllib2 | |
| 5 | + | from time import sleep | |
| 6 | + | import os | |
| 7 | + | from subprocess import Popen, PIPE | |
| 8 | + | import Image | |
| 9 | + | from cStringIO import StringIO | |
| 10 | + | import signal | |
| 11 | + | import sys | |
| 12 | + | ||
| 13 | + | framerate = 10 | |
| 14 | + | frame = 0 | |
| 15 | + | framecount = 60000 | |
| 16 | + | global password | |
| 17 | + | global keep | |
| 18 | + | ||
| 19 | + | keep = True | |
| 20 | + | ||
| 21 | + | def signal_handler(signal, frame): | |
| 22 | + | global keep | |
| 23 | + | print('You pressed Ctrl+C!') | |
| 24 | + | keep = False | |
| 25 | + | sys.exit(0) | |
| 26 | + | ||
| 27 | + | def GoProCMD(location, cmd, data): | |
| 28 | + | global password | |
| 29 | + | url = "http://10.5.5.9/%s/%s?t=%s&p=%s" %(location,cmd,password,data) | |
| 30 | + | ret = urllib2.urlopen(url).read() | |
| 31 | + | return ret | |
| 32 | + | ||
| 33 | + | def GetImageName(): | |
| 34 | + | data = urllib2.urlopen("http://10.5.5.9:8080/DCIM/").read() | |
| 35 | + | data = data.split("\"") | |
| 36 | + | folder = None | |
| 37 | + | for i in data: | |
| 38 | + | if "GOPRO" in i: | |
| 39 | + | folder = i.replace("/","") | |
| 40 | + | break | |
| 41 | + | if folder != None: | |
| 42 | + | #print "Found folder %s" %folder | |
| 43 | + | data = urllib2.urlopen("http://10.5.5.9:8080/DCIM/%s/" %folder).read() | |
| 44 | + | data = data.split("\"") | |
| 45 | + | image = None | |
| 46 | + | for i in data: | |
| 47 | + | if ".JPG" in i: | |
| 48 | + | image = i | |
| 49 | + | break | |
| 50 | + | if image != None: | |
| 51 | + | #print "Found Image %s"%image | |
| 52 | + | return "http://10.5.5.9:8080/DCIM/%s/%s" %(folder, image), image | |
| 53 | + | else: | |
| 54 | + | print "NOT FOUND" | |
| 55 | + | return None, None | |
| 56 | + | else: | |
| 57 | + | print "NOT FOUND" | |
| 58 | + | return None, None | |
| 59 | + | ||
| 60 | + | def GetWifiPassword(): | |
| 61 | + | return urllib2.urlopen("http://10.5.5.9/bacpac/sd").read().split("\n")[1] | |
| 62 | + | ||
| 63 | + | def PhotoMode(): | |
| 64 | + | return GoProCMD("camera","CM","%01") | |
| 65 | + | ||
| 66 | + | def TakePicture(): | |
| 67 | + | return GoProCMD("bacpac","SH","%01") | |
| 68 | + | ||
| 69 | + | def DeleteLast(): | |
| 70 | + | return GoProCMD("camera","DL","") | |
| 71 | + | ||
| 72 | + | def DeleteAll(): | |
| 73 | + | return GoProCMD("camera","DA","") | |
| 74 | + | ||
| 75 | + | def WBAuto(): | |
| 76 | + | return GoProCMD("camera","WB", "%00") | |
| 77 | + | ||
| 78 | + | def WBRAW(): | |
| 79 | + | return GoProCMD("camera","WB", "%04") | |
| 80 | + | ||
| 81 | + | def Picture5MP(): | |
| 82 | + | return GoProCMD("camera","PR","%03") | |
| 83 | + | ||
| 84 | + | def Picture7MP(): | |
| 85 | + | return GoProCMD("camera","PR","%04") | |
| 86 | + | ||
| 87 | + | def Picture10MP(): | |
| 88 | + | return GoProCMD("camera","PR","%08") | |
| 89 | + | ||
| 90 | + | password = GetWifiPassword() | |
| 91 | + | print "WiFi Password: %s" %password | |
| 92 | + | print "Deleting all" | |
| 93 | + | DeleteAll() | |
| 94 | + | sleep(2) | |
| 95 | + | ret = PhotoMode() | |
| 96 | + | print "Photo Mode: %s" %ret | |
| 97 | + | print "Setting to RAW" | |
| 98 | + | WBRAW() | |
| 99 | + | ||
| 100 | + | print "Setting to 7MP (Wide)" | |
| 101 | + | Picture7MP() | |
| 102 | + | ||
| 103 | + | print "Opening FFMPEG" | |
| 104 | + | #p = Popen(['ffmpeg', '-y', '-f', 'image2pipe', '-vcodec', 'mjpeg', '-r', '%s'%framerate, '-i', '-', '-vcodec', 'libx264', '-pix_fmt', 'yuvj420p', '-r', '%s'%framerate, 'video.mp4'], stdin=PIPE) | |
| 105 | + | p = Popen(['ffmpeg', '-y', '-f', 'image2pipe', '-vcodec', 'mjpeg', '-r', '%s'%framerate, '-i', '-', '-vcodec', 'libvpx', '-qmin' , '0' , '-qmax' , '5' , '-crf', '4', '-pix_fmt', 'yuv420p', '-r', '%s'%framerate, 'video.webm'], stdin=PIPE) | |
| 106 | + | ||
| 107 | + | #p = Popen(['ffmpeg', '-y', '-f', 'image2pipe', '-vcodec', 'mjpeg', '-i', '-', '-vcodec', 'libx264', '-s', 'hd720', '-pix_fmt', 'yuv420p', '-f','flv', 'rtmp://localhost/live/gopro'], stdin=PIPE) | |
| 108 | + | ||
| 109 | + | signal.signal(signal.SIGINT, signal_handler) | |
| 110 | + | while frame != framecount: | |
| 111 | + | global keep | |
| 112 | + | print "Taking a picture %s" %frame | |
| 113 | + | try: | |
| 114 | + | ret = TakePicture() | |
| 115 | + | sleep(2) | |
| 116 | + | url, name = GetImageName() | |
| 117 | + | except: | |
| 118 | + | print "Connection Error" | |
| 119 | + | name = None | |
| 120 | + | if name == None: | |
| 121 | + | print "MISSING FRAME" | |
| 122 | + | else: | |
| 123 | + | print "Downloading Image: %s" %url | |
| 124 | + | try: | |
| 125 | + | data = urllib2.urlopen(url,timeout=3) | |
| 126 | + | size = int(data.headers["Content-Length"]) | |
| 127 | + | data = data.read() | |
| 128 | + | if len(data) == size: | |
| 129 | + | p.stdin.write(data) | |
| 130 | + | f = open("pics/%s"%name,"w") | |
| 131 | + | f.write(data) | |
| 132 | + | f.close() | |
| 133 | + | else: | |
| 134 | + | print "Bad frame!" | |
| 135 | + | except: | |
| 136 | + | print "Error receiving frame" | |
| 137 | + | print "Deleting" | |
| 138 | + | try: | |
| 139 | + | DeleteLast() | |
| 140 | + | sleep(2) | |
| 141 | + | except: | |
| 142 | + | print "Connection Error" | |
| 143 | + | frame += 1 | |
| 144 | + | if not keep: | |
| 145 | + | print "Got Signal to exit" | |
| 146 | + | break | |
| 147 | + | ||
| 148 | + | os.killpg(p.pid, signal.SIGTERM) | |
Newer
Older