Initial release of PDEV AI Video Studio
This commit is contained in:
commit
754c56c478
19 files changed
+11121
No files matched your search
+105
@@ -0,0 +1,105 @@
|
||||
import "dotenv/config";
|
||||
import cors from "cors";
|
||||
import express from "express";
|
||||
import {mkdir, writeFile} from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import {fileURLToPath} from "node:url";
|
||||
import {bundle} from "@remotion/bundler";
|
||||
import {renderMedia, selectComposition} from "@remotion/renderer";
|
||||
|
||||
const root = path.dirname(fileURLToPath(import.meta.url));
|
||||
const appRoot = process.env.PDEV_APP_ROOT || root;
|
||||
const outputDir = process.env.PDEV_OUTPUT_DIR || path.join(root, "out");
|
||||
const mediaDir = process.env.PDEV_MEDIA_DIR || path.join(outputDir, "media");
|
||||
const remotionEntry = process.env.PDEV_REMOTION_ENTRY || path.join(root, "src/remotion/index.ts");
|
||||
const app = express();
|
||||
const port = Number(process.env.PORT || 4318);
|
||||
const jobs = new Map();
|
||||
let bundlePromise;
|
||||
|
||||
app.use(cors());
|
||||
app.use(express.json({limit: "10mb"}));
|
||||
app.use("/renders", express.static(outputDir));
|
||||
app.use("/media", express.static(mediaDir, {acceptRanges: true}));
|
||||
app.get("/api/health", (_req, res) => res.json({status: "ok", desktop: Boolean(process.env.PDEV_DESKTOP)}));
|
||||
|
||||
app.post("/api/media", express.raw({type: "application/octet-stream", limit: "2gb"}), async (req, res) => {
|
||||
try {
|
||||
const original = decodeURIComponent(String(req.headers["x-file-name"] || "media.mp4"));
|
||||
const extension = path.extname(original).replace(/[^.a-zA-Z0-9]/g, "").slice(0, 10) || ".mp4";
|
||||
const base = path.basename(original, path.extname(original)).replace(/[^a-zA-Z0-9_-]+/g, "-").slice(0, 60) || "media";
|
||||
const fileName = `${Date.now()}-${base}${extension}`;
|
||||
await mkdir(mediaDir, {recursive: true});
|
||||
await writeFile(path.join(mediaDir, fileName), req.body);
|
||||
res.json({name: original, url: `${req.protocol}://${req.get("host")}/media/${encodeURIComponent(fileName)}`, size: req.body.length});
|
||||
} catch (error) {
|
||||
res.status(500).json({error: error instanceof Error ? error.message : String(error)});
|
||||
}
|
||||
});
|
||||
|
||||
const fallbackStoryboard = (prompt) => {
|
||||
const subject = prompt.trim().replace(/\s+/g, " ").slice(0, 90) || "Deine nächste große Idee";
|
||||
return [
|
||||
{layout: "hero", eyebrow: "DER HOOK", title: subject, body: "Eine Geschichte, die in der ersten Sekunde Aufmerksamkeit gewinnt.", accent: "#8b5cf6", duration: 3.5},
|
||||
{layout: "split", eyebrow: "DAS PROBLEM", title: "Komplex muss nicht kompliziert wirken.", body: "Wir reduzieren die Botschaft auf das, was wirklich hängen bleibt.", accent: "#2dd4bf", duration: 4},
|
||||
{layout: "steps", eyebrow: "DER WEG", title: "Idee. Bewegung. Wirkung.", body: "Ein klarer visueller Rhythmus führt durch die Story.", accent: "#38bdf8", duration: 4.5},
|
||||
{layout: "stat", eyebrow: "DER BEWEIS", title: "3x", body: "Stärkere Botschaft durch präzises Timing, Kontrast und Fokus.", accent: "#f59e0b", duration: 3.5},
|
||||
{layout: "outro", eyebrow: "READY TO CREATE", title: "Mach die Idee sichtbar.", body: "PDEV AI Video Studio", accent: "#8b5cf6", duration: 3.5}
|
||||
].map((scene, index) => ({...scene, id: `ai-${Date.now()}-${index}`}));
|
||||
};
|
||||
|
||||
app.post("/api/generate", async (req, res) => {
|
||||
const prompt = String(req.body?.prompt || "");
|
||||
if (!process.env.OPENAI_API_KEY) return res.json({scenes: fallbackStoryboard(prompt), source: "local"});
|
||||
try {
|
||||
const response = await fetch("https://api.openai.com/v1/chat/completions", {method: "POST", headers: {"Content-Type": "application/json", Authorization: `Bearer ${process.env.OPENAI_API_KEY}`}, body: JSON.stringify({model: process.env.OPENAI_MODEL || "gpt-4.1-mini", response_format: {type: "json_object"}, messages: [{role: "system", content: "Du bist Creative Director für kurze Social Videos. Antworte nur als JSON {scenes:[...]}. Erzeuge 4-7 deutsche Szenen. Jede Szene braucht layout (hero|split|stat|steps|quote|outro), eyebrow, title, body, accent als Hexfarbe und duration zwischen 2 und 8 Sekunden."}, {role: "user", content: prompt}]})});
|
||||
if (!response.ok) throw new Error(`OpenAI ${response.status}`);
|
||||
const data = await response.json();
|
||||
const parsed = JSON.parse(data.choices[0].message.content);
|
||||
const scenes = parsed.scenes.map((scene, index) => ({...scene, id: `ai-${Date.now()}-${index}`}));
|
||||
res.json({scenes, source: "openai"});
|
||||
} catch (error) {
|
||||
res.json({scenes: fallbackStoryboard(prompt), source: "fallback", warning: error.message});
|
||||
}
|
||||
});
|
||||
|
||||
app.post("/api/render", async (req, res) => {
|
||||
const project = req.body?.project;
|
||||
if (!project || (project.activeMode === "cut" ? !project.clips?.length : !project.scenes?.length)) {
|
||||
return res.status(400).json({error: project?.activeMode === "cut" ? "Die Sequenz enthält keine Clips." : "Das Projekt enthält keine Szenen."});
|
||||
}
|
||||
const id = `${Date.now()}-${Math.random().toString(36).slice(2, 7)}`;
|
||||
jobs.set(id, {status: "queued", progress: 0});
|
||||
res.status(202).json({id});
|
||||
try {
|
||||
jobs.set(id, {status: "bundling", progress: 0.03});
|
||||
bundlePromise ||= bundle({entryPoint: remotionEntry, webpackOverride: (config) => config});
|
||||
const serveUrl = await bundlePromise;
|
||||
const [width, height] = project.format === "16:9" ? [1920, 1080] : project.format === "1:1" ? [1080, 1080] : [1080, 1920];
|
||||
const durationSeconds = project.activeMode === "cut"
|
||||
? Math.max(1, ...project.clips.map((clip) => Number(clip.start) + (Number(clip.sourceEnd) - Number(clip.sourceStart)) / Math.max(0.1, Number(clip.speed))))
|
||||
: project.scenes.reduce((sum, scene) => sum + Number(scene.duration), 0);
|
||||
const durationInFrames = Math.max(1, Math.round(durationSeconds * project.fps));
|
||||
const composition = await selectComposition({serveUrl, id: "PDEVVideo", inputProps: project});
|
||||
await mkdir(outputDir, {recursive: true});
|
||||
const fileName = `pdev-${id}.mp4`;
|
||||
jobs.set(id, {status: "rendering", progress: 0.08});
|
||||
await renderMedia({composition: {...composition, width, height, durationInFrames, fps: project.fps}, serveUrl, codec: "h264", outputLocation: path.join(outputDir, fileName), inputProps: project, onProgress: ({progress}) => jobs.set(id, {status: "rendering", progress: 0.08 + progress * 0.92})});
|
||||
jobs.set(id, {status: "done", progress: 1, url: `/renders/${fileName}`});
|
||||
} catch (error) {
|
||||
jobs.set(id, {status: "error", progress: 0, error: error instanceof Error ? error.message : String(error)});
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/api/render/:id", (req, res) => {
|
||||
const job = jobs.get(req.params.id);
|
||||
if (!job) return res.status(404).json({error: "Render-Job nicht gefunden."});
|
||||
res.json(job);
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
app.use(express.static(path.join(appRoot, "dist")));
|
||||
app.use((_req, res) => res.sendFile(path.join(appRoot, "dist/index.html")));
|
||||
}
|
||||
|
||||
app.listen(port, () => console.log(`PDEV render server: http://localhost:${port}`));
|
||||
Reference in new issue
Block a user