-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.py
executable file
·194 lines (136 loc) · 4.67 KB
/
make.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/python
from os import mkdir
from os import path
from os import remove as rm
from os import stat
from os import system as cmd
from sys import stdout
from glob import glob
from PIL import Image
if path.exists("assets"): quit()
if not path.exists("build"): mkdir("build")
if not path.exists("preproc/preproc"):
cmd("cd preproc && make -j$(nproc)")
if not path.exists("build/badapple.mp4"):
cmd("youtube-dl https://www.youtube.com/watch?v=FtutLA63Cp8 -o /tmp/badapple")
cmd("ffmpeg -i /tmp/badapple.mkv -vf 'fps=30,scale=212:160' build/badapple.mp4")
rm("/tmp/badapple.mkv")
if not path.exists("build/frame_6572.bmp"):
cmd("ffmpeg -i build/badapple.mp4 build/frame_\%04d.bmp")
if not path.exists("build/frame_6572.raw"):
globbed: list = glob("build/*.bmp")
stdout.write("Processing frames... (%4d/%4d) [%18s]" % (0, len(globbed), ""))
stdout.flush()
for i, file in enumerate(globbed):
with Image.open(file) as image:
empty: bool = True
try:
a, b, c, d = image.getbbox()
empty = False
except:
pass
x: int = 0
y: int = 0
duplicates: int = 0
last: int = None
if not empty:
buffer: bytearray = bytearray()
while y < 160:
while x < 211:
x += 1
rgb: tuple[int, int, int] = image.getpixel((x, y))
average: int = int(round((rgb[0] + rgb[1] + rgb[2]) / 3))
rounded: int = 0
if average > 128 + 64:
rounded = 2
elif average > 128:
rounded = 1
if rounded == last:
duplicates += 1
elif last != None:
buffer.append(duplicates)
buffer.append(last)
last = rounded
duplicates = 1
else:
last = rounded
duplicates = 1
if x >= 211:
buffer.append(duplicates)
buffer.append(last)
last = None
duplicates = 0
x = 0
y += 1
if duplicates != 0:
buffer.append(duplicates)
buffer.append(last)
f = open(file.replace(".bmp", ".raw"), "wb")
f.write(buffer)
f.close()
else:
buffer: bytearray = bytearray()
for _ in range(0, 160):
buffer.append(211)
buffer.append(0)
f = open(file.replace(".bmp", ".raw"), "wb")
f.write(buffer)
f.close()
stdout.write(\
"\rProcessing frames... ({:4d}/{:4d}) [{:#<{complete}}{:<{remaining}}]".format(\
i, len(globbed),\
"", "",\
complete = int((float(i) / float(len(globbed))) * 18.0),\
remaining = 18 - int((float(i) / float(len(globbed))) * 18.0)))
stdout.flush()
stdout.write("\n")
stdout.flush()
if (not path.exists("build/frames.c")) \
and (not path.exists("source/frames.c")):
buffer: str = """
#ifndef FRAMES
#define FRAMES
#include <stdint.h>
"""
names: list[str] = [ ]
for file in glob("build/*.raw"):
name = file\
.replace("build/", "")\
.replace(".raw", "")
buffer += "static const uint8_t _%s[%i] = INCBIN_U8(\"%s\");\n" % (name, stat(file).st_size, file)
names.append(name)
buffer += "\n"
names.sort()
buffer += "static const struct { int l; const uint8_t *d; } frames[%i] = {\n" % len(names)
for name in names:
buffer += " { %i, (const uint8_t *)&_%s },\n" % (stat("build/%s.raw" % name).st_size, name)
buffer += "};\n"
buffer += "\n"
buffer += "#endif // FRAMES\n"
buffer += "\n"
f = open("build/frames.c", "w")
f.write(buffer)
f.close()
if not path.exists("source/frames.c"):
cmd("preproc/preproc build/frames.c preproc/charmap.txt > source/frames.c")
rm("build/frames.c")
if not path.exists("build/badapple.wav"): cmd("ffmpeg -i build/badapple.mp4 build/badapple.wav")
if not path.exists("build/badapple.pcm"):
cmd("ffmpeg -i build/badapple.wav -f s8 -ac 1 -ar 22050 -acodec pcm_s8 build/badapple.pcm")
if (not path.exists("build/audio.c")) \
and (not path.exists("source/audio.c")):
buffer: str = """
#ifndef AUDIO
#define AUDIO
#include <stdint.h>
"""
buffer += \
"static const uint8_t audio[%i] = INCBIN_U8(\"%s\");\n" \
% (stat("build/badapple.pcm").st_size, "build/badapple.pcm")
buffer += "\n#endif // AUDIO\n\n"
f = open("build/audio.c", "w")
f.write(buffer)
f.close()
if not path.exists("source/audio.c"):
cmd("preproc/preproc build/audio.c preproc/charmap.txt > source/audio.c")
rm("build/audio.c")