// Listing reel from your photos: a property's photos become a vertical reel, // each with a slow push-in or pan, the address and price over the opening, a // logo in the corner, a music bed, and a closing card. A 30-second reel from // ten photos is 45 credits; the Preview is free. // The calls of the page https://api.oneupai.com/docs/recipes/listing-reel, in // its order, as one program. // // In the folder you run it from: the photos as photo-1.jpg, photo-2.jpg, … (in // the order they should play; the page's Agent takes 4 to 40), your logo as // logo.png and the music as music.mp3. Change ADDRESS, DETAILS and LINES below. // // export ONEUP_API_KEY="oneup_live_…" # your Builder key // npx tsx run.mts # Node.js 20 or newer // // ONEUP_BASE_URL changes the host (default https://api.oneupai.com). Every // answer is printed as it arrives. import { readFile, readdir } from "node:fs/promises"; const API = process.env.ONEUP_BASE_URL ?? "https://api.oneupai.com"; const KEY = process.env.ONEUP_API_KEY; if (!KEY) throw new Error("Set ONEUP_API_KEY to your API key first"); const ADDRESS = "14 Maple Lane, Oakridge"; const DETAILS = "$849,000 · 3 bd · 2.5 ba"; // Script lines: [photo number, words shown over that photo]. const LINES: [number, string][] = [ [2, "Sunlit kitchen with an island"], [3, "Primary suite with a view"], ]; // One API call: the answer is printed and returned; a refusal stops here. async function call( method: string, path: string, body?: object, headers?: Record ) { const res = await fetch(API + path, { signal: AbortSignal.timeout(60_000), method, headers: { Authorization: `Bearer ${KEY}`, "Content-Type": "application/json", ...headers, }, body: body && JSON.stringify(body), }); const json = await res.json(); console.log(JSON.stringify(json)); if (!res.ok) throw new Error(`${res.status} ${json.error}: ${json.message}`); return json; } async function upload(file: string, type: string): Promise { const bytes = await readFile(file); const target = await call("POST", "/v1/uploads/initiate", { content_type: type, file_name: file, size_bytes: bytes.length, }); const put = await fetch(target.upload_url, { signal: AbortSignal.timeout(300_000), method: "PUT", headers: { "Content-Type": type }, body: bytes, }); if (!put.ok) throw new Error(`${file}: the upload answered ${put.status}`); return target.upload_id; } async function render(projectId: string, versionId: string, kind: string) { let r = await call( "POST", "/v1/renders", { alias: "oneup/render-timeline", project_id: projectId, version_id: versionId, kind, }, { "Idempotency-Key": crypto.randomUUID() } ); const renderId = r.render_id; while (r.status !== "COMPLETED" && r.status !== "FAILED") { await new Promise((resolve) => setTimeout(resolve, 5000)); r = await call("GET", `/v1/renders/${renderId}`); } return r; } const files = (await readdir(".")) .filter((f) => /^photo-\d+\.jpg$/.test(f)) .sort((a, b) => parseInt(a.slice(6)) - parseInt(b.slice(6))); if (!files.length) throw new Error("no photo-*.jpg in this folder"); const photos: string[] = []; for (const file of files) photos.push(await upload(file, "image/jpeg")); const logo = await upload("logo.png", "image/png"); const music = await upload("music.mp3", "audio/mpeg"); const project = await call("POST", "/v1/projects", { agent_slug: "oneup/render-timeline", }); const projectId = project.project_id; // Each photo is 3 s, joined by 0.4 s crossfades; the card is 4 s. The reel's // length is the durations added up, minus each crossfade. const n = photos.length; const total = n * 3000 + 4000 - n * 400; const pushIn = { op: "kenburns", from: { x: 0.5, y: 0.5, zoom: 1 }, to: { x: 0.5, y: 0.5, zoom: 1.18 }, ease: "ease_in_out", }; const pan = { op: "kenburns", from: { x: 0.58, y: 0.5, zoom: 1.18 }, to: { x: 0.42, y: 0.5, zoom: 1.18 }, ease: "ease_in_out", }; const timeline = { v: "1.1", output: { aspect: "9:16", resolution: 1080, fps: 30, container: "mp4" }, segments: [ ...photos.map((id, i) => ({ id: `p${i + 1}`, source: { $asset: id }, duration: 3000, effects: [i % 2 === 0 ? pushIn : pan], transition_out: { op: "crossfade", duration: 400 }, })), { id: "card", source: { $asset: photos[n - 1] }, duration: 4000, effects: [{ op: "fade", from: 1, to: 0, duration: 600 }], }, ], overlays: [ { id: "logo", kind: "image", source: { $asset: logo }, position: { x: 0.05, y: 0.028 }, scale: 0.12, range: { from: 0, to: total }, }, { id: "t1", kind: "text", content: ADDRESS, style: "lower_third", position: "middle", range: { from: 0, to: 3000 }, enter: { op: "fade", duration: 250 }, exit: { op: "fade", duration: 200 }, }, { id: "t2", kind: "text", content: DETAILS, style: "lower_third", position: "bottom", range: { from: 0, to: 3000 }, }, ...LINES.filter(([photo]) => photo >= 2 && photo <= n).map( ([photo, words]) => ({ id: `line${photo}`, kind: "text", content: words, style: "lower_third", position: "bottom", range: { from: (photo - 1) * 2600 + 400, to: (photo - 1) * 2600 + 3000, }, }) ), ], audio: [ { id: "bed", role: "music", source: { $asset: music }, range: { from: 0, to: total }, loop: true, gain: -14, fade_in: 600, fade_out: 600, }, ], }; const version = await call("PUT", `/v1/projects/${projectId}/timeline`, { timeline, }); await render(projectId, version.version_id, "preview"); const final = await render(projectId, version.version_id, "final"); await call("GET", `/v1/queue/result/${final.ticket}`);