-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-sora2-video.ts
More file actions
348 lines (298 loc) · 10.2 KB
/
create-sora2-video.ts
File metadata and controls
348 lines (298 loc) · 10.2 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env bun
/**
* create-sora2-video.ts
* Generate a Sora 2 video via OpenAI v1/videos API using Bun.
*
* Usage examples:
* bun run create-sora2-video.ts --prompt "A golden retriever surfing at sunset" --orientation landscape --duration 8
* bun run create-sora2-video.ts --prompt "Raindrops on window, moody score" --orientation portrait --duration 4 --image ./guide.jpg
*
* Env:
* OPENAI_API_KEY=sk-...
*/
import path from "node:path";
import fs from "node:fs/promises";
interface Args {
prompt?: string;
duration?: number;
orientation?: "portrait" | "landscape";
imagePath?: string;
outdir?: string;
seed?: number;
model?: string;
help?: boolean;
}
interface VideoJob {
id?: string;
video_id?: string;
status?: string;
url?: string;
video_url?: string;
video?: {
url?: string;
download_url?: string;
};
download_url?: string;
output?: Array<{ url?: string; type?: string; kind?: string }>;
outputs?: Array<{ url?: string; type?: string; kind?: string }>;
assets?: {
video?: { url?: string };
audio?: { url?: string };
};
error?: { message?: string };
progress?: number;
message?: string;
[key: string]: any; // Allow for unknown fields
}
const API_KEY = process.env.OPENAI_API_KEY;
if (!API_KEY) {
console.error("❌ Missing OPENAI_API_KEY in environment.");
process.exit(1);
}
function parseArgs(argv: string[]): Args {
const args: Args = {};
for (let i = 2; i < argv.length; i++) {
const a = argv[i];
const hasNext = i + 1 < argv.length && !!argv[i + 1] && !argv[i + 1].startsWith("--");
const next = () => hasNext ? argv[++i] : "";
switch (a) {
case "--prompt":
args.prompt = next();
break;
case "--duration":
args.duration = Number(next());
break;
case "--orientation":
args.orientation = next() as Args["orientation"];
break;
case "--image":
args.imagePath = next();
break;
case "--outdir":
args.outdir = next();
break;
case "--seed":
args.seed = Number(next());
break;
case "--model":
args.model = next();
break;
case "--help":
args.help = true;
break;
}
}
return args;
}
const args = parseArgs(Bun.argv);
if (args.help || !args.prompt) {
console.log(`
Sora 2 Video Generator (OpenAI v1/videos API)
Required:
--prompt Text prompt describing the scene
Optional:
--duration Seconds: 4, 8, or 12 (default: 8)
--orientation portrait | landscape (default: landscape)
--image Optional guide image path
--model sora-2 (default) or sora-2-pro
--outdir Output folder (default: ./out)
--seed Optional random seed
Examples:
bun run create-sora2-video.ts --prompt "Futuristic city timelapse, neon reflections" --orientation landscape --duration 12
bun run create-sora2-video.ts --prompt "Watercolor koi fish in a pond" --orientation portrait --duration 8 --image ./koi.jpg
`);
process.exit(0);
}
const outdir = args.outdir ? path.resolve(args.outdir) : path.resolve(process.cwd(), "out");
await fs.mkdir(outdir, { recursive: true });
// Map user-friendly values to API parameters
const orientation = (args.orientation || "landscape").toLowerCase() as "portrait" | "landscape";
const size = orientation === "portrait" ? "720x1280" : "1280x720";
// Duration must be "4", "8", or "12" as strings for the API
const requestedDuration = args.duration || 8;
const validDurations = [4, 8, 12];
const duration = validDurations.includes(requestedDuration) ? requestedDuration : 8;
const seconds = duration.toString(); // API expects string
const model = args.model || "sora-2";
function logStep(msg: string) {
console.log(`[${new Date().toISOString()}] ${msg}`);
}
async function maybeEncodeImage(imagePath?: string): Promise<string | null> {
if (!imagePath) return null;
const file = Bun.file(imagePath);
if (!(await file.exists())) {
throw new Error(`Image not found: ${imagePath}`);
}
const ext = imagePath.split(".").pop()?.toLowerCase() || "jpg";
const mime = ext === "png" ? "image/png" : ext === "webp" ? "image/webp" : "image/jpeg";
const buf = await file.arrayBuffer();
return `data:${mime};base64,${Buffer.from(buf).toString("base64")}`;
}
async function createJob(): Promise<VideoJob> {
const imageDataUrl = await maybeEncodeImage(args.imagePath);
// Build payload according to Sora 2 API spec
const payload: Record<string, unknown> = {
model, // "sora-2" or "sora-2-pro"
prompt: args.prompt,
size, // "1280x720" or "720x1280"
seconds, // "4", "8", or "12" as string
};
// Add optional image reference
if (imageDataUrl) {
payload.input_reference = imageDataUrl;
}
// Add seed if provided
if (args.seed !== undefined) {
payload.seed = args.seed;
}
const res = await fetch("https://api.openai.com/v1/videos", {
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (!res.ok) {
const errorText = await res.text();
throw new Error(`Failed to create job (${res.status}): ${errorText}`);
}
const result = await res.json();
return result as VideoJob;
}
async function getJob(jobId: string): Promise<VideoJob> {
const res = await fetch(`https://api.openai.com/v1/videos/${jobId}`, {
headers: { Authorization: `Bearer ${API_KEY}` },
});
if (!res.ok) {
const errorText = await res.text();
throw new Error(`Failed to fetch job (${res.status}): ${errorText}`);
}
const result = await res.json();
return result as VideoJob;
}
async function getVideoContent(jobId: string): Promise<any> {
// Try to get the video content/download link
const res = await fetch(`https://api.openai.com/v1/videos/${jobId}/content`, {
headers: { Authorization: `Bearer ${API_KEY}` },
});
if (!res.ok) {
// If content endpoint doesn't exist, return null
if (res.status === 404) {
return null;
}
const errorText = await res.text();
throw new Error(`Failed to fetch video content (${res.status}): ${errorText}`);
}
// The content endpoint might return the video directly or a JSON with URL
const contentType = res.headers.get("content-type");
if (contentType && contentType.includes("video")) {
// Direct video stream - save the response
return res;
}
// JSON response with URL
return res.json();
}
async function downloadFile(url: string, dest: string) {
const res = await fetch(url, {
headers: { Authorization: `Bearer ${API_KEY}` }, // May be required for some URLs
});
if (!res.ok) {
throw new Error(`Download failed (${res.status}): ${await res.text()}`);
}
await Bun.write(dest, res);
}
function randSuffix(): string {
return Math.random().toString(36).slice(2, 8);
}
async function saveAssets(state: VideoJob, jobId: string) {
const id = state.id || state.video_id || jobId || randSuffix();
const baseName = `sora2_${id}`;
// Try to get video content from the content endpoint
logStep("Fetching video content...");
const content = await getVideoContent(jobId);
const videoPath = path.join(outdir, `${baseName}.mp4`);
if (content) {
if (content instanceof Response) {
// Direct video stream
logStep(`Saving video → ${videoPath}`);
await Bun.write(videoPath, content);
} else if (content.url || content.download_url) {
// JSON with URL
const videoUrl = content.url || content.download_url;
logStep(`Downloading video from URL → ${videoPath}`);
await downloadFile(videoUrl, videoPath);
} else {
console.error("Debug - Content response:", JSON.stringify(content, null, 2));
throw new Error("Unexpected content response format");
}
} else {
// Fallback: Try to extract URL from state (for backwards compatibility)
const videoUrl =
state.url ||
state.video_url ||
state.download_url ||
state.video?.url ||
state.video?.download_url ||
state.output?.[0]?.url ||
state.outputs?.[0]?.url ||
state.assets?.video?.url;
if (!videoUrl) {
console.error("Debug - Full response:", JSON.stringify(state, null, 2));
console.error("Debug - Available keys:", Object.keys(state));
throw new Error("No video URL found and content endpoint returned nothing");
}
logStep(`Downloading video → ${videoPath}`);
await downloadFile(videoUrl, videoPath);
}
logStep("✅ Video generation complete!");
}
(async () => {
try {
logStep(`Creating Sora 2 video (model=${model}, size=${size}, duration=${seconds}s)…`);
const created = await createJob();
const jobId = created.id || created.video_id;
if (!jobId) {
// Check if we got an immediate response with assets
if (created.url || created.video_url || created.output || created.assets) {
logStep("Immediate response received — saving video...");
await saveAssets(created, "immediate");
return;
}
throw new Error(`No job ID found in response: ${JSON.stringify(created)}`);
}
logStep(`Job ${jobId} created. Polling for completion...`);
let status = created.status || "queued";
let lastProgress = -1;
while (true) {
const state = await getJob(jobId);
status = state.status || status;
// Show progress if available
if (typeof state.progress === "number") {
const progress = Math.round(state.progress);
if (progress !== lastProgress) {
lastProgress = progress;
logStep(`Progress: ${progress}%`);
}
} else {
logStep(`Status: ${status}`);
}
// Check for completion
if (["completed", "succeeded", "complete"].includes(status)) {
logStep("Rendering complete. Downloading assets...");
await saveAssets(state, jobId);
break;
}
// Check for failure
if (["failed", "error", "cancelled", "canceled"].includes(status)) {
const reason = state.error?.message || state.message || "Unknown error";
throw new Error(`Job ${status}: ${reason}`);
}
// Wait before next poll
await Bun.sleep(5000);
}
} catch (err: any) {
console.error(`\n❌ ERROR: ${err.message}\n`);
process.exit(1);
}
})();