-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnft.py
More file actions
222 lines (204 loc) · 10.6 KB
/
nft.py
File metadata and controls
222 lines (204 loc) · 10.6 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import os
import json
from PIL import Image
import matchering as mg
import base64
metadata = []
if os.path.isfile("metadata.json") == False:
print("no metadata.json file")
exit(0)
if os.path.isfile("config.json") == False:
print("no config.json file")
exit(0)
# Read metadata for generating NFTs
with open('metadata.json') as metadata_file:
metadata = json.load(metadata_file)
# Read visual_orders from config.json
with open('config.json') as config_file:
config_data = json.load(config_file)
visual_orders = config_data["visual_orders"]
base_uri = config_data["baseURI"]
# Get an asset location
def getAssetLocation(trait, value, extension, wall = True):
subpath = "Audio/"
if extension != "wav":
subpath = "Visual/" + str(visual_orders[trait]) + ". "
if wall == True:
return "'./Asset/" + subpath + trait + "/" + value + "." + extension + "'"
return "./Asset/" + subpath + trait + "/" + value + "." + extension
# Get an NFT result location
def getNFTLocation(title, extension, wall = True):
if wall == True:
return "'./NFT/" + title + "." + extension + "'"
return "./NFT/" + title + "." + extension
#
def generateNFTs():
for i in range(len(metadata)):
attributes = metadata[i]["attributes"]
index = metadata[i]["name"].split("#")[1]
# wav
if len(attributes["wav"]) != 0:
shell = "ffmpeg -y"
for attr in attributes["wav"]:
shell += " -i " + getAssetLocation(attr["trait_type"], attr["value"], "wav")
shell += " -filter_complex amix=inputs=" + str(len(attributes["wav"])) + ":duration=first:dropout_transition=0,alimiter=level_out=2 "+ getNFTLocation(index + "_master", "wav")
os.system(shell)
# matchering
mg.process(
target=getNFTLocation(index + "_master", "wav", False),
reference="reference.wav",
results=[mg.pcm24(getNFTLocation(index, "wav", False))],
)
# png
cut = 0
length = len(attributes["png"])
for cut in range(length):
if visual_orders[attributes["png"][cut]["trait_type"]] > 6:
break
png_1 = attributes["png"][0:cut]
png_2 = attributes["png"][cut:length]
#png_1
background = Image.open(getAssetLocation(png_1[0]["trait_type"], png_1[0]["value"], "png", False), 'r')
for attr in png_1:
img = Image.open(getAssetLocation(attr["trait_type"], attr["value"], "png", False), 'r')
background = Image.alpha_composite(background, img)
background.save(getNFTLocation(index + "_1", "png", False))
#png_2
background = Image.open(getAssetLocation(png_2[0]["trait_type"], png_2[0]["value"], "png", False), 'r')
for attr in png_2:
img = Image.open(getAssetLocation(attr["trait_type"], attr["value"], "png", False), 'r')
background = Image.alpha_composite(background, img)
background.save(getNFTLocation(index + "_2", "png", False))
#
if len(attributes["mp4"]) != 0:
filemp4 = attributes["mp4"][0]
# Overlay png_1 with mp4
os.system("ffmpeg -y -i "
+ getAssetLocation(filemp4["trait_type"], filemp4["value"], "mp4")
+ " -i "
+ getNFTLocation(index + "_1", "png")
+ " -filter_complex '[0][1]overlay[v]' -map '[v]' -c:v libx264 -preset ultrafast -qp 0 "
+ getNFTLocation(index + "_mp4_png", "mp4"))
if len(attributes["mov"]) != 0:
filemov = attributes["mov"][0]
os.system("ffmpeg -y -i "
+ getNFTLocation(index + "_mp4_png", "mp4")
+ " -i " + getAssetLocation(filemov["trait_type"], filemov["value"], "mov")
+ " -filter_complex '[1:v]setpts=PTS-STARTPTS+0/TB[ovr];[0:v][ovr]overlay=enable=gte(t\,0):eof_action=pass,format=yuv420p[vid]' -map '[vid]' -c:v libx264 -preset ultrafast -qp 0 "
+ getNFTLocation(index + "_mp4_mov", "mp4"))
# Overlay png_2 with mp4
os.system("ffmpeg -y -i "
+ getNFTLocation(index + "_mp4_mov", "mp4")
+ " -i "
+ getNFTLocation(index + "_2", "png")
+ " -filter_complex '[0][1]overlay[v]' -map '[v]' -c:v libx264 -preset ultrafast -qp 0 "
+ getNFTLocation(index + "_mp4_png_final", "mp4"))
# Overlay alpha with mp4
if len(attributes["mov"]) == 2:
filemov = attributes["mov"][1]
os.system("ffmpeg -y -i "
+ getNFTLocation(index + "_mp4_png_final", "mp4")
+ " -i " + getAssetLocation(filemov["trait_type"], filemov["value"], "mov")
+ " -filter_complex '[1:v]setpts=PTS-STARTPTS+0/TB[ovr];[0:v][ovr]overlay=enable=gte(t\,0):eof_action=pass,format=yuv420p[vid]' -map '[vid]' -c:v libx264 -preset ultrafast -qp 0 "
+ getNFTLocation(index + "_mp4_png_final_alpha", "mp4"))
# Merge mp4 with wav
os.system("ffmpeg -y -i "
+ getNFTLocation(index + "_mp4_png_final_alpha", "mp4")
+" -i "
+ getNFTLocation(index, "wav")
+ " -c:v libx264 -c:a aac -b:a 320k -movflags +faststart "
+ getNFTLocation(index + "_mp4_wav", "mp4"))
else:
# Merge mp4 with wav
os.system("ffmpeg -y -i "
+ getNFTLocation(index + "_mp4_png_final", "mp4")
+" -i "
+ getNFTLocation(index, "wav")
+ " -c:v libx264 -c:a aac -b:a 320k -movflags +faststart "
+ getNFTLocation(index + "_mp4_wav", "mp4"))
os.system("mv " + getNFTLocation(index + "_mp4_wav", "mp4") + " " + getNFTLocation(index + "_mp4", "mp4"))
os.system("mv " + getNFTLocation(index + "_mp4", "mp4") + " " + getNFTLocation(index, "mp4"))
# remove _mp4 & _mp4_mov & _mp4_wav
os.system("rm -f " + getNFTLocation(index + "_1", "png"))
os.system("rm -f " + getNFTLocation(index + "_2", "png"))
os.system("rm -f " + getNFTLocation(index + "_mp4_mov", "mp4"))
os.system("rm -f " + getNFTLocation(index + "_mp4_wav", "mp4"))
os.system("rm -f " + getNFTLocation(index + "_mp4_png", "mp4"))
os.system("rm -f " + getNFTLocation(index + "_mp4_png_final", "mp4"))
if len(attributes["mov"]) == 2:
os.system("rm -f " + getNFTLocation(index + "_mp4_png_final_alpha", "mp4"))
else:
# remove _mp4_mov & rename _mp4_wav
os.system("mv " + getNFTLocation(index + "_mp4_wav", "mp4") + " " + getNFTLocation(index, "mp4"))
os.system("rm -f " + getNFTLocation(index + "_mp4_mov", "mp4"))
else:
# merge png & wav
os.system("ffmpeg -y -loop 1 -i "
+ getNFTLocation(index, "png")
+ " -i "
+ getNFTLocation(index, "wav")
+ " -c:v libx264 -tune stillimage -c:a aac -b:a 320k -pix_fmt yuv420p -shortest "
+ getNFTLocation(index + "_png_wav", "mp4"))
# cut video by duration
os.system("ffmpeg -ss 00:00:00 -to 00:01:00 -i "
+ getNFTLocation(index + "_png_wav", "mp4")
+ " -c copy "
+ getNFTLocation(index, "mp4"))
os.system("rm -f " + getNFTLocation(index + "_png_wav", "mp4"))
# remove png & wav
os.system("rm -f " + getNFTLocation(index, "png"))
os.system("rm -f " + getNFTLocation(index, "wav"))
# Do not remove Master WAV, we may need it, instead lets move it to index.wav
os.system("mv " + getNFTLocation(index + "_master","wav") + " " + getNFTLocation(index, "wav"))
# os.system("rm -f " + getNFTLocation(index + "_master", "wav"))
# extract an image from video
os.system("ffmpeg -i " + getNFTLocation(index, "mp4") + " -ss 00:00:00 -vframes 1 " + getNFTLocation(index, "png"))
# generate metadata json
temp_json = metadata[i]
temp_json["image"] = base_uri + index + ".png"
temp_json["animation_url"] = base_uri + index + ".mp4"
temp_json["dna"] = ""
temp_png = []
tattoos_large= ""
tattoos_small= ""
alpha_animations= ""
for attr in temp_json["attributes"]["png"]:
if attr["trait_type"] == "Tattoos Large":
tattoos_large = attr["value"]
elif attr["trait_type"] == "Tattoos Small":
tattoos_small = attr["value"]
elif attr["trait_type"] in ["Under Nails", "Under Nails Normal", "Alpha Animations", "Over Stickers", "Under Hands", "Windows", "Details", "Tape Colors", "Nails", "Cassette Stickers", "Rings"]:
# includes
no = 0
else:
temp_png.append(attr)
temp_json["dna"] += attr["trait_type"] + ":" + attr["value"] + ","
temp_png.append({
"trait_type": "Tattoos",
"value": tattoos_large + " + " + tattoos_small
})
temp_mov = []
for attr in temp_json["attributes"]["mov"]:
if attr["trait_type"] == "Alpha Animations":
alpha_animations = attr["value"]
elif attr["trait_type"] != "Gears":
temp_mov.append(attr)
temp_json["dna"] += attr["trait_type"] + ":" + attr["value"] + ","
temp_mov.append({
"trait_type": "The Drip",
"value": alpha_animations
})
for attr in temp_json["attributes"]["wav"]:
temp_json["dna"] += attr["trait_type"] + ":" + attr["value"] + ","
for attr in temp_json["attributes"]["mp4"]:
temp_json["dna"] += attr["trait_type"] + ":" + attr["value"] + ","
temp_json["attributes"] = [
*temp_png,
*temp_json["attributes"]["wav"],
*temp_mov
]
temp_json["dna"] = (temp_json["dna"][0:(len(temp_json["dna"]) - 1)]).encode("ascii")
temp_json["dna"] = (base64.b64encode(temp_json["dna"])).decode("ascii")
with open(getNFTLocation(index, "json", False), 'w') as outfile:
json.dump(temp_json, outfile, indent=4)
generateNFTs()