Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/react/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
MusicProps,
OverlayProps,
PackshotProps,
RawProps,
RenderProps,
SliderProps,
SpeechProps,
Expand Down Expand Up @@ -139,3 +140,11 @@ export function Swipe(props: SwipeProps): VargElement<"swipe"> {
export function Packshot(props: PackshotProps): VargElement<"packshot"> {
return createElement("packshot", props as Record<string, unknown>, undefined);
}

export function Raw(props: RawProps): VargElement<"raw"> {
return createElement(
"raw",
props as unknown as Record<string, unknown>,
undefined,
);
}
2 changes: 2 additions & 0 deletions src/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
Music,
Overlay,
Packshot,
Raw,
Render,
Slider,
Speech,
Expand All @@ -27,6 +28,7 @@ export type {
OverlayProps,
PackshotProps,
PositionProps,
RawProps,
RenderOptions,
RenderProps,
SliderProps,
Expand Down
61 changes: 61 additions & 0 deletions src/react/renderers/raw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { $ } from "bun";
import { File } from "../../ai-sdk/file";
import type { RawInput, RawProps, VargElement } from "../types";
import type { RenderContext } from "./context";
import { renderImage } from "./image";
import { renderVideo } from "./video";

async function resolveInput(
input: RawInput,
ctx: RenderContext,
): Promise<string> {
if (typeof input === "string") {
return input;
}

if (input instanceof Uint8Array) {
const tempPath = await File.toTemp({ uint8Array: input });
ctx.tempFiles.push(tempPath);
return tempPath;
}

if (input.type === "image") {
return renderImage(input as VargElement<"image">, ctx);
}

if (input.type === "video") {
return renderVideo(input as VargElement<"video">, ctx);
}

if (input.type === "raw") {
return renderRaw(input as VargElement<"raw">, ctx);
}

throw new Error(`Unsupported Raw input type: ${(input as VargElement).type}`);
}

export async function renderRaw(
element: VargElement<"raw">,
ctx: RenderContext,
): Promise<string> {
const props = element.props as unknown as RawProps;

const inputPaths = await Promise.all(
props.inputs.map((input) => resolveInput(input, ctx)),
);

const outPath = props.output ?? `/tmp/varg-raw-${Date.now()}.mp4`;

const inputArgs = inputPaths.flatMap((path) => ["-i", path]);
const ffmpegArgs = [...inputArgs, ...props.args, "-y", outPath];

const result =
await $`ffmpeg -hide_banner -loglevel error ${ffmpegArgs}`.quiet();

if (result.exitCode !== 0) {
throw new Error(`ffmpeg failed with exit code ${result.exitCode}`);
}

ctx.tempFiles.push(outPath);
return outPath;
}
6 changes: 5 additions & 1 deletion src/react/renderers/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
import type { RenderContext } from "./context";
import { renderImage } from "./image";
import { addTask, completeTask, startTask } from "./progress";
import { renderRaw } from "./raw";
import { renderSpeech } from "./speech";
import { computeCacheKey, toFileUrl } from "./utils";

Expand Down Expand Up @@ -107,7 +108,10 @@ export async function renderVideo(
const props = element.props as VideoProps;

if (props.src && !props.prompt) {
return props.src;
if (typeof props.src === "object" && props.src.type === "raw") {
return renderRaw(props.src, ctx);
}
return props.src as string;
}

const prompt = props.prompt;
Expand Down
19 changes: 17 additions & 2 deletions src/react/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export type VargElementType =
| "split"
| "slider"
| "swipe"
| "packshot";
| "packshot"
| "raw";

export interface VargElement<T extends VargElementType = VargElementType> {
type: T;
Expand Down Expand Up @@ -115,7 +116,7 @@ export type VideoProps = BaseProps &
AudioProps &
TrimProps & {
prompt?: VideoPrompt;
src?: string;
src?: string | VargElement<"raw">;
model?: VideoModelV3;
resize?: ResizeMode;
aspectRatio?: `${number}:${number}`;
Expand Down Expand Up @@ -202,6 +203,19 @@ export interface PackshotProps extends BaseProps {
duration?: number;
}

export type RawInput =
| string
| Uint8Array
| VargElement<"image">
| VargElement<"video">
| VargElement<"raw">;

export interface RawProps extends BaseProps {
inputs: RawInput[];
args: string[];
output?: string;
}

export type RenderMode = "strict" | "preview";

export interface DefaultModels {
Expand Down Expand Up @@ -236,4 +250,5 @@ export interface ElementPropsMap {
slider: SliderProps;
swipe: SwipeProps;
packshot: PackshotProps;
raw: RawProps;
}