// Als ESM speichern (z. B. clip.mjs) und mit `node clip.mjs` laufen lassen.
import { writeFileSync } from "node:fs";
const API = "https://www.lucflow.de/api/v1";
const headers = { Authorization: `Bearer ${process.env.LUCFLOW_API_KEY}` };
// 1. Job anlegen
const create = await fetch(`${API}/jobs`, {
method: "POST",
headers: { ...headers, "Content-Type": "application/json" },
body: JSON.stringify({
source_url: "https://www.youtube.com/watch?v=…",
options: { format: "9:16", content_type: "gaming" },
}),
});
const { id } = await create.json();
// 2. Pollen, bis der Job fertig ist
let job;
do {
await new Promise((r) => setTimeout(r, 30_000));
job = await (await fetch(`${API}/jobs/${id}`, { headers })).json();
console.log(job.status, `${job.progress}%`);
} while (job.status === "queued" || job.status === "processing");
if (job.status !== "done") throw new Error(job.error ?? "failed");
// 3. Ersten Clip speichern
const clip = job.clips[0];
const res = await fetch(clip.download_url, { headers });
const buf = Buffer.from(await res.arrayBuffer());
writeFileSync(`${clip.id}.mp4`, buf);