forked from unlimitedbacon/um3timelapse
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimelapse.py
executable file
·130 lines (113 loc) · 3.78 KB
/
timelapse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/python3
import os
import argparse
from requests import exceptions
from tempfile import mkdtemp
from time import sleep
from urllib.request import urlopen
from um3api import Ultimaker3
import json
import math
cliParser = argparse.ArgumentParser(description=
'Creates a time lapse video from the onboard camera on your Ultimaker 3.')
cliParser.add_argument('HOST', type=str,
help='IP address of the Ultimaker 3')
cliParser.add_argument('POST_SEC', type=float,
help='Seconds of postroll, or how much time to capture after the print is completed.')
cliParser.add_argument('OUTFILE', type=str,
help='Name of the video file to create. Recommended formats are .mkv or .mp4.')
options = cliParser.parse_args()
imgurl = "http://" + options.HOST + ":8080/?action=snapshot"
api = Ultimaker3(options.HOST, "Timelapse")
#api.loadAuth("auth.data")
def printing():
status = None
# If the printer gets disconnected, retry indefinitely
while status == None:
try:
status = api.get("api/v1/printer/status").json()
if status == 'printing':
state = api.get("api/v1/print_job/state").json()
if state == 'wait_cleanup':
return False
else:
return True
else:
return False
except exceptions.ConnectionError as err:
status = None
print_error(err)
def progress():
p = None
# If the printer gets disconnected, retry indefinitely
while p == None:
try:
p = api.get("api/v1/print_job/progress").json() * 100
return "%05.2f %%" % (p)
except exceptions.ConnectionError as err:
print_error(err)
def print_error(err):
print("Connection error: {0}".format(err))
print("Retrying")
print()
sleep(1)
def location_check(json_object, variant):
# possible locations include x,y: 213,207; 213,189;
x = json_object["x"]
y = json_object["y"]
if variant == "Ultimaker 3":
if math.fabs(x-213) <= 1: #if the absolute value of x minus a number is less than or equal to 2
if math.fabs(y-189) <= 2 : #or math.fabs(y-207) <= 1
return True
elif variant == "Ultimaker S5":
if x == 330:
if y == 219:
return True
def get_variant():
variant = api.get("api/v1/system/variant").json()
return variant
tmpdir = mkdtemp()
filenameformat = os.path.join(tmpdir, "%05d.jpg")
print(":: Saving images to",tmpdir)
if not os.path.exists(tmpdir):
os.makedirs(tmpdir)
print(":: Getting machine type")
variant = get_variant()
print(":: Found", variant)
print(":: Waiting for print to start")
while not printing():
sleep(1)
print(":: Printing")
count = 0
while printing():
count += 1
response = urlopen(imgurl)
filename = filenameformat % count
f = open(filename,'bw')
f.write(response.read())
f.close
print("Print progress: %s Image: %05i" % (progress(), count), end='\r')
# sleep(options.DELAY)
#sleep while printing a layer, wait for extruder position change
while not location_check(api.get("api/v1/printer/heads/0/position").json(), variant) and printing(): #location check should detect when printcore 2 is moved up or down
sleep(0.5)
sleep(5) # I think this is necessary because of the way the printer cools down and reheats the print cores between switching. To prevent taking multiple pictures of the same layer.
#caputre a few frames of postroll
print()
print(":: Printing Completed or Cancelled") #maybe I should write some code to detect when a print was cancelled
post_frames = 30 * options.POST_SEC
for x in range(0, int(post_frames)):
count += 1
response = urlopen(imgurl)
filename = filenameformat % count
f = open(filename,'bw')
f.write(response.read())
f.close
print("Post-Print Capture progress: %05i Image: %05i" % (x, count), end='\r')
sleep(0.1)
print()
print(":: Encoding video")
ffmpegcmd = "ffmpeg -r 30 -i " + filenameformat + " -vcodec libx264 -preset veryslow -crf 18 -loglevel panic " + options.OUTFILE
print(ffmpegcmd)
os.system(ffmpegcmd)
print(":: Done!")