Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

raea cut takes a sound-only take (#89, item 2)

shinyobjectz09ee78513801
diff --git a/engine/cli/src/index.ts b/engine/cli/src/index.ts
index e1bf3471..33b853e9 100644
--- a/engine/cli/src/index.ts
+++ b/engine/cli/src/index.ts
@@ -41,19 +41,21 @@ export interface Cut {
from: number;
to: number;
line: string;
+ /** a sound-only take's clip plays as <Audio> (#89); absent means video */
+ kind?: "video" | "audio";
}
/** cuts.ts from the clips: data the edit imports (each take's file as a module, so the bundler finds the media). */
export function cutsModule(clips: Cut[], edit: { width: number; height: number; fps: number }): string {
const files = [...new Set(clips.map((c) => c.file))];
const id = (f: string) => "m_" + f.replace(/\W/g, "_");
- const rows = clips.map((c) => ` { take: ${JSON.stringify(c.take)}, src: ${id(c.file)}, from: ${c.from.toFixed(3)}, to: ${c.to.toFixed(3)}, line: ${JSON.stringify(c.line)} },`);
+ const rows = clips.map((c) => ` { take: ${JSON.stringify(c.take)}, src: ${id(c.file)}, from: ${c.from.toFixed(3)}, to: ${c.to.toFixed(3)}, line: ${JSON.stringify(c.line)}, kind: ${JSON.stringify(c.kind ?? "video")} },`);
return `// written by \`raea cut\`: the clips, in order, in each take's own seconds. Re-run raea cut to change them, or edit
// by hand (the edit reads only this file). cuts.json beside it is the same list for raea's checks.
${files.map((f) => `import ${id(f)} from ${JSON.stringify("./" + f)};`).join("\n")}
export const edit = ${JSON.stringify(edit)};
-export type Cut = { take: string; src: string; from: number; to: number; line: string };
+export type Cut = { take: string; src: string; from: number; to: number; line: string; kind: "video" | "audio" };
export const cuts: Cut[] = [
${rows.join("\n")}
];
diff --git a/engine/cli/src/native.ts b/engine/cli/src/native.ts
index e99f4b84..ab536b58 100644
--- a/engine/cli/src/native.ts
+++ b/engine/cli/src/native.ts
@@ -35,6 +35,10 @@ function engineRun(a: string[]): string {
const engineJson = (a: string[]): any => JSON.parse(engineRun(a));
const VIDEO = ["mp4", "mov", "m4v", "mkv", "webm"];
+/** a take with sound alone (a voice recording, a bed): cut on its words as a video is, played as <Audio> (#89) */
+const AUDIO = ["wav", "mp3", "m4a", "aac", "flac", "aif", "aiff", "ogg", "opus"];
+const extOf = (f: string) => extname(f).slice(1).toLowerCase();
+const isAudio = (f: string) => AUDIO.includes(extOf(f));
/** Every take under media/: its path from the project's root */
function takes(): string[] {
@@ -43,7 +47,7 @@ function takes(): string[] {
for (const e of existsSync(at(dir)) ? readdirSync(at(dir), { withFileTypes: true }) : []) {
const p = `${dir}/${e.name}`;
if (e.isDirectory()) walk(p);
- else if (VIDEO.includes(extname(e.name).slice(1).toLowerCase())) out.push(p);
+ else if (VIDEO.includes(extOf(e.name)) || AUDIO.includes(extOf(e.name))) out.push(p);
}
};
walk("media");
@@ -115,7 +119,8 @@ function hearTake(take: string, out: string) {
function cutPlan(take: string, ws: Word[], lines: string[]): { plan: [number, number, string][]; notes: string[] } {
const norm = (s: string) => s.toLowerCase().replace(/[^\p{L}\p{N}'\s]/gu, "");
const toks = ws.map((w) => norm(w.text).trim());
- const cuts = (engineJson(["media", "cuts", at(take), "--threshold", "0.08"]).cuts as { at: number }[]).map((c) => c.at);
+ // a sound-only take has no jump cuts to keep off, and its grid is the edit's usual 30 (a probe has no video fps)
+ const cuts = isAudio(take) ? [] : (engineJson(["media", "cuts", at(take), "--threshold", "0.08"]).cuts as { at: number }[]).map((c) => c.at);
const fps = (engineJson(["media", "probe", at(take)]).video?.fps as number) || 30;
let from = 0;
const spans: [number, number][] = [];
@@ -172,7 +177,7 @@ function cutPlan(take: string, ws: Word[], lines: string[]): { plan: [number, nu
const framesOf = (from: number, to: number, fps: number) => Math.max(1, Math.round((to - from) * fps));
const ms = (s: number) => Math.round(s * 1000) / 1000;
-type Clip = { take: string; file: string; from: number; to: number; line: string };
+type Clip = { take: string; file: string; from: number; to: number; line: string; kind?: "video" | "audio" };
/** `raea cut`'s host step: the clips for the lines (or --at spans), the words in the edit's frames, and what was cut */
function cut(a: { take: string; lines: string[]; spans: number[][]; keep: Clip[] }) {
@@ -180,19 +185,20 @@ function cut(a: { take: string; lines: string[]; spans: number[][]; keep: Clip[]
if (!a.lines.length && !a.spans.length) throw new Error(`cut needs lines ("what's said") or spans (--at A-B, in the take's seconds)`);
const edit = shape();
const stem = basename(file, extname(file));
+ const kind = isAudio(file) ? "audio" as const : "video" as const;
const clips: Clip[] = [...a.keep];
let notes: string[] = [];
if (a.lines.length) {
const r = cutPlan(file, words(file, true), a.lines);
notes = r.notes;
- for (const [from, to, line] of r.plan) clips.push({ take: stem, file, from: ms(from), to: ms(to), line });
+ for (const [from, to, line] of r.plan) clips.push({ take: stem, file, from: ms(from), to: ms(to), line, kind });
}
const { dur } = probeOf(file);
for (const [s, e] of a.spans) {
const [from, to] = [Math.max(s, 0), dur > 0 ? Math.min(e, dur) : e];
if (!(to > from)) throw new Error(`the span ${s}-${e} is outside ${file} (${dur.toFixed(1)} s)`);
const line = words(file).filter((w) => w.from >= from - 0.02 && w.to <= to + 0.08).map((w) => w.text).join(" ");
- clips.push({ take: stem, file, from: ms(from), to: ms(to), line });
+ clips.push({ take: stem, file, from: ms(from), to: ms(to), line, kind });
}
// the words in the edit's frames, for captions (useWords())
let t = 0;
@@ -256,6 +262,8 @@ function perception(edit: { width: number; height: number; fps: number }, clips:
for (const c of clips) {
const mid = (c.from + c.to) / 2;
const key = `${c.file}|${Math.floor(mid * 10)}`;
+ // a sound-only clip has no face to find: the band alone
+ if (!seen.has(key) && (c.kind === "audio" || isAudio(c.file))) seen.set(key, undefined);
if (!seen.has(key)) {
let face: Box | undefined;
try {
diff --git a/engine/cli/src/registry.gen.ts b/engine/cli/src/registry.gen.ts
index 9a59b3be..282f469d 100644
--- a/engine/cli/src/registry.gen.ts
+++ b/engine/cli/src/registry.gen.ts
@@ -22,6 +22,6 @@ export const REGISTRY: Record<string, Record<string, string>> = {
}
};
export const TEMPLATE: Record<string, string> = {
- "edit.tsx": "// The edit: a React composition rendered by raea (`raea render`). The clips come from cuts.ts (`raea cut` writes it),\n// the parts from components/ (`raea part add` copies them in; they're yours to change), the look from brand/brand.css.\n// Change anything here: layout, timing, what plays when. `raea check` renders it and says what's wrong.\nimport { AbsoluteFill, Composition, Series, Video, registerRoot } from \"raea\";\nimport { cuts, edit, frames, total } from \"./cuts\";\nimport { css } from \"./styles.gen\";\n\n// Before any cuts: a card saying what comes next, three seconds long. Its colours are its own on purpose, not a brand's\n// (`raea brand SITE` writes brand/brand.css); delete it once the edit has clips.\nfunction Start() {\n return (\n <AbsoluteFill style={{ background: \"#14120f\", color: \"#f2ede4\", justifyContent: \"center\", padding: \"0 8%\", fontFamily: \"Helvetica, Arial, sans-serif\" }}>\n <div style={{ fontSize: edit.width * 0.11, fontWeight: 700, letterSpacing: \"-0.02em\" }}>raea</div>\n <div style={{ fontSize: edit.width * 0.04, marginTop: edit.width * 0.03, lineHeight: 1.4, opacity: 0.8 }}>\n No clips yet. Run raea cut on a take to put its footage in cuts.ts, and this edit plays it.\n </div>\n </AbsoluteFill>\n );\n}\n\nfunction Edit() {\n if (cuts.length === 0) return <Start />;\n return (\n <AbsoluteFill className=\"bg-brand-bg\">\n <style>{css}</style>\n <Series>\n {cuts.map((c, i) => (\n <Series.Sequence key={i} durationInFrames={frames(c)}>\n {/* a talking take: <FaceFrame>; a screen recording: <ScreenFit> (`raea frame TAKE` says which) */}\n <Video src={c.src} startFrom={Math.round(c.from * edit.fps)} endAt={Math.round(c.to * edit.fps)} style={{ width: \"100%\", height: \"100%\", objectFit: \"cover\" }} />\n </Series.Sequence>\n ))}\n </Series>\n </AbsoluteFill>\n );\n}\n\nregisterRoot(() => <Composition id=\"edit\" component={Edit} width={edit.width} height={edit.height} fps={edit.fps} durationInFrames={total || edit.fps * 3} />);\n",
+ "edit.tsx": "// The edit: a React composition rendered by raea (`raea render`). The clips come from cuts.ts (`raea cut` writes it),\n// the parts from components/ (`raea part add` copies them in; they're yours to change), the look from brand/brand.css.\n// Change anything here: layout, timing, what plays when. `raea check` renders it and says what's wrong.\nimport { AbsoluteFill, Audio, Composition, Series, Video, registerRoot } from \"raea\";\nimport { cuts, edit, frames, total } from \"./cuts\";\nimport { css } from \"./styles.gen\";\n\n// Before any cuts: a card saying what comes next, three seconds long. Its colours are its own on purpose, not a brand's\n// (`raea brand SITE` writes brand/brand.css); delete it once the edit has clips.\nfunction Start() {\n return (\n <AbsoluteFill style={{ background: \"#14120f\", color: \"#f2ede4\", justifyContent: \"center\", padding: \"0 8%\", fontFamily: \"Helvetica, Arial, sans-serif\" }}>\n <div style={{ fontSize: edit.width * 0.11, fontWeight: 700, letterSpacing: \"-0.02em\" }}>raea</div>\n <div style={{ fontSize: edit.width * 0.04, marginTop: edit.width * 0.03, lineHeight: 1.4, opacity: 0.8 }}>\n No clips yet. Run raea cut on a take to put its footage in cuts.ts, and this edit plays it.\n </div>\n </AbsoluteFill>\n );\n}\n\nfunction Edit() {\n if (cuts.length === 0) return <Start />;\n return (\n <AbsoluteFill className=\"bg-brand-bg\">\n <style>{css}</style>\n <Series>\n {cuts.map((c, i) => (\n <Series.Sequence key={i} durationInFrames={frames(c)}>\n {/* a talking take: <FaceFrame>; a screen recording: <ScreenFit> (`raea frame TAKE` says which); a take with\n sound alone (a voice recording) plays under whatever you put in its place */}\n {c.kind === \"audio\" ? (\n <Audio src={c.src} startFrom={Math.round(c.from * edit.fps)} endAt={Math.round(c.to * edit.fps)} />\n ) : (\n <Video src={c.src} startFrom={Math.round(c.from * edit.fps)} endAt={Math.round(c.to * edit.fps)} style={{ width: \"100%\", height: \"100%\", objectFit: \"cover\" }} />\n )}\n </Series.Sequence>\n ))}\n </Series>\n </AbsoluteFill>\n );\n}\n\nregisterRoot(() => <Composition id=\"edit\" component={Edit} width={edit.width} height={edit.height} fps={edit.fps} durationInFrames={total || edit.fps * 3} />);\n",
"theme.css": "/* The Tailwind theme every raea project compiles with: class names (text-brand-accent, bg-brand-surface, font-brand,\n text-caption, rounded-brand) mapped onto the brand kit's variables (brand/brand.css, the --brand-* format).\n Nothing here is a colour, a font or a size of its own: change the brand, and every component follows. The kit's\n sizes are the website's, so on a 1080x1920 video they're scaled up (the kit's own note: about 2.5x). */\n@theme inline {\n --color-brand-accent: var(--brand-accent);\n --color-brand-bg: var(--brand-background);\n --color-brand-surface: var(--brand-background);\n --color-brand-on-surface: var(--brand-text);\n --color-brand-ink: var(--brand-background);\n --color-brand-on-accent: var(--brand-background);\n --color-brand-1: var(--brand-color-1);\n --color-brand-2: var(--brand-color-2);\n --color-brand-3: var(--brand-color-3);\n --font-brand: var(--brand-font-heading);\n --font-brand-body: var(--brand-font);\n --text-display: calc(var(--brand-h1-size) * 3);\n --text-title: calc(var(--brand-h2-size) * 2.5);\n --text-caption: calc(var(--brand-h2-size) * 2.5);\n --text-body: calc(var(--brand-p-size) * 2.5);\n --text-label: calc(var(--brand-h4-size) * 2.2);\n --radius-brand: var(--brand-radius);\n}\n"
};
diff --git a/engine/cli/template/edit.tsx b/engine/cli/template/edit.tsx
index a899e4f3..5055549c 100644
--- a/engine/cli/template/edit.tsx
+++ b/engine/cli/template/edit.tsx
@@ -1,7 +1,7 @@
// The edit: a React composition rendered by raea (`raea render`). The clips come from cuts.ts (`raea cut` writes it),
// the parts from components/ (`raea part add` copies them in; they're yours to change), the look from brand/brand.css.
// Change anything here: layout, timing, what plays when. `raea check` renders it and says what's wrong.
-import { AbsoluteFill, Composition, Series, Video, registerRoot } from "raea";
+import { AbsoluteFill, Audio, Composition, Series, Video, registerRoot } from "raea";
import { cuts, edit, frames, total } from "./cuts";
import { css } from "./styles.gen";
@@ -26,8 +26,13 @@ function Edit() {
<Series>
{cuts.map((c, i) => (
<Series.Sequence key={i} durationInFrames={frames(c)}>
- {/* a talking take: <FaceFrame>; a screen recording: <ScreenFit> (`raea frame TAKE` says which) */}
- <Video src={c.src} startFrom={Math.round(c.from * edit.fps)} endAt={Math.round(c.to * edit.fps)} style={{ width: "100%", height: "100%", objectFit: "cover" }} />
+ {/* a talking take: <FaceFrame>; a screen recording: <ScreenFit> (`raea frame TAKE` says which); a take with
+ sound alone (a voice recording) plays under whatever you put in its place */}
+ {c.kind === "audio" ? (
+ <Audio src={c.src} startFrom={Math.round(c.from * edit.fps)} endAt={Math.round(c.to * edit.fps)} />
+ ) : (
+ <Video src={c.src} startFrom={Math.round(c.from * edit.fps)} endAt={Math.round(c.to * edit.fps)} style={{ width: "100%", height: "100%", objectFit: "cover" }} />
+ )}
</Series.Sequence>
))}
</Series>