-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-media.js
More file actions
494 lines (401 loc) · 16.8 KB
/
ai-media.js
File metadata and controls
494 lines (401 loc) · 16.8 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// AI Image & Video Generation Engine
import { writeFileSync, existsSync, mkdirSync } from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const MEDIA_DIR = path.join(__dirname, "output", "media");
if (!existsSync(MEDIA_DIR)) mkdirSync(MEDIA_DIR, { recursive: true });
// --- Image Prompt Builder ---
// Converts content topic into a visual prompt for AI image generators
export function buildImagePrompt(topic, style, context) {
const baseStyles = {
cinematic: "cinematic lighting, dramatic atmosphere, 8k, ultra detailed, professional photography",
minimal: "minimalist design, clean composition, modern aesthetic, solid color background",
vibrant: "vibrant colors, bold composition, eye-catching, high contrast, trending on artstation",
dark: "dark moody aesthetic, neon accents, cyberpunk inspired, dramatic shadows",
luxury: "luxury aesthetic, gold accents, premium feel, elegant composition, high-end",
african: "African patterns, warm earth tones, cultural motifs, vibrant ankara textiles, beautiful composition",
};
const topicVisuals = {
export: "global trade, shipping containers, world map, cargo ships, modern logistics",
"africa-trade": "African marketplace, vibrant market scene, African business, cultural commerce",
fashion: "fashion editorial, haute couture, stylish outfit flat lay, fashion photography",
culture: "African art, cultural heritage, traditional patterns, modern African aesthetic",
beauty: "beauty products flat lay, skincare aesthetic, luxury cosmetics, soft lighting",
business: "modern workspace, entrepreneur lifestyle, success aesthetic, office setup",
lifestyle: "lifestyle aesthetic, daily routine, wellness, modern living",
};
const visualContext = topicVisuals[topic] || context || "professional content creation";
const styleStr = baseStyles[style] || baseStyles.cinematic;
return `${visualContext}, ${styleStr}, no text, no watermark, no people faces, faceless content style`;
}
// --- DALL-E 3 (OpenAI) ---
export async function generateImageDalle(prompt, size = "1024x1792") {
const key = process.env.OPENAI_API_KEY;
if (!key) throw new Error("OPENAI_API_KEY not set in .env");
const resp = await fetch("https://api.openai.com/v1/images/generations", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${key}`,
},
body: JSON.stringify({
model: "dall-e-3",
prompt: prompt,
n: 1,
size, // 1024x1024, 1024x1792 (portrait), 1792x1024 (landscape)
quality: "hd",
style: "vivid",
}),
});
if (!resp.ok) {
const err = await resp.json();
throw new Error(`DALL-E: ${err.error?.message || resp.status}`);
}
const data = await resp.json();
const imageUrl = data.data[0]?.url;
const revisedPrompt = data.data[0]?.revised_prompt;
// Download and save
const imageResp = await fetch(imageUrl);
const buffer = Buffer.from(await imageResp.arrayBuffer());
const filename = `dalle-${Date.now()}.png`;
const filepath = path.join(MEDIA_DIR, filename);
writeFileSync(filepath, buffer);
return {
provider: "dall-e-3",
url: `/output/media/${filename}`,
filepath,
revisedPrompt,
};
}
// --- Stability AI (Stable Diffusion) ---
export async function generateImageStability(prompt, aspectRatio = "9:16") {
const key = process.env.STABILITY_API_KEY;
if (!key) throw new Error("STABILITY_API_KEY not set in .env");
const resp = await fetch("https://api.stability.ai/v2beta/stable-image/generate/sd3", {
method: "POST",
headers: {
Authorization: `Bearer ${key}`,
Accept: "image/*",
},
body: (() => {
const form = new FormData();
form.append("prompt", prompt);
form.append("aspect_ratio", aspectRatio);
form.append("output_format", "png");
form.append("model", "sd3.5-large");
return form;
})(),
});
if (!resp.ok) {
const err = await resp.text();
throw new Error(`Stability AI: ${err}`);
}
const buffer = Buffer.from(await resp.arrayBuffer());
const filename = `stability-${Date.now()}.png`;
const filepath = path.join(MEDIA_DIR, filename);
writeFileSync(filepath, buffer);
return {
provider: "stability-ai",
url: `/output/media/${filename}`,
filepath,
};
}
// --- FAL.ai (Fast image + video) ---
export async function generateImageFal(prompt, imageSize = "portrait_16_9") {
const key = process.env.FAL_KEY;
if (!key) throw new Error("FAL_KEY not set in .env");
// Submit request
const resp = await fetch("https://queue.fal.run/fal-ai/flux-pro/v1.1", {
method: "POST",
headers: {
Authorization: `Key ${key}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
prompt,
image_size: imageSize, // square_hd, portrait_4_3, portrait_16_9, landscape_4_3, landscape_16_9
num_images: 1,
safety_tolerance: "5",
}),
});
if (!resp.ok) {
const err = await resp.text();
throw new Error(`FAL.ai: ${err}`);
}
const data = await resp.json();
const imageUrl = data.images?.[0]?.url;
if (!imageUrl) throw new Error("FAL.ai: No image returned");
// Download and save
const imageResp = await fetch(imageUrl);
const buffer = Buffer.from(await imageResp.arrayBuffer());
const filename = `fal-${Date.now()}.png`;
const filepath = path.join(MEDIA_DIR, filename);
writeFileSync(filepath, buffer);
return {
provider: "fal-flux-pro",
url: `/output/media/${filename}`,
filepath,
};
}
// --- AI Video Generation (Replicate) ---
export async function generateVideoReplicate(prompt, imageUrl) {
const token = process.env.REPLICATE_API_TOKEN;
if (!token) throw new Error("REPLICATE_API_TOKEN not set in .env");
// Use minimax video-01 or similar model
const input = imageUrl
? { prompt, first_frame_image: imageUrl }
: { prompt };
const resp = await fetch("https://api.replicate.com/v1/predictions", {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
// minimax video generation model
version: "c8bbbda3eaa8db820b5b659beca98d7ae09aff2ef2c1e42586de0ece48b5a72f",
input: {
prompt: `${prompt}, smooth camera movement, professional quality, no faces`,
...input,
},
}),
});
if (!resp.ok) {
const err = await resp.json();
throw new Error(`Replicate: ${err.detail || resp.status}`);
}
const prediction = await resp.json();
// Poll for completion (max 60 attempts = ~3 min)
let result = prediction;
let attempts = 0;
const MAX_POLL_ATTEMPTS = 60;
while (result.status !== "succeeded" && result.status !== "failed") {
if (++attempts > MAX_POLL_ATTEMPTS) throw new Error("Replicate: Timeout — zu viele Polling-Versuche (max 3 Min)");
await new Promise(r => setTimeout(r, 3000));
const poll = await fetch(`https://api.replicate.com/v1/predictions/${result.id}`, {
headers: { Authorization: `Bearer ${token}` },
});
result = await poll.json();
}
if (result.status === "failed") throw new Error(`Replicate: ${result.error || "Generation failed"}`);
const videoUrl = result.output;
if (!videoUrl) throw new Error("Replicate: No video returned");
// Download video
const videoResp = await fetch(typeof videoUrl === "string" ? videoUrl : videoUrl[0]);
const buffer = Buffer.from(await videoResp.arrayBuffer());
const filename = `ai-video-${Date.now()}.mp4`;
const filepath = path.join(MEDIA_DIR, filename);
writeFileSync(filepath, buffer);
return {
provider: "replicate",
url: `/output/media/${filename}`,
filepath,
};
}
// --- FAL.ai Video (Kling, Minimax) ---
export async function generateVideoFal(prompt, imageUrl) {
const key = process.env.FAL_KEY;
if (!key) throw new Error("FAL_KEY not set in .env");
const input = { prompt, duration: "5" };
if (imageUrl) input.image_url = imageUrl;
// Submit to queue
const resp = await fetch("https://queue.fal.run/fal-ai/minimax/video-01-live", {
method: "POST",
headers: {
Authorization: `Key ${key}`,
"Content-Type": "application/json",
},
body: JSON.stringify(input),
});
if (!resp.ok) {
const err = await resp.text();
throw new Error(`FAL.ai Video: ${err}`);
}
const data = await resp.json();
const videoUrl = data.video?.url;
if (!videoUrl) throw new Error("FAL.ai: No video returned");
const videoResp = await fetch(videoUrl);
const buffer = Buffer.from(await videoResp.arrayBuffer());
const filename = `fal-video-${Date.now()}.mp4`;
const filepath = path.join(MEDIA_DIR, filename);
writeFileSync(filepath, buffer);
return {
provider: "fal-minimax",
url: `/output/media/${filename}`,
filepath,
};
}
// --- FAL.ai Kling (China AI - hochwertige Videos) ---
export async function generateVideoKling(prompt, imageUrl) {
const key = process.env.FAL_KEY;
if (!key) throw new Error("FAL_KEY not set in .env");
const input = { prompt, duration: "5", aspect_ratio: "9:16" };
if (imageUrl) input.image_url = imageUrl;
const resp = await fetch("https://queue.fal.run/fal-ai/kling-video/v1.5/pro/image-to-video", {
method: "POST",
headers: { Authorization: `Key ${key}`, "Content-Type": "application/json" },
body: JSON.stringify(input),
});
if (!resp.ok) throw new Error(`Kling: ${await resp.text()}`);
const data = await resp.json();
const videoUrl = data.video?.url;
if (!videoUrl) throw new Error("Kling: No video returned");
const videoResp = await fetch(videoUrl);
const buffer = Buffer.from(await videoResp.arrayBuffer());
const filename = `kling-${Date.now()}.mp4`;
const filepath = path.join(MEDIA_DIR, filename);
writeFileSync(filepath, buffer);
return { provider: "kling-v1.5", url: `/output/media/${filename}`, filepath };
}
// --- FAL.ai Hailuo / MiniMax (Nana-Banana Style) ---
export async function generateVideoHailuo(prompt, imageUrl) {
const key = process.env.FAL_KEY;
if (!key) throw new Error("FAL_KEY not set in .env");
const input = { prompt };
if (imageUrl) input.image_url = imageUrl;
const resp = await fetch("https://queue.fal.run/fal-ai/minimax/video-01", {
method: "POST",
headers: { Authorization: `Key ${key}`, "Content-Type": "application/json" },
body: JSON.stringify({ prompt: `${prompt}, smooth cinematic motion, professional`, ...input }),
});
if (!resp.ok) throw new Error(`Hailuo/MiniMax: ${await resp.text()}`);
const data = await resp.json();
const videoUrl = data.video?.url;
if (!videoUrl) throw new Error("Hailuo: No video returned");
const videoResp = await fetch(videoUrl);
const buffer = Buffer.from(await videoResp.arrayBuffer());
const filename = `hailuo-${Date.now()}.mp4`;
const filepath = path.join(MEDIA_DIR, filename);
writeFileSync(filepath, buffer);
return { provider: "hailuo-minimax", url: `/output/media/${filename}`, filepath };
}
// --- FAL.ai Luma Dream Machine ---
export async function generateVideoLuma(prompt, imageUrl) {
const key = process.env.FAL_KEY;
if (!key) throw new Error("FAL_KEY not set in .env");
const input = { prompt };
if (imageUrl) input.image_url = imageUrl;
const resp = await fetch("https://queue.fal.run/fal-ai/luma-dream-machine", {
method: "POST",
headers: { Authorization: `Key ${key}`, "Content-Type": "application/json" },
body: JSON.stringify(input),
});
if (!resp.ok) throw new Error(`Luma: ${await resp.text()}`);
const data = await resp.json();
const videoUrl = data.video?.url;
if (!videoUrl) throw new Error("Luma: No video returned");
const videoResp = await fetch(videoUrl);
const buffer = Buffer.from(await videoResp.arrayBuffer());
const filename = `luma-${Date.now()}.mp4`;
const filepath = path.join(MEDIA_DIR, filename);
writeFileSync(filepath, buffer);
return { provider: "luma-dream-machine", url: `/output/media/${filename}`, filepath };
}
// --- Replicate: Runway Gen-3 Alpha ---
export async function generateVideoRunway(prompt, imageUrl) {
const token = process.env.REPLICATE_API_TOKEN;
if (!token) throw new Error("REPLICATE_API_TOKEN not set in .env");
const input = { prompt: `${prompt}, cinematic, smooth motion, no faces, professional quality` };
if (imageUrl) input.image = imageUrl;
const resp = await fetch("https://api.replicate.com/v1/predictions", {
method: "POST",
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
body: JSON.stringify({
version: "2d2dc25b68a51c2dcc5b8fee7a87e0f89b8e5e83c2e9132ea32d4ab7f3ef6eb0",
input,
}),
});
if (!resp.ok) throw new Error(`Runway: ${(await resp.json()).detail || resp.status}`);
let result = await resp.json();
let attempts = 0;
const MAX_POLL_ATTEMPTS = 60; // max 60 * 5s = 5 min
while (result.status !== "succeeded" && result.status !== "failed") {
if (++attempts > MAX_POLL_ATTEMPTS) throw new Error("Runway: Timeout — zu viele Polling-Versuche (max 5 Min)");
await new Promise(r => setTimeout(r, 5000));
result = await (await fetch(`https://api.replicate.com/v1/predictions/${result.id}`, {
headers: { Authorization: `Bearer ${token}` },
})).json();
}
if (result.status === "failed") throw new Error(`Runway: ${result.error}`);
const videoUrl = typeof result.output === "string" ? result.output : result.output?.[0];
if (!videoUrl) throw new Error("Runway: No video returned");
const videoResp = await fetch(videoUrl);
const buffer = Buffer.from(await videoResp.arrayBuffer());
const filename = `runway-${Date.now()}.mp4`;
const filepath = path.join(MEDIA_DIR, filename);
writeFileSync(filepath, buffer);
return { provider: "runway-gen3", url: `/output/media/${filename}`, filepath };
}
// --- Smart Generator: picks best available provider ---
export async function generateImage(prompt, aspectRatio = "portrait") {
const sizeMap = {
portrait: { dalle: "1024x1792", stability: "9:16", fal: "portrait_16_9" },
square: { dalle: "1024x1024", stability: "1:1", fal: "square_hd" },
landscape: { dalle: "1792x1024", stability: "16:9", fal: "landscape_16_9" },
};
const sizes = sizeMap[aspectRatio] || sizeMap.portrait;
// Try providers in order of preference
const providers = [
{ name: "fal", key: "FAL_KEY", fn: () => generateImageFal(prompt, sizes.fal) },
{ name: "dalle", key: "OPENAI_API_KEY", fn: () => generateImageDalle(prompt, sizes.dalle) },
{ name: "stability", key: "STABILITY_API_KEY", fn: () => generateImageStability(prompt, sizes.stability) },
];
for (const p of providers) {
if (process.env[p.key]) {
try {
return await p.fn();
} catch (err) {
console.error(`${p.name} failed: ${err.message}`);
}
}
}
throw new Error("Keine Bild-AI konfiguriert. Setze OPENAI_API_KEY, STABILITY_API_KEY oder FAL_KEY in .env");
}
export async function generateAIVideo(prompt, imageUrl, preferredProvider) {
const allProviders = [
{ name: "kling", key: "FAL_KEY", fn: () => generateVideoKling(prompt, imageUrl) },
{ name: "hailuo", key: "FAL_KEY", fn: () => generateVideoHailuo(prompt, imageUrl) },
{ name: "luma", key: "FAL_KEY", fn: () => generateVideoLuma(prompt, imageUrl) },
{ name: "minimax", key: "FAL_KEY", fn: () => generateVideoFal(prompt, imageUrl) },
{ name: "runway", key: "REPLICATE_API_TOKEN", fn: () => generateVideoRunway(prompt, imageUrl) },
{ name: "replicate", key: "REPLICATE_API_TOKEN", fn: () => generateVideoReplicate(prompt, imageUrl) },
];
// If preferred provider specified, try it first
const providers = preferredProvider
? [
...allProviders.filter(p => p.name === preferredProvider),
...allProviders.filter(p => p.name !== preferredProvider),
]
: allProviders;
for (const p of providers) {
if (process.env[p.key]) {
try {
return await p.fn();
} catch (err) {
console.error(`${p.name} video failed: ${err.message}`);
}
}
}
throw new Error("Keine Video-AI konfiguriert. Setze FAL_KEY oder REPLICATE_API_TOKEN in .env");
}
// --- Full Pipeline: Content → Image → Video ---
export async function generateFullMedia(content, topic, format, aspectRatio = "portrait") {
const imagePrompt = buildImagePrompt(topic, "cinematic", content.slice(0, 200));
const results = { image: null, video: null, errors: [] };
// Step 1: Generate Image
try {
results.image = await generateImage(imagePrompt, aspectRatio);
} catch (err) {
results.errors.push(`Bild: ${err.message}`);
}
// Step 2: Generate AI Video (image-to-video if image available)
try {
const videoPrompt = `${content.slice(0, 100)}, cinematic, smooth, professional, faceless content`;
const imgUrl = results.image?.filepath ? `file://${results.image.filepath}` : undefined;
results.video = await generateAIVideo(videoPrompt, imgUrl);
} catch (err) {
results.errors.push(`Video: ${err.message}`);
}
return results;
}