-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstep-05-image-to-video.ts
297 lines (294 loc) · 8.66 KB
/
step-05-image-to-video.ts
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
import p from "node:process";
import puppeteer, { Locator, Page } from "puppeteer";
import { sleep } from "./util";
import { CacheOrComputer, WriteUtil } from "./util/api-cache";
type P = {
RUNWAY_USERNAME: string;
RUNWAY_PASSWORD: string;
RUNWAY_COOKIE?: string;
RUNWAY_TOKEN?: string;
prompt: string;
imageFilePath: string;
};
type Result = "refused-content-error" | "ok";
async function processA(props: P, util: WriteUtil): Promise<Result> {
const browser = await puppeteer.launch({
headless: true,
protocolTimeout: 30 * 60 * 1000,
});
const page = await browser.newPage();
try {
return await processB(props, util, page);
} finally {
await browser.close();
}
}
/**
* since they don't have an official API yet, this is a fairly fragile browser-based script
*/
async function processB(
props: P,
util: WriteUtil,
page: Page
): Promise<Result> {
const timeout = 50000;
page.setDefaultTimeout(timeout);
if (props.RUNWAY_TOKEN) {
await page.evaluateOnNewDocument((token) => {
localStorage.setItem("RW_USER_TOKEN", token);
}, props.RUNWAY_TOKEN);
}
if (props.RUNWAY_COOKIE) {
const cookies = JSON.parse(props.RUNWAY_COOKIE);
await page.setCookie(...cookies);
}
{
const targetPage = page;
await targetPage.setViewport({
width: 1499,
height: 1383,
});
}
{
const targetPage = page;
const promises: Promise<unknown>[] = [];
const startWaitingForEvents = () => {
promises.push(targetPage.waitForNavigation());
};
startWaitingForEvents();
await targetPage.goto("https://app.runwayml.com/login");
await Promise.all(promises);
}
if (page.url() === "https://app.runwayml.com/login") {
{
const targetPage = page;
await Locator.race([
targetPage.locator("::-p-aria(Username or Email)"),
targetPage.locator("input[type='text']"),
targetPage.locator(
'::-p-xpath(//*[@id=\\"root\\"]/div/div[4]/div[1]/div/div[2]/div/div[2]/div/form/div[1]/input[1])'
),
targetPage.locator(":scope >>> input[type='text']"),
])
.setTimeout(timeout)
.click({
offset: {
x: 98.25,
y: 9.609375,
},
});
}
{
const targetPage = page;
await Locator.race([
targetPage.locator("::-p-aria(Username or Email)"),
targetPage.locator("input[type='text']"),
targetPage.locator(
'::-p-xpath(//*[@id=\\"root\\"]/div/div[4]/div[1]/div/div[2]/div/div[2]/div/form/div[1]/input[1])'
),
targetPage.locator(":scope >>> input[type='text']"),
])
.setTimeout(timeout)
.fill(props.RUNWAY_USERNAME);
}
{
const targetPage = page;
await Locator.race([
targetPage.locator("::-p-aria(Password)"),
targetPage.locator("input[type='password']"),
targetPage.locator(
'::-p-xpath(//*[@id=\\"root\\"]/div/div[4]/div[1]/div/div[2]/div/div[2]/div/form/div[1]/input[2])'
),
targetPage.locator(":scope >>> input[type='password']"),
])
.setTimeout(timeout)
.click({
offset: {
x: 159.25,
y: 20.609375,
},
});
}
{
const targetPage = page;
await Locator.race([
targetPage.locator("::-p-aria(Password)"),
targetPage.locator("input[type='password']"),
targetPage.locator(
'::-p-xpath(//*[@id=\\"root\\"]/div/div[4]/div[1]/div/div[2]/div/div[2]/div/form/div[1]/input[2])'
),
targetPage.locator(":scope >>> input[type='password']"),
])
.setTimeout(timeout)
.fill(props.RUNWAY_PASSWORD);
}
await sleep(2000);
{
const targetPage = page;
await Locator.race([
targetPage.locator("::-p-aria(Log in)"),
targetPage.locator("form > button"),
targetPage.locator(
'::-p-xpath(//*[@id=\\"root\\"]/div/div[4]/div[1]/div/div[2]/div/div[2]/div/form/button)'
),
targetPage.locator(":scope >>> form > button"),
])
.setTimeout(timeout)
.click({
offset: {
x: 179.25,
y: 34.609375,
},
});
}
await page.waitForNavigation();
console.log(
"puppeteer token",
await page.evaluate(() => localStorage.getItem("RW_USER_TOKEN"))
);
}
{
const targetPage = page;
const promises: Promise<unknown>[] = [];
const startWaitingForEvents = () => {
promises.push(targetPage.waitForNavigation());
};
startWaitingForEvents();
await targetPage.goto(
`https://app.runwayml.com/video-tools/teams/${props.RUNWAY_USERNAME}/ai-tools/generative-video`
);
await Promise.all(promises);
}
/*{
const targetPage = page;
await Locator.race([
targetPage.locator('::-p-aria(Drop an image or click to upload Select from Assets) >>>> ::-p-aria([role=\\"paragraph\\"])'),
targetPage.locator('#data-panel-id-left-panel-panel-top p'),
targetPage.locator('::-p-xpath(//*[@id=\\"data-panel-id-left-panel-panel-top\\"]/div/div[2]/div/div/p)'),
targetPage.locator(':scope >>> #data-panel-id-left-panel-panel-top p'),
targetPage.locator('::-p-text(Drop an image)')
])
.setTimeout(timeout)
.click({
offset: {
x: 163.21875,
y: 4.078125,
},
});
}*/
await sleep(2000);
const fileInput = await page.waitForSelector("input[type=file]", { timeout });
if (!fileInput) throw Error("no file input");
const file = p.cwd() + "/" + props.imageFilePath;
console.log("uploading", file);
await fileInput.uploadFile(file);
await sleep(10000);
{
const targetPage = page;
await Locator.race([
targetPage.locator("::-p-aria(Text Prompt Input)"),
targetPage.locator("div.TextInput-module__textbox__X8YXn"),
targetPage.locator(
'::-p-xpath(//*[@id=\\"data-panel-id-left-panel-panel-bottom\\"]/div/div/div/div/div[1])'
),
targetPage.locator(":scope >>> div.TextInput-module__textbox__X8YXn"),
])
.setTimeout(timeout)
.fill(props.prompt);
/*.click({
offset: {
x: 123,
y: 41,
},
});*/
}
{
const targetPage = page;
await Locator.race([
targetPage.locator("button:not([disabled])::-p-text(Generate)"),
])
.setTimeout(30 * 60 * 1000)
.click({
offset: {
x: 44.1875,
y: 11,
},
});
}
console.log("pushed generate, waiting for video");
let vid;
// Content error
while (true) {
try {
vid = await page.waitForSelector("video source", {
timeout: 60 * 1000,
});
if (!vid) throw Error("video not found");
break;
} catch (e) {
const ele = await page.$("[class*=ProgressBar__percentage]");
if (ele) {
const text = await ele.evaluate((el) => el.textContent);
console.log("video not ready(?)", "status", text);
continue;
}
const res = await page.$("::-p-aria(Content error)");
if (res) {
console.error("content error, they refused to generate");
return "refused-content-error";
}
console.error(
"no readyness element found, probably need to press gen button (again)",
props.prompt
);
const targetPage = page;
await Locator.race([
targetPage.locator("button:not([disabled])::-p-text(Generate)"),
])
.setTimeout(30 * 60 * 1000)
.click({
offset: {
x: 44.1875,
y: 11,
},
});
}
}
console.log("video done!");
const src = await vid.evaluate((el: HTMLSourceElement) => el.src);
const ab = await fetch(src).then((res) => res.arrayBuffer());
await util.writeCompanion("-vid.mp4", new Uint8Array(ab));
return "ok";
}
export async function step05ImageToVideo(
apiFromCacheOr: CacheOrComputer,
config: {
RUNWAY_USERNAME?: string;
RUNWAY_PASSWORD?: string;
},
prompt: string,
imageFilePath: string,
preSleep: () => Promise<void>
) {
if (!config.RUNWAY_PASSWORD || !config.RUNWAY_USERNAME)
throw Error("no RUNWAY_PASSWORD or RUNWAY_USERNAME");
const input = {
prompt,
imageFilePath,
};
const full = {
RUNWAY_USERNAME: config.RUNWAY_USERNAME,
RUNWAY_PASSWORD: config.RUNWAY_PASSWORD,
...input,
};
const result = await apiFromCacheOr(
`https://app.runwayml.com/...`,
input,
async (util) => {
await preSleep();
const result = await processA(full, util);
return { result };
}
);
return { ...result, videoFilePath: result.meta.cachePrefix + "-vid.mp4" };
}