-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgif_link.pde
84 lines (72 loc) · 2.54 KB
/
gif_link.pde
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
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.nio.file.Paths;
class GifLink {
private int length;
private int max_length;
private Process process;
private String out_filename;
private String first_filename;
private String last_filename;
private String dir;
private static final int fps = 30;
private final String engiffenCmd = Paths.get(System.getProperty("user.home"), ".cargo", "bin", "engiffen").toString();
// Is file separator == "\" a reliable way to test for Windows? Probably not!
private final String deleteCmd = System.getProperty("file.separator") == "\\" ? "del" : "rm -r";
private final boolean engiffenInstalled = isEngiffenInstalled();
GifLink(int seconds) {
DateFormat dateformat = new SimpleDateFormat("YYYY-MM-dd-HH-mm-ss");
this.dir = dateformat.format(new Date());
this.max_length = seconds * fps;
this.length = 0;
println("Recording frames!");
}
void feed(PGraphics canvas) {
// Only limit length if we're actually turning something into a gif
// Otherwise dump frames infinitely like usual
if (!this.engiffenInstalled || (this.length <= this.max_length)) {
String filename = String.format("glitch_%06d.bmp", frameCount);
if (this.first_filename == null) {
this.first_filename = filename;
}
this.last_filename = filename;
String out_frame = new File(this.dir, filename).toString();
canvas.save(out_frame);
}
if (this.engiffenInstalled && this.process == null && this.length >= this.max_length) {
this.end();
}
this.length += 1;
}
void end() {
clearGifLink();
println("Stopped recording.");
if (this.process != null) {
return;
}
if (this.engiffenInstalled) {
ProcessBuilder pb = new ProcessBuilder("bash", "-c", String.format(
"%s -o %s -f %d -s 2 -r %s %s && %s %s",
this.engiffenCmd,
String.format("%s.gif", sketchPath(this.dir)),
this.fps,
new File(sketchPath(this.dir), this.first_filename).toString(),
new File(sketchPath(this.dir), this.last_filename).toString(),
this.deleteCmd,
sketchPath(this.dir)
));
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
try {
this.process = pb.start();
} catch (IOException e) {
println("uh oh ", e);
}
}
}
private boolean isEngiffenInstalled() {
File f = new File(this.engiffenCmd);
return f.exists() && !f.isDirectory();
}
}