-
Notifications
You must be signed in to change notification settings - Fork 0
/
mechanical_duck.js
319 lines (276 loc) · 9.38 KB
/
mechanical_duck.js
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import {EdenClient} from "eden-sdk";
import dotenv from 'dotenv';
import https from 'https';
import fs from 'fs';
import {parseLines} from './utils.js';
import { exec } from 'child_process';
dotenv.config()
const eden = new EdenClient();
eden.loginApi(process.env.EDEN_API_KEY, process.env.EDEN_API_SECRET);
function downloadFile(url, filePath) {
return new Promise((resolve, reject) => {
const fileStream = fs.createWriteStream(filePath);
https.get(url, (response) => {
response.pipe(fileStream);
fileStream.on('finish', () => {
fileStream.close();
resolve();
});
}).on('error', (err) => {
fs.unlinkSync(filePath);
reject(err);
});
});
}
async function generateManyDucks() {
const promptprompt = `Give me a bunch of text prompts about a mechanical duck. acceptable variations revolve around a mechanical, robotic, steampunk, digital, cyber, or other such form factors, but always a duck. The prompts should describe an aesthetically novel duck with a set of complementary adjectives aligning with futuristic and technological aesthetics such as steel, silver, chrome, carbon wafer, and genres like cyberpunk, hacker and blacksmith chic.
Some examples:
A mechanical duck made out of silver chrome, acrylic, neon borders, sci-fi illustration, 3D render, detailed metal textures and shiny lights.
A robotic duck made out watch parts, against a steampunk cityscape, futuristic painting, smokey textures, anime style.
Machine duck in an alternate reality featuring a hybrid fractal landscape, smooth gradients and vivid highlights.
Give me 10 more.`
async function doDuck(text) {
console.log(text);
const config = {
text_input: text,
width: 960,
height: 640,
seed: Math.floor(Math.random() * 1000000),
}
const result = await eden.create("create", config);
return result;
}
let new_prompts = await eden.create("complete", {
prompt: promptprompt,
temperature: 0.9,
max_tokens: 500,
top_p: 1,
frequency_penalty: 0.15,
presence_penalty: 0.1
});
console.log(new_prompts);
let ducks = parseLines(new_prompts.task.output.result);
const promises = ducks.map(async (text) => {
const result = await doDuck(text);
const {task, uri} = result;
const txtName = `ducks/${text.substring(0, 150)}.json`;
const imgName = `ducks/${text.substring(0, 150)}.png`;
const imgFile = fs.createWriteStream(imgName);
https.get(uri, function(response) {
response.pipe(imgFile);
imgFile.on('finish', function() {
imgFile.close();
console.log('File downloaded and saved to disk!');
});
});
// save config json to json file
const file = fs.createWriteStream(txtName);
file.write(JSON.stringify(task.config));
file.end();
return imgName;
});
const all_ducks = await Promise.all(promises);
console.log(all_ducks)
}
// get all png files in /ducks
const dir = './ducks';
const files = fs.readdirSync(dir);
const pngs = files.filter((file) => file.endsWith('.png'));
// randomize order and take first 10
const shuffled = pngs.sort(() => 0.5 - Math.random());
const selected = shuffled.slice(0, 100);
console.log(selected);
// get all adjacent pairs, i.e. 0 and 1, 1 and 2, 2 and 3, looping back to 0 with N-1 and 0
const pairs = [];
for (let i = 0; i < selected.length; i++) {
const j = (i + 1) % selected.length;
// pairs.push([i, selected[i], selected[j]]);
}
console.log(pairs);
// for each pair, get corresponding json file (replace png with json), then load the json file
// and use the config to generate a new image
const promises = pairs.map(async (pair) => {
const json1 = pair[1].replace('.png', '.json');
const json2 = pair[2].replace('.png', '.json');
// load the jsons
const [config1, config2] = await Promise.all([
new Promise((resolve, reject) => {
fs.readFile(`${dir}/${json1}`, 'utf-8', (err, data) => {
if (err) reject(err);
else resolve(JSON.parse(data));
});
}),
new Promise((resolve, reject) => {
fs.readFile(`${dir}/${json2}`, 'utf-8', (err, data) => {
if (err) reject(err);
else resolve(JSON.parse(data));
});
}),
]);
const config = {
text_input: config1.text_input + " to " + config2.text_input,
width: 960,
height: 640,
interpolation_seeds: [config1.seed, config2.seed],
interpolation_texts: [config1.text_input, config2.text_input],
steps: 80,
n_frames: 200,
n_film: 1,
smooth: true,
loop: false,
fps: 25,
}
const result = await eden.startTask("interpolate", config);
console.log(result);
return result.taskId;
});
//const all_tasks = await Promise.all(promises);
//console.log(all_tasks);
const all_tasks = [
'xsqvpg4lxvav3csik6ym4i2rbm',
'xbploz2pwfbdthiu73ma7wu32q',
'dm6xofp7gneebiv54julybg4i4',
'eruqgi5sqbcmvdzqgknmecptou',
'f7egc2msinasbjv63h522kdws4',
'jqi2pxpgbrhvhozgupy4wkykdq',
'2s63byijbzem7avnrflowwtify',
'u5k5u2wt3bc2hb6pb7cguiabjm',
'pdx73yjykvga5cq6ji3coz3u4m',
'ikizvwj65nhzpkpmocfxtalmfq',
'3qf2ywk6zfdilhygxowi5wrq4u',
'6m2qwb6g3bcxnhfg743wz2vroe',
'ij433gpn45etbiquqag62d76ra',
'ub3arnrs75dqxpo2ijikosid3y',
'mzxjpxcdpvastohjoo6rs644im',
'n2e6ocwlabddlirwmatglzt2qu',
'q7f537z33fcjjpkpcca55c5psu',
'piucf473kbd7nlblr3li5ctxmm',
'nwwm7j2p4fhevmd2aanugwgp6y',
'6b6mzxaqbbglfjkkgvj62u4gbm',
'swrk66tvbzb7tck742zvz2vgfy',
'ktllr3222nezhfg3fhzwxugvt4',
// 'fmpqv3nkvbhunfhestise5b7ra',
'6f7fseecsrdzfosifbkiwi5w7e',
'xkmbobmsmrfblp24ouipjsn6u4',
'lsqivka5cbc7hkrxt5vpbyrqpi',
'dxnfexve45annaly3tl3rmxe2y',
'cfhdfinntre43fyhyrbhwegw6y',
'63wxplkj5vccpi5ti4wzx2enxy',
// '3od4zlzpwrdmpkhz43bpaei64q',
'luucnsg4zncfjitt575rhxtcx4',
'4vohcimrnzh3je3kkwcjajj4h4',
'5bdiegy6lfh2di5qpple6ouxrq',
'v3vizarhfvcbnmv3jcmtjzx5xy',
'ybrct2ey45ha5g3ubv62tijpha',
'4umskckifrf53l7untnouvlfni',
'v45epvwklffnzjbpi3ay5emqge',
'iko45rvlmfcwzme5rtuauw54o4',
'2tmxkc3eafayvfrjocdu6yqrty',
'qqm3uwxqkvdotfho3zdxmlkmzy',
'dvafwmsgw5bzdh6rm2nqw32ena',
'43egzlwxkrewndi26wig4wn7pm',
'm3xrrpwza5anxczay3cszzq7sy',
'5bkpe54sdrcejkpn6dz4ce2nsq',
'7in75shr6bhkxmwd2moudv65qu',
'hy4idyzgsjf6boxqzx7lsqeb6i',
'6ucdrpbkbrcqffrv4cd3itoujm',
'mr5alfnmbbcgtjrdot53n24qce',
'czxox54g4fcdbdehd6j5rr2z2e',
'derpiyjtwjbznko6rj4elkmsk4',
'xotqohoidzardci6k23vhkimy4',
'56fps6nyi5bhdl4awgze6gkuai',
'6va5hdslo5f4dd2fdtqrcgoocm',
'feuisacgbfczpa4pxx75rdfjyq',
'yopl3vvghvac3bc2qpa25yvp24',
'jcbeqj3h3zfeljt76ag6uqgybu',
'itaomgrjkzhc7kq7w3eodyzkwm',
'74s6tzmfvzg3tapo3yrlxszwxq',
'ugigwxluubgb5kus576ownnjfe',
'a7wf2tbpvjaezaq76hxtbfrlri',
've4a2wdecfdixkb7vczzft5pp4',
'qymg4he6sjeoncqpwwna3bnc7q',
'tvfre434inhy7heg6a2cxynhrm',
'pp2krkiqffdb5gw2f3j2ch6qi4',
'enhegvgdvfd4rpnn6d4g7mahai',
'5j72bpgjqnh4pggxcfs467txv4',
'57sv4jl6crf7jpzr5mmaskwlmm',
'2ssrz572rzcndncrs27on47ase',
'icewqqtsdrgkld75jrwon264eq',
'sixzwnw4lbehndr5czyagedkte',
'bsbxryn7hzbkxasqnh726qhrti',
'ie4zargejngrxp42ypqdosd5c4',
'vrwsxjqlffhqziicvh5yrmfrva',
'lf7xj44qkrd3lhjpv5a3wspwd4',
'zn64ejkrrfa2jjjtzr2biogpdy',
'3s2tz3hnjreblnfjmbnl356kcu',
'su4u23tikrepdhqwt5przxwlia',
'xf4kxci4lfbnpmsnsrhfaoy52q',
'3lt4jiyqnvabfo776heilgibbi',
'tqwrc3k7wvaydhdnhw6cddbsc4',
'ndmej7fshbbf7e2lrroynorlye',
'vp5swpqt6rajnafrt4yztjvl7m',
'mejw7f57ozh5xbanyv4k57iwb4',
'i2xdzahrpfelngha6vmfxfn3xi',
'lmvmhewmpjgzvb6hfjrjosz7y4',
'z3imdez5fbduznivxbwipf27le',
'5urlyfx2obflri2cmey3igg3ua',
'l5ja3wzxkbezjpsii3rqfvyyg4',
// '3xlultkwnfft3co3xtel6z4xku',
'4uudcqwcjbbq7hk2pagutcmh7m',
'gfcmy2luwrakjm5dv3qfj3dz6m',
'thhd6dwt45b5rpz27le4tn43m4',
'cndnuhpaindyhg4xpw7ksbvfii',
'bjhjbtq4hrdntlrbzzx6jd3obq',
'e5toczqmwvhbvbdtokyw5swoja',
'drtremmrl5c55bi7foypsnnib4',
'iixpoqbrrvfyddrrnrybudaq3e',
'sqxvxsy23fagxivlc6fxgdibfe',
'tnavuoj25ff27cuyd4y257bsjm',
'fyg3pjh7bbectohsng6kd36xfe'
]
// const all_tasks = [
// 'ukhc5fc2mjcmfcjsrqcwoe7cua',
// 'narlmllrqzcvfaa5k2vrleo6da',
// 'vy52ux7vurhrld7sllqxkt7gii'
// ]
// sleep for 20 minutes
console.log('sleeping');
//await new Promise(resolve => setTimeout(resolve, 10 * 1000));
console.log('done sleeping');
const promises2 = all_tasks.map(async (taskId) => {
const result = await eden.getTaskStatus(taskId);
const vidName = `videos/${taskId}.mp4`;
// check if already downloaded
const isDownloaded = fs.existsSync(vidName);
if (isDownloaded) {
console.log('already downloaded');
return vidName;
}
console.log(vidName);
console.log(result);
if (result.task.creation) {
const creation = await eden.getCreation(result.task.creation);
console.log(creation);
downloadFile(creation.uri, vidName)
}
return vidName;
});
const all_videos = await Promise.all(promises2);
console.log(all_videos);
// await downloadFile(result2.uri, vidName);
console.log("making final output video")
// // Create the text file
const inputFilePath = 'files.txt';
const inputFileContent = all_videos.map(filename => `file '${filename}'`).join('\n');
fs.writeFileSync(inputFilePath, inputFileContent);
await new Promise(resolve => setTimeout(resolve, 5 * 1000));
// Run the FFmpeg command
const outputFilePath = 'output3.mp4';
const command = `ffmpeg -f concat -safe 0 -i ${inputFilePath} -c copy ${outputFilePath}`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`FFmpeg error: ${error}`);
return;
}
console.log(`FFmpeg output: ${stdout}`);
});