Media Transforms
Media Transforms turn any file the user uploads — or any URL on the open web, or any route from your own deployment — into the shape you actually want to serve. Normalize a wobbly phone-recorded webm into a clean mp4, pull a poster frame out of it, resize an oversized photo, detect the faces in an avatar so a square crop lands on the face instead of a shoulder, extract text from a scanned PDF, convert a Word doc to PDF, fill a .docx template with the user’s logo, screenshot a public webpage, render an OG image from your own /og/posts/[slug] route, or produce a deterministic MP4 from an HTML composition with animations. Your code asks for the transform; the platform queues, runs, and stores the result. In development, the CLI processes everything locally. In production, Maravilla Cloud handles it. Your code works identically in both environments.
import { getPlatform } from '@maravilla-labs/platform';
const platform = getPlatform();
// Extract a poster frame one second into the video
const job = await platform.media.transforms.thumbnail('uploads/videos/clip.webm', {
at: '1s',
width: 640,
format: 'jpg',
});
// Render the derived file at `/api/v/{job.output_key}` — no round-trip needed.
How It Works
- Your server (or a handler running in response to an upload) calls a transform method like
transcodeorthumbnail. The call returns immediately with a JobHandle — no blocking on a multi-minute encode. - The job is queued and processed in the background. The derived file’s storage key is deterministic — computed from the source key and the options — so you can render the final URL the moment you enqueue the job.
- Clients subscribe to lifecycle events (
transform.queued,transform.started,transform.progress,transform.complete,transform.failed) via Real-Time Events and update the UI as work progresses. - The output lands in Storage under the
__derived/prefix, served by the same/api/v/...route as any other file.
Two entry points: the explicit platform.media.transforms.* API in any server code, and a declarative transforms block in your maravilla.config.ts that reacts to new uploads automatically.
Transform Operations
transcode(srcKey, opts)
Re-encode a video into a consistent container and codec. Use this to normalize the output of mobile browsers (which write whatever their OS supports) into something every device plays.
Transcode browser & webcam recordings before you seek into them. Video captured in the browser (MediaRecorder) or by some screen/webcam tools is variable-frame-rate — its frames are spaced unevenly in time, so the clip doesn’t seek to an arbitrary timestamp reliably and can stall partway through continuous playback. Transcoding rewrites it to an evenly-paced, constant-frame-rate clip that seeks frame-accurately. This is a hard prerequisite for extractFrames and for driving a Frames Render from an uploaded clip — both step through the source frame by frame, so feed them a transcoded clip, never the raw recording.
Parameters:
srcKey(string) — storage key of the source videoopts(object):format('mp4' | 'webm') — target containercodec(string, optional) — specific video codec (e.g.'h264','vp9')max_width(number, optional) — cap on output width; aspect ratio preservedmax_height(number, optional) — cap on output heightaudio_codec(string, optional) —'aac','opus', etc.bitrate_kbps(number, optional) — target bitrate. Treated as a cap, not a goal: the encoder never goes above the source’s own bitrate, so a small low-bitrate clip is not inflated into a larger filefps(number, optional) — force a constant output frame rate. When omitted, constant frame rate is applied automatically only if the source probes as variable-frame-ratekeyframe_interval_ms(number, optional) — force a keyframe at least every N milliseconds;1000gives an edit copy that scrubs quickly
Returns: JobHandle — { id, src_key, output_key, status }
const job = await platform.media.transforms.transcode('uploads/videos/raw.webm', {
format: 'mp4',
max_width: 1080,
});
// Serve the result at `/api/v/${job.output_key}` as soon as `transform.complete` fires.
Already-web-ready sources are copied, not re-encoded. When the target is an MP4 with H.264/AAC and the source is already an H.264 (8-bit 4:2:0) / AAC (or silent) MP4 or MOV that is constant-frame-rate, fits within max_width / max_height, is not above bitrate_kbps, and you pinned neither fps nor keyframe_interval_ms, the job re-wraps the streams with -c copy -movflags +faststart instead of re-encoding. That is faster and lossless. The copy is verified before it is used: the output is re-probed, checked for size and bitrate against the bytes on disk, and fully decoded. If any check fails the job falls back to a normal re-encode, so the result is always a valid file. Sources over the limits are still downscaled and capped.
thumbnail(srcKey, opts)
Extract a single frame from a video as an image. Perfect for poster frames in <video poster=...>.
Parameters:
srcKey(string) — storage key of the source videoopts(object):at(string) — offset into the source. Accepts'1s','00:00:01', or a plain number of secondswidth(number, optional) — output width in pixelsheight(number, optional) — output height in pixelsformat('jpg' | 'png' | 'webp', default:'jpg') — output formatquality(number, optional) — 1–100 quality hint for jpg / webptile(object, optional) —{ frames, columns }: produce a filmstrip instead of one frame.framesthumbnails are spread evenly across the whole source and tiled left-to-right, top-to-bottom,columnsper row.atis ignored andwidth/heightsize a single tile
const poster = await platform.media.transforms.thumbnail('uploads/videos/clip.webm', {
at: '1s',
width: 640,
format: 'jpg',
});
// A 4×3 filmstrip for a scrub bar.
const strip = await platform.media.transforms.thumbnail('uploads/videos/clip.webm', {
at: '0s',
width: 160,
format: 'jpg',
tile: { frames: 12, columns: 4 },
});
JPEG output is full-range and WebP is encoded with libwebp, so H.264 sources work for every format. If at is past the end of the video, the thumbnail falls back to the last frame, then to the first, instead of failing. The same encoders are used by filmstrips and extractFrames.
resize(srcKey, opts)
Resize or reformat an image. Generate responsive variants at upload time so your pages never ship a 5 MB hero image.
Parameters:
-
srcKey(string) — storage key of the source image -
opts(object):width(number, optional) — target width; omit to let height drive aspectheight(number, optional) — target height; omit to let width drive aspectformat('jpg' | 'png' | 'webp') — output formatquality(number, optional) — 1–100 quality hintstrip_metadata(boolean, optional, default:true) — drop EXIF and camera datacrop('focal' | 'center', optional) — crop-to-fill instead of the default aspect-preserving fit. Requires bothwidthandheight; the output is exactlywidth×height.'focal'centers the cut on the image’s stored focal point (falling back to center when none is set);'center'is a plain center crop. Omitcropto keep the aspect-preserving fit.zoom(number0.4..=100.0or'auto', optional) — withcrop: 'focal'only, how tightly to frame the detected face(s).1.0is the smallest crop of the requested aspect that fully contains the subject;> 1pulls back for more context;< 1crops into the subject; large enough saturates to plain scale-to-cover.'auto'frames the subject to a comfortable, consistent occupancy. Because zoom is face-box-relative it self-normalises across images (a distant face and a close one frame the same). Needs stored face data — a no-op with no faces.padding(number0.0..=2.0, optional) — extra margin around the subject box before framing, a fraction of the box added per side (0.15= 15% breathing room).crop: 'focal'only.subject('union' | 'primary', optional) — which detected face(s) to frame:'union'(all faces, default — never clips a face) or'primary'(the highest-confidence face, so a group photo can crop to one person; also re-centres on that face).crop: 'focal'only.
Omit
zoom/padding/subjectfor the exact legacy crop — unset, they don’t change the content-addressed derived key, so existing crops are byte-identical.
const thumb = await platform.media.transforms.resize('uploads/photos/pic.png', {
width: 400,
format: 'webp',
quality: 80,
});
// A roomy avatar: keep the face centered but leave breathing room.
const avatar = await platform.media.transforms.resize('uploads/photos/pic.png', {
width: 512,
height: 512,
format: 'webp',
crop: 'focal',
zoom: 1.6,
});
// A group photo cropped tight to just the main person.
const solo = await platform.media.transforms.resize('uploads/photos/group.jpg', {
width: 400,
height: 500,
format: 'jpg',
crop: 'focal',
subject: 'primary',
zoom: 'auto',
padding: 0.1,
});
Because derived URLs are content-addressed, changing an image’s focal point (or any framing knob) produces a new derived asset — a stale focal crop is never served.
watermark(srcKey, opts)
Stamp a logo and/or a short line of text onto an image or a video — a photo-booth frame, a clip guests share, a preview you don’t want reposted unbranded. The logo sits above the text in the chosen corner, faded to opacity, with a soft dark shadow so it reads on light and dark footage alike. Sizes follow the picture, so the same options look right on a 640 px thumbnail and a 4K frame. The text is set in a font bundled with the platform (Inter SemiBold), so it renders identically in development and production.
The source kind comes from the key’s extension (or, without one, its stored content type): an image keeps its format family unless you pass format; a video (.mp4, .mov, .webm, …) always becomes an H.264/AAC MP4 with +faststart. AAC or MP3 audio is copied; other audio (Opus, Vorbis) is re-encoded to AAC.
Parameters:
srcKey(string) — storage key of the source image or videoopts(object) — givelogo_key,text, or both:logo_key(string, optional) — storage key of the logo: a PNG with transparency (JPEG/WebP also work). Rasterize an SVG logo to PNG firsttext(string, optional) — up to 80 characters of letters, digits, spaces and. , - _ : / @ & + ! ? ' ’ ( ) # · | – — © ® ™ •(e.g.'www.example.com');%,\, double quotes,;,=, brackets and$are rejectedposition('bottom-right' | 'bottom-left' | 'top-right' | 'top-left' | 'bottom-center', default:'bottom-right')margin_pct(number0..=25, default:3) — distance from the edges, percent of the shorter sidelogo_width_pct(number1..=100, default:18) — logo width, percent of the output widthopacity(number,0exclusive to1, default:0.85) — applied to logo and texttext_color('#rrggbb', default:'#ffffff')shadow(boolean, default:true) — soft dark shadow behind logo and textformat('jpg' | 'png' | 'webp', optional) — images only (rejected for videos); defaults to the source’s family (heic/tiff become jpg, gif becomes png)quality(number1..=100, optional) — jpg/webp quality for images (default90); mapped onto the H.264 CRF for videos
Returns: JobHandle — { id, src_key, output_key, status }. Different logos, texts or positions derive different output keys, so each watermark variant is its own cached asset.
// A branded copy of a booth photo.
const photo = await platform.media.transforms.watermark('photos/booth/0001.jpg', {
logo_key: 'brand/logo.png',
text: 'www.example.com',
});
// The same mark on a guest's clip — the output is an MP4.
const clip = await platform.media.transforms.watermark('clips/guest-42.webm', {
logo_key: 'brand/logo.png',
text: 'www.example.com',
position: 'bottom-center',
opacity: 0.9,
});
watermark is not available in the declarative config block; call it from server code or an onStorage handler.
detectFaces(srcKey, opts?)
Find the faces in an image and remember where they are. Detection runs on-platform as a background job like any other transform — no third-party API calls, and your image bytes never leave the platform. The result is a stored focal point: the single anchor your frontend uses for CSS object-position / background-position, and the point that resize’s crop: 'focal' cuts around. Point it at avatars, hero photos, or any user upload where a naive center crop would slice someone’s head off.
Parameters:
srcKey(string) — storage key of the source imageopts(object, optional):min_score(number0..1, default:0.6) — minimum confidence to keep a detected facemax_faces(number, default:32) — cap on faces retained (highest-scoring first)fast(boolean, optional) — lower-accuracy, lower-latency mode
Returns: JobHandle. The derived output at output_key is a JSON artifact — all coordinates normalized 0..1:
{
"faces": [{ "x": 0.31, "y": 0.18, "w": 0.12, "h": 0.2, "score": 0.97 }],
"face_count": 1,
"focal": { "x": 0.37, "y": 0.28 },
"width": 1200,
"height": 1600
}
Re-running detection on an unchanged image completes instantly — results are cached per image content.
// Detect faces, then render the image anchored on the computed focal point.
const job = await platform.media.transforms.detectFaces('uploads/avatars/user-42.jpg');
// Later, on the page (see `getFocalPoint` below for the SSR read):
// <img
// src="/api/v/uploads/avatars/user-42.jpg"
// style={{ objectFit: 'cover', objectPosition: `${x * 100}% ${y * 100}%` }}
// />
Focal points
A focal point is one normalized { x, y } anchor stored per source image. detectFaces writes it automatically; three companion methods let you read it, override it by hand, and clear it. Manual overrides win — a later detectFaces won’t clobber a point you set yourself.
setFocalPoint(srcKey, { x, y })— manually set or override the focal point, each component normalized0..=1. Persists as a manual point that survives later detections.getFocalPoint(srcKey)— read the stored record, ornullif none is set. Returns{ src_key, focal: { x, y }, source: 'faces' | 'manual', face_count, faces?, updated_at }. Cheap enough to call inline during SSR.clearFocalPoint(srcKey)— remove the stored point; resolves totruewhen a record existed,falsewhen there was nothing to clear.
// SSR: read the focal point and emit an object-position, defaulting to center.
const record = await platform.media.transforms.getFocalPoint('uploads/avatars/user-42.jpg');
const { x, y } = record?.focal ?? { x: 0.5, y: 0.5 };
const objectPosition = `${x * 100}% ${y * 100}%`; // '50% 50%' when unset
// <img style={`object-fit:cover;object-position:${objectPosition}`} ... />
CSS-only focal framing (focalCssFrame, unionFaceBox, primaryFaceBox)
Pure, isomorphic, framework-agnostic helpers exported from @maravilla-labs/platform — the browser-side counterpart to resize’s crop: 'focal'. Instead of pre-rendering a server crop per output shape, compute CSS values once and let a single <img> re-frame around the face(s) for any container aspect ratio + zoom/padding/subject, with no server round-trip and no derived asset per shape. They return plain CSS strings/numbers, so any framework (React, Svelte, Nuxt, vanilla) applies them however it applies styles.
focalCssFrame(input)— returns{ objectPosition, scale, transformOrigin }.inputis{ focal, faces?, imgWidth, imgHeight, containerAspect, zoom?, padding?, subject? }.focal/facescome fromgetFocalPoint/detectFaces;imgWidth/imgHeightare the source’s intrinsic dimensions (only their ratio matters — free from a loaded<img>’snaturalWidth/naturalHeight, or known in SSR);containerAspectiswidth / heightof the target box.zoom(number or'auto', default1),padding(default0), andsubject('union' | 'primary') mirror the server knobs exactly. With no faces it degrades to a plain focal-point cover (scale: 1).unionFaceBox(faces?)/primaryFaceBox(faces?)— the union of all faces, or the highest-confidence face (plus its focal), normalized0..1— the same subject selection the server uses.
import { focalCssFrame } from '@maravilla-labs/platform';
const rec = await platform.media.transforms.getFocalPoint('uploads/avatars/user-42.jpg');
const { objectPosition, scale, transformOrigin } = focalCssFrame({
focal: rec?.focal ?? { x: 0.5, y: 0.5 },
faces: rec?.faces,
imgWidth: img.naturalWidth, // browser: the loaded <img>; SSR: known dims
imgHeight: img.naturalHeight,
containerAspect: 3 / 1, // a banner
zoom: 1.4,
});
// <img style={`object-fit:cover; object-position:${objectPosition};
// transform-origin:${transformOrigin}; transform:scale(${scale})`} />
probe(srcKey)
Read metadata without producing a new file. Synchronous (returns MediaInfo directly, not a JobHandle) because the work is cheap.
Returns: MediaInfo — { duration_secs?, width?, height?, video_codec?, audio_codec?, bitrate_bps?, container? }
const meta = await platform.media.transforms.probe('uploads/videos/clip.webm');
if (meta.duration_secs && meta.duration_secs > 60) {
throw new Error('clip too long');
}
ocr(srcKey, opts)
Extract text from an image or PDF.
Parameters:
srcKey(string) — storage key of the source image or PDFopts(object):lang(string, default:'eng') — ISO 639-2 language code, or+-separated combination ('eng+deu')
const job = await platform.media.transforms.ocr('uploads/scans/receipt.png', {
lang: 'eng',
});
// The extracted text lands at job.output_key as a .txt file once complete.
extractFrames(srcKey, opts)
Decode a video into a sequence of still images at a fixed frame rate, written as a numbered frame set plus a small JSON manifest. Use it for scrubbable filmstrips, frame-accurate thumbnails, or to drive a Frames Render composition that plays back an uploaded clip.
Give it a seekable source. Frame extraction steps through the clip at a fixed rate, so a variable-frame-rate recording (anything from
MediaRecorder/ a browser or webcam capture) can yield an uneven or short frame set.transcodeit to a constant-frame-rate mp4 first — see the note there.
Parameters:
srcKey(string) — storage key of the source videoopts(object):fps(number) — frames per second to sample the source atwidth(number, optional) — max width; aspect ratio preserved, no upscalingformat('jpg' | 'png' | 'webp', default:'jpg') — frame image formatquality(number, optional) — 1–100 quality hint for jpg / webpseek_ms(number, optional) — start offset into the clip, in msduration_ms(number, optional) — how much of the clip to extract fromseek_ms, in msdetach_audio(object, optional) — also split the source’s audio into a standalone track in the same job (see below)
job.output_key is the JSON manifest; the individual frame images live alongside it. Frames use the same still encoders as thumbnail: full-range JPEG and libwebp WebP, so H.264 sources work for every format.
Detach the audio in the same call. A recorded clip — a camera or screen capture — carries its picture and sound muxed together. When you want both as separate, independently-editable assets, pass detach_audio and a single call produces the frame set and the audio track, with no second round-trip:
target(string) — storage key to write the extracted audio (AAC / m4a) tobitrate_kbps(number, optional) — audio bitrateseek_ms/duration_ms(number, optional) — same windowing as the frame extraction
const job = await platform.media.transforms.extractFrames('uploads/clips/take.mp4', {
fps: 30,
width: 480,
detach_audio: { target: 'uploads/clips/take.audio.m4a' },
});
// Frame manifest at job.output_key; the detached audio at uploads/clips/take.audio.m4a —
// ready to drop onto a frames timeline as an audio track (below).
extractAudio(srcKey, opts?)
Split a video’s audio track off into a standalone AAC / m4a file. Use it to detach the mic or system audio recorded alongside a clip so it can be placed, gained, faded, and ducked independently on a frames timeline. When you’re already extracting frames from the same upload, prefer extractFrames’s detach_audio option above — it does both in one job.
Parameters:
srcKey(string) — storage key of the source videoopts(object, optional):bitrate_kbps(number, optional) — audio bitrateseek_ms(number, optional) — start offset into the source, in msduration_ms(number, optional) — how much of the source to extract, in ms
const job = await platform.media.transforms.extractAudio('uploads/clips/take.mp4');
// The standalone audio lands at job.output_key once complete.
Document Templating
The headline use case for documents: take a .docx / .odt / .pptx template, fill in a customer name, swap in their logo, and embed a backlink QR code — render the result as a PDF. One API call. Conceptually similar to Word’s mail merge, but driven from your server with everything (text, images, QR codes) in a single render.
docTemplateMerge(srcKey, opts)
The unified template-fill: text + images + QR codes in one call. Use this whenever a render mixes more than one substitution kind. (docReplaceImages and docInsertQrCode below remain available for the narrower single-kind cases.)
Parameters:
srcKey(string) — storage key of the template documentopts(object):data(object, optional) — text-tag → string, e.g.{ '{{NAME}}': 'Acme Corp' }. Tags are matched verbatim; missing tags are silently skipped (idempotent).images(object, optional) — text-tag → image, e.g.{ '{{LOGO}}': { src_key: 'images/logo.png' } }.named_objects(object, optional) — object-name → image. Matches by the drawing object’sNameproperty (set in Word/Writer via Format → Anchor → Properties → Name) and preserves the template’s frame size, border, anchor, and wrap.qr_codes(object, optional) — text-tag →{ payload: string, size?: number }. The platform generates each QR PNG and injects it via the same placeholder pipeline as images.payload≤ 1500 bytes;sizedefaults to 256 px.output_format('pdf' | 'docx' | 'odt' | 'xlsx' | 'html' | 'txt' | 'rtf', optional) — output format; defaults to keeping the input format. Render to PDF if you don’t want the user receiving an editable copy of your template.auto_resize(boolean, default:true) — pre-fit each image (and generated QR) to its target frame’s dimensions, so a 4K user upload doesn’t blow up render memory.
const job = await platform.media.transforms.docTemplateMerge('templates/invoice.docx', {
data: {
'{{CUSTOMER_NAME}}': 'Acme Corp',
'{{INVOICE_ID}}': '#2026-0142',
'{{TOTAL}}': '€ 1,234.56',
},
images: {
'{{CUSTOMER_LOGO}}': { src_key: 'customers/acme/logo.png' },
},
named_objects: {
'CompanyLogo': { src_key: 'brand/maravilla-logo.png' },
},
qr_codes: {
'{{PAYMENT_QR}}': { payload: 'upi://pay?pa=acct@bank&am=1234.56', size: 384 },
},
output_format: 'pdf',
});
// `job.output_key` is the PDF — render an iframe with `/api/v/${job.output_key}` once `transform.complete` fires.
The doc is rendered in one server-side pass — text replacement first, then images, then named-object swaps, then QR injection — same outcome as composing docReplaceImages + docInsertQrCode would give, ~3× faster.
Document Operations
Office documents (.docx, .doc, .odt, .rtf, .xlsx, .xls, .ods, .pptx, .ppt, .odp, .csv, .html, .txt, .epub, .md) come in through the same Storage upload path as anything else and use the same JobHandle lifecycle as the media operations above. Seven methods cover the common needs: convert to PDF, render thumbnails, extract content for LLMs, fill templates with images, and embed QR codes.
docToPdf(srcKey, opts?)
Convert any supported document into a PDF. The most common ask — invoices, contracts, reports, exports.
Parameters:
srcKey(string) — storage key of the source documentopts(object, optional) — reserved for future page-range / quality / accessibility knobs; pass{}or omit
const job = await platform.media.transforms.docToPdf('uploads/contracts/agreement.docx');
// Render the PDF at `/api/v/${job.output_key}` once `transform.complete` fires.
docThumbnail(srcKey, opts)
Render a single page of a document as an image. Useful for document previews in lists, dashboards, and search results.
Parameters:
srcKey(string) — storage key of the source documentopts(object):page(number, default:1) — 1-indexed page number to renderwidth(number, optional) — output width in pixels (height preserves aspect)height(number, optional) — output height in pixelsformat('png' | 'jpg' | 'webp', default:'png') — output formatquality(number, optional) — 1–100 quality hint for jpg / webp
const cover = await platform.media.transforms.docThumbnail('uploads/reports/q3.pptx', {
page: 1,
width: 480,
format: 'webp',
});
docConvert(srcKey, opts)
Convert between any pair of supported document formats — .docx → .pdf, .csv → .xlsx, .html → .docx, etc.
Parameters:
srcKey(string) — storage key of the source documentopts(object):to('pdf' | 'docx' | 'odt' | 'xlsx' | 'html' | 'txt' | 'rtf') — target format
const job = await platform.media.transforms.docConvert('uploads/data/sales.csv', {
to: 'xlsx',
});
docToMarkdown(srcKey, opts?)
Extract a document’s content as clean Markdown — headings, lists, and tables preserved. Built for RAG and LLM pipelines that prefer structured text over raw HTML or PDF.
Parameters:
srcKey(string) — storage key of the source documentopts(object, optional):preserve_tables(boolean, default:true) — keep GitHub-flavored Markdown tables; turn off when the consumer wants pure prose
const job = await platform.media.transforms.docToMarkdown('uploads/docs/handbook.docx');
// Once complete, the .md file is at `/api/v/${job.output_key}` — feed it to your LLM.
docToHtml(srcKey, opts?)
Convert a document into a single self-contained HTML file with images base64-inlined and styles inlined. The output has no sidecar assets — perfect for email rendering, iframe embedding, or sending the whole document as one string.
Parameters:
srcKey(string) — storage key of the source documentopts(object, optional):inline_images(boolean, default:true) — embed<img>references as base64data:URIsinline_styles(boolean, default:true) — inline CSS as<style>blocks
const job = await platform.media.transforms.docToHtml('uploads/newsletters/march.docx');
// Once complete, the HTML is fully self-contained — embed in <iframe> or send via email.
docReplaceImages(srcKey, opts)
Image-only template fill. For mixed renders (text + images + QR), use docTemplateMerge — it does this job in one pass instead of two.
Fill a .docx / .odt / .pptx template with caller-supplied images. Two strategies, both supported in one call:
- Placeholder text-tags (
{{LOGO}}) — find the literal text in the document and swap in an image at that position. - Named drawing objects — match by the object’s
Nameproperty (set in Word/Writer via Format → Anchor → Properties → Name) and swap the image while preserving the template’s exact frame size, border, anchor, and text-wrap.
Use placeholders when the user types a tag in their template. Use named objects when the template already has a positioned dummy image you want to “fill” — frame styling stays.
Parameters:
srcKey(string) — storage key of the template documentopts(object):placeholders(object, optional) —{ '{{TAG}}': { src_key: 'images/logo.png' }, ... }named_objects(object, optional) —{ 'CompanyLogo': { src_key: 'images/logo.png' }, ... }output_format('pdf' | 'docx' | 'odt' | 'xlsx' | 'html' | 'txt' | 'rtf', optional) — output format; defaults to keeping the input formatauto_resize(boolean, default:true) — pre-fit each replacement image to the target frame’s dimensions to keep the output size sane
const job = await platform.media.transforms.docReplaceImages('templates/welcome.docx', {
placeholders: {
'{{USER_PROFILE_IMAGE}}': { src_key: 'avatars/user-42.png' },
},
named_objects: {
'CompanyLogo': { src_key: 'brand/logo-2026.png' },
'HeaderBanner': { src_key: 'campaigns/spring.jpg' },
},
output_format: 'pdf',
});
docInsertQrCode(srcKey, opts)
QR-only template fill. For mixed renders (text + images + QR), use docTemplateMerge — it does this job in one pass.
Generate QR codes server-side and inject them into a document — typically a backlink to the document’s own page in your app, a payment URL, or a tracking code on an invoice. Each code can target a placeholder text-tag or a named object, same as docReplaceImages.
Parameters:
srcKey(string) — storage key of the template documentopts(object):codes(array) — one entry per QR to embed:placeholder(string) ORnamed_object(string) — exactly onepayload(string) — the URL or string to encode (≤ 1500 bytes)size(number, default:256) — output PNG width in pixels
output_format('pdf' | 'docx' | 'odt' | 'xlsx' | 'html' | 'txt' | 'rtf', optional) — output format; defaults to keeping the input format
const job = await platform.media.transforms.docInsertQrCode('templates/invoice.docx', {
codes: [
{
placeholder: '{{QR_BACKLINK}}',
payload: `https://app.example.com/invoice/${invoiceId}`,
size: 256,
},
{
named_object: 'PaymentQR',
payload: 'upi://pay?pa=acct@bank&am=42.00',
size: 384,
},
],
output_format: 'pdf',
});
pdfStamp(srcKey, opts)
Place images and lines of text on an existing PDF — a signature, a seal, a “Draft” label on every page, a footer. The result is a new PDF; the original is untouched and nothing is rasterized, so text stays selectable.
Parameters:
srcKey(string) — storage key of the PDFopts.marks(array, required, 1 to 200 entries) — each mark has:page(number |'last'|'all') — 1-based page number, the last page, or every pagex,y(number | percentage string) — the top-left corner of the mark’s box. The origin is the top-left of the page as displayed,ygrows downward, and a number is in PDF points (1/72 inch). A string such as'12.5%'is a percentage of the page width (x) or height (y). Rotated pages use the displayed orientationkind: 'image'—key(storage key of a PNG, JPEG or WebP; transparency is kept),width(required),height(optional; omit to keep the aspect ratio)kind: 'text'—text(up to 1000 characters,\nstarts a new line),size(points,4..=72, default10),color('#rrggbb', default'#000000'),bold(defaultfalse)
Text is set in Helvetica. Latin letters with accents, digits and common punctuation are supported; any other character is written as ? rather than failing the job, so a signer’s name never blocks a whole document.
Returns: JobHandle — { id, src_key, output_key, status }.
const job = await platform.media.transforms.pdfStamp('contracts/nda.pdf', {
marks: [
{ page: 'last', x: 72, y: '82%', kind: 'image', key: 'signatures/jana.png', width: 140 },
{ page: 'last', x: 72, y: '90%', kind: 'text', text: 'Jana Müller\n12 Nov 2026', size: 9 },
{ page: 'all', x: 36, y: 20, kind: 'text', text: 'Confidential', color: '#888888' },
],
});
pdfMerge(srcKey, opts)
Join PDFs in order and, if you like, close the result with one generated audit page — a title and a list of label/value rows, the closing page of a signing record.
Parameters:
srcKey(string) — storage key of the first documentopts(object) — giveappend,audit, or both:append(string[]) — storage keys of the PDFs that follow, in order. WithsrcKey, at most 50 sourcesaudit(object, optional) —title,subtitle(optional),rows(up to 200 of{ label, value }; long values wrap),footer(optional text),footer_link(optional{ text, url }that makestext, which must appear infooter, a clickable link) andfooter_logo(optional,'maravilla')
Returns: JobHandle.
const job = await platform.media.transforms.pdfMerge('contracts/nda-signed.pdf', {
append: ['contracts/annex-a.pdf'],
audit: {
title: 'Signing record',
subtitle: 'NDA — Example Ltd.',
rows: [
{ label: 'Signer', value: 'Jana Müller' },
{ label: 'Signed', value: '2026-11-12 09:41 UTC' },
],
},
});
Both transforms read every extra key (signature images, appended PDFs) through the same access rules as the source, are cached by their options like every other transform. A malformed or oversized PDF fails its own job with a clear error. Neither is available in the declarative config block.
Browser Capture
Capture any public webpage — or any route from your own deployment — as a PNG, JPEG, WebP, or PDF. Two modes share the same surface:
urlmode captures any public webpage. Internal addresses are blocked so a tenant script can’t reach private services.pathmode captures a route on your own deployment. Your auth, SSR, and dynamic data work exactly as in a normal browser visit — your page is rendered the same way a user would see it.
path mode is the recommended way to render content you already build in your app: OG images, dynamic social cards, email previews, dashboard exports. Everything you already render in production becomes capturable without duplicating templates.
BROWSER.screenshot(opts)
Capture a webpage as an image.
Parameters:
opts(object) — provide exactly one ofurlorpath:url(string) — a fully-qualified public URL (https://example.com/foo)path(string) — a path on the tenant’s own deployment (/og/posts/hello)viewport(object, optional) —{ width, height }in CSS pixels; defaults to 1280×800wait_for(object, optional) — wait for one of{ selector: 'main' },{ network_idle: true }, or{ ms: 500 }before snappingfull_page(boolean, default:false) — capture the entire scroll height, not just the viewportformat('png' | 'jpeg' | 'webp', default:'png') — output format (webp falls back to png on builds where Chromium lacks webp support)quality(number, optional) — 1–100 quality hint for jpeg / webpcookies(array, optional) —[{ name, value, domain? }]set before navigationheaders(object, optional) — extra request headerstarget(string) — storage key the image is written to (required)
Returns: JobHandle — { id, src_key, output_key, status }. Wait for it by listening for transform.complete on REN, not by polling job(id).
// Screenshot a public URL
const shot = await platform.env.BROWSER.screenshot({
url: 'https://example.com',
viewport: { width: 1440, height: 900 },
wait_for: { network_idle: true },
format: 'png',
target: 'shots/example.png',
});
// Render an OG image from your own deployment
const og = await platform.env.BROWSER.screenshot({
path: `/og/posts/${slug}`,
viewport: { width: 1200, height: 630 },
format: 'png',
target: `og/${slug}.png`,
});
// Serve at /api/v/og/${slug}.png once transform.complete fires.
BROWSER.pdf(opts)
Render a webpage as a PDF. Same url / path modes as screenshot.
Parameters:
opts(object) — provide exactly one ofurlorpath:url(string) orpath(string) — same asscreenshotformat('A4' | 'A3' | 'Letter' | 'Legal' | 'Tabloid', optional) — page size; defaults toA4landscape(boolean, default:false)print_background(boolean, default:true) — include CSS backgrounds and imagesmargin(object, optional) — page margins in inches:{ top, right, bottom, left }(each optional). Omit a side for Chromium’s default (~0.4in); set all to0when the document supplies its own margins.prefer_css_page_size(boolean, optional) — use the document’s CSS@pagesize instead offormat.wait_for(object, optional) — wait before printing:{ selector },{ network_idle: true }, or{ ms }. Use a selector to wait for client-side rendering (charts, a paged.js layout) to finish.display_header_footer(boolean, optional) — render a repeating header/footer on every page.header_template/footer_template(string, optional) — HTML for the header/footer (inline styles only; set an explicitfont-size). The spans.pageNumber,.totalPages,.title,.date,.urlare filled in by Chromium — this is how you get “Page n of n”.target(string) — storage key the PDF is written to (required)
// Invoice with a repeating footer and page numbers
const invoice = await platform.env.BROWSER.pdf({
path: `/invoices/${invoiceId}/print`,
format: 'A4',
print_background: true,
display_header_footer: true,
header_template: '<span></span>',
footer_template:
'<div style="font-size:8px;width:100%;text-align:right;padding:0 18mm;color:#94a3b8;font-family:sans-serif;">Page <span class="pageNumber"></span> of <span class="totalPages"></span></div>',
margin: { top: 0.55, bottom: 0.5, left: 0, right: 0 },
target: `invoices/${invoiceId}.pdf`,
});
Multi-page layouts (paged.js). For richer paged media — running headers/footers,
a table of contents, CSS @page margin boxes — render the document with
paged.js client-side (in the page you point path at)
and print it from the browser, or capture it server-side here. When capturing
server-side, run paged.js to completion first (signal a selector and pass it via
wait_for), then print with prefer_css_page_size: true and margin: { top: 0, right: 0, bottom: 0, left: 0 } so paged.js’s pages are emitted 1:1. For a simple
repeating header/footer + page numbers, the native display_header_footer +
*_template options above are simpler and need no extra dependency.
Frames Render
Produce a deterministic MP4 (or WebM) from a route on your deployment. Same input → same bytes out, every time — render twice and the files are byte-identical.
Use it for animated title cards, narrated explainers, social-share teasers, demo loops — anything you’d normally need a video-editing pipeline for. Because the composition is a route in your own app, everything you already have works: SSR, auth, dynamic data, designed components, your CI tests.
FRAMES.render(opts)
Parameters:
opts(object):path(string) — relative path on the tenant’s deployment (required)width,height(number) — frame dimensions in pixels (required)fps(number) — frames per second;1–120duration_ms(number) — total clip duration in millisecondscodec('h264' | 'vp9', default:'h264') — video codec (vp9produces WebM with Opus audio;h264produces MP4 with AAC)crf(number, optional) — constant rate factor; lower = higher quality. Defaults:23for h264,31for vp9audio(string, optional) — storage key of a single pre-mixed audio file to mix in; the output runs as long as the shorter of the video or the audio. For multiple, individually-placed tracks (voiceover + music + sfx, with automatic ducking), declare them on the timeline instead — see Audio in the timelinetarget(string) — storage key the MP4/WebM is written to (required)
// Render the route at /render/title-card as a 4-second 1080p30 MP4 with narration
const job = await platform.env.FRAMES.render({
path: '/render/title-card?headline=Hello',
width: 1920,
height: 1080,
fps: 30,
duration_ms: 4000,
codec: 'h264',
crf: 20,
audio: 'audio/narration.mp3',
target: 'reels/title-card.mp4',
});
// Serve at /api/v/reels/title-card.mp4 once transform.complete fires.
When a frames render runs locally on macOS (maravilla dev), each frame is captured as a JPEG at quality 95 rather than a lossless PNG, which keeps per-frame capture fast.
The @maravilla-labs/frames Helper
A tiny vanilla-JS helper (no runtime dependencies) you import on the page being rendered. Declare a TimelineSchema and the renderer drives the animation deterministically by stepping the Web Animations API frame by frame. In a regular browser visit, the timeline auto-plays — your render route stays directly testable as a normal webpage.
Each entry has an at (when on the timeline it starts, in ms) and an optional duration (how long it runs). Entries with different at values are staggered; entries with the same at start together. Use kind: 'video' to schedule a <video> element instead of an animation, or kind: 'css' to drive a CSS-class-based animation from a library like Animate.css.
// src/routes/render/title-card/+page.svelte (or any framework's equivalent)
import { defineTimeline } from '@maravilla-labs/frames';
import { onMount } from 'svelte';
onMount(() => {
defineTimeline({
duration: 6000,
instructions: [
// Title flies in over the first 500 ms.
{
at: 0,
duration: 500,
selector: 'h1',
props: [
{ opacity: 0, transform: 'translateY(20px)' },
{ opacity: 1, transform: 'translateY(0)' },
],
},
// Subtitle fades in 300 ms later.
{
at: 300,
duration: 600,
selector: '.subtitle',
props: [{ opacity: 0 }, { opacity: 1 }],
},
// An Animate.css class bounces a "Premiere" pill in at t=1.4 s.
// The helper captures the CSS animation as a WAAPI Animation and
// drives it frame by frame for deterministic rendering.
{
kind: 'css',
at: 1400,
duration: 1000,
selector: '.badge',
classes: ['animate__animated', 'animate__bounceInDown'],
},
// A clip plays from t=2 s to t=6 s — 4 seconds of video.
{
kind: 'video',
at: 2000,
duration: 4000,
selector: 'video.intro',
},
],
});
});
The CSS-class instruction works with any library that ships its motion as CSS @keyframes triggered by a class name — Animate.css, Magic, your own custom keyframes. Make sure the stylesheet that defines the keyframes is loaded before defineTimeline runs (the simplest pattern is await new Promise(r => addEventListener('load', r, { once: true })) at the top of a <script type="module">).
Using Animate.css
A complete minimal example — load the stylesheet, pre-hide the element, declare the instruction:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
<style>
.badge {
visibility: hidden; /* pre-hide until the timeline fires */
}
</style>
<span class="badge">Premiere</span>
<script type="module">
import { defineTimeline } from '@maravilla-labs/frames';
// Wait for the stylesheet to finish loading before the helper runs
await new Promise(r => addEventListener('load', r, { once: true }));
defineTimeline({
duration: 3000,
instructions: [
{
kind: 'css',
at: 600,
duration: 1000,
selector: '.badge',
classes: ['animate__animated', 'animate__bounceInDown'],
},
],
});
</script>
The helper flips visibility: visible automatically when the instruction fires, then drives the animation through to completion.
The pre-hide gotcha
Always pre-hide with visibility: hidden, never with opacity: 0. Animate.css’s “In” keyframes (bounceInDown, fadeInUp, zoomInLeft, all of them) define opacity at the start and middle of the animation, but not at 100%. CSS fills any missing keyframe value with the element’s underlying baseline — so an opacity: 0 baseline silently gets pulled in as the animation’s 100% value, and the element fades right back out as soon as it lands. The result: you see the bounce, then the element vanishes. Use visibility: hidden instead — it isn’t touched by any Animate.css keyframe, so it stays cleanly out of the way until the helper flips it on.
If the same animation also needs prefers-reduced-motion to be respected (Animate.css collapses to 1 ms by default under that media query), omit duration on the instruction. If you want the timeline to run regardless — typical for video-render compositions — pass a duration and the helper writes it inline with !important so the media query can’t shrink it.
The same declaration drives both your live page (each item plays at its scheduled time) and the renderer (it steps through the timeline frame by frame), so the rendered video matches exactly what users see in a browser. Bring your own animation library if you want — GSAP, Lottie, Three.js all work alongside the helper. Vanilla CSS or the Web Animations API is enough if you don’t.
Keeping animations smooth
The renderer captures frames one at a time so it doesn’t care about runtime performance — every frame gets as long as it needs. But while you’re designing the composition in a real browser (and especially while previewing inside a CSS-scaled container like a preview iframe, a popup, or a thumbnail), animations can stutter even though the same page runs smoothly when opened on its own. Two quick rules avoid it:
Tell the browser what’s about to change. Add will-change to any element your timeline animates. The browser promotes that element to its own GPU compositor layer; subsequent frames apply opacity / transform / filter changes as cheap GPU operations instead of re-rasterizing the parent every frame.
.title,
.tagline,
.meta {
will-change: opacity, transform, filter;
transform: translateZ(0); /* nudge older Safari onto a GPU layer */
}
<video> elements get the same treatment when they sit inside a scaled or transformed container:
.composition video {
will-change: transform;
}
Animate the cheap properties. transform, opacity, and filter composite on the GPU. width, height, top, left, margin, padding trigger layout — every animated frame recomputes the entire page flow. Slide an element with transform: translateX(...) rather than left: or margin-left:. Scale with transform: scale(...) rather than changing width/height.
Filter effects like blur(), drop-shadow(), and backdrop-filter are particularly worth promoting — without will-change the blur kernel runs over the full parent surface every frame.
To check whether a given element is on its own GPU layer, open Chrome DevTools → Rendering → enable “Layer borders”. Yellow borders mark elements that the browser is compositing on the GPU. No border means the browser is still drawing them in pixels every frame.
Audio in the timeline
Beyond visuals, the helper schedules time-bound audio tracks right in the same timeline — a voiceover, a background-music bed, and one-shot sound effects, each placed at its own start time. The tracks are mixed into the exported video and played back in the browser preview by a built-in Web Audio engine, so what you hear while designing matches the final file.
import { defineTimeline } from '@maravilla-labs/frames';
defineTimeline({
duration: 12000,
instructions: [
// ...your visual instructions...
// A music bed under the whole piece, fading in and out.
{
kind: 'audio',
at: 0,
duration: 12000,
role: 'music',
src: '/audio/bed.m4a', // URL the browser preview fetches + decodes
key: 'audio/bed.m4a', // storage key the renderer mixes into the export
fadeIn: 400,
fadeOut: 800,
},
// A voiceover starting at 3 s — the music bed automatically ducks beneath it.
{
kind: 'audio',
at: 3000,
role: 'voiceover',
src: '/audio/vo.m4a',
key: 'audio/vo.m4a',
},
],
});
Audio fields:
at/duration/seek(number, ms) — placement on the timeline and the window into the source file, exactly like akind: 'video'instructionsrc(string) — a URL the browser preview fetches and decodeskey(string, optional) — the storage key the renderer mixes into the exported video; omit for preview-only tracksrole('voiceover' | 'music' | 'sfx', default'music')gain(number, default1) — linear volume multiplierfadeIn/fadeOut(number, ms) — fade durationsloop(number, ms) — repeat the section[seek, seek + loop)for the wholeduration; see Loops and freeze framesid(string, optional) — names the track so you can change its voice while the preview playsdenoise/effect— voice processing; see Voice processingduck(boolean) — whether a music track dips under voiceover; defaults totrueforrole: 'music'
Ducking lowers the music under the voice automatically. The dip is a fixed envelope computed from where the voiceover sits on the timeline, not from its loudness — so it’s deterministic, and the preview reproduces the exact same dip the export bakes in.
During continuous preview playback the tracks are audible; scrubbing the playhead stays silent, the way a video editor behaves. The detached audio you get from extractAudio — or from extractFrames’s detach_audio — drops straight in as a track’s key.
Voice processing
Two independent steps for a spoken track, applied in this order before fades, gain and ducking:
{ kind: 'audio', id: 'vo-1', at: 0, role: 'voiceover', src: '/audio/vo.webm', key: 'audio/vo.webm',
denoise: { amount: 0.9 }, // or `true` (= 1)
effect: { kind: 'chain', stages: [
{ type: 'level', target: -20 },
{ type: 'highpass', freq: 300 },
{ type: 'compressor', threshold: -20, ratio: 4, attack: 5, release: 120, makeup: 9 },
{ type: 'echo', inGain: 0.8, outGain: 0.9, taps: [{ delay: 50, decay: 0.3 }, { delay: 100, decay: 0.15 }] },
{ type: 'limit', ceiling: -1 },
] } }
denoiseremoves background noise at speech level, so a quiet voice is not mistaken for noise, and returns the take to its own loudness.amount(0…1) blends the original back in.effectis an ordered list of stages, so a studio voice, a stadium PA and a deep announcer are just different lists. Stages:level(bring speech totargetdBFS),gain,highpass,lowpass,peak,lowshelf,highshelf,compressor,drive,echo(up to three reflections),pitch(semitones) andlimit. Units are Hz, dB, ms and semitones; every parameter has a default, and an unknown stage type is skipped.
Preview and export use the same processing, so what you hear while editing is what the exported video contains. Change a track’s voice while the preview plays with __mvFrames.setAudioVoice(id, { denoise, effect }), compare with the original using __mvFrames.setAudioBypass(true), and call prepareAudio() to process ahead of the first play.
Loops and freeze frames
A video or audio instruction can repeat a section of its file with loop, a length in ms counted from seek. The section repeats for the whole duration: at timeline time t the clip is at seek + ((t − at) mod loop), so scrubbing, live playback and the export land on the same frame of the same repeat. Audio loops are sample-exact. A loop shorter than a few milliseconds plays once, and a loop longer than what the file has after seek repeats what there is.
// Repeat the 2 seconds of a clip that start 5 s in, for 10 s of the timeline.
{ kind: 'video', at: 0, duration: 10000, seek: 5000, loop: 2000, selector: 'video#hero' }
// Hold one frame — the frame at `seek` — for 3 s.
{ kind: 'video', at: 4000, duration: 3000, seek: 7500, freeze: true, selector: 'video#hero' }
freeze: true holds the frame at seek for the whole duration. Starting live playback no longer re-seeks a video that is already on its frame.
job(id)
Look up the current status of a job by its id. Returns 'pending' | 'running' | 'complete' | 'failed'.
const status = await platform.media.transforms.job(job.id);
This is for a one-off check, not for waiting. A loop that calls it every few hundred milliseconds costs a request per tick per client and is still late by up to one interval — while the worker is already announcing transform.queued, transform.started, transform.progress, transform.complete and transform.failed on REN. Listen for the event that names your job:
import { RenClient } from '@maravilla-labs/platform/ren';
const ren = new RenClient({ subscriptions: ['transforms'] }); // one per app
const outputKey = await new Promise((resolve, reject) => {
const off = ren.on((e) => {
if (e.data?.jobId !== job.id) return;
if (e.t === 'transform.complete') { off(); resolve(e.data.outputKey); }
if (e.t === 'transform.failed') { off(); reject(new Error('transform failed')); }
// `transform.progress` carries { percent, stage } — drive a progress bar from it.
});
});
Subscribe before you start the job: a fast transform can finish before a listener attached afterwards exists. Reserve job(id) for a CLI or other request/response caller that has nowhere to receive a push.
Derived Keys
Every transform output lands at a deterministic storage key derived from the source key and the options:
__derived/<srcHash>/<variantHash>.<ext>
Two users requesting the same transform of the same file hit the same output — the expensive work happens once. Because the key is a pure function of the inputs, you can compute it on the client before the job finishes:
import { transforms } from '@maravilla-labs/platform';
const posterKey = transforms.keyFor('uploads/videos/clip.webm', {
kind: 'thumbnail',
at: '1s',
width: 640,
format: 'jpg',
});
// → '__derived/<srcHash>/<variantHash>.jpg' (each hash is 16 hex characters)
// Render the <video> element immediately with the final URL — the browser
// will 404 until the job completes, then hydrate on transform.complete.
Lifecycle Events
Every transform job emits five events through Real-Time Events. Subscribe in the browser to drive loading states, progress bars, or poster swaps without a page reload.
| Event | Fires when |
|---|---|
transform.queued | Job has been accepted; output_key is already known |
transform.started | A worker picked up the job |
transform.progress | Periodic update with { percent, stage } for long-running work |
transform.complete | Output is written to storage |
transform.failed | Terminal failure after retries are exhausted |
import { RenClient } from '@maravilla-labs/platform/ren';
const ren = new RenClient({ subscriptions: ['transforms'] });
ren.on((event) => {
if (event.t === 'transform.complete' && event.data?.outputKey === expectedPosterKey) {
posterEl.src = `/api/v/${event.data.outputKey}?v=${Date.now()}`;
}
});
Every event’s data carries jobId, srcKey, outputKey and attempts; transform.failed adds error and retries.
From RaisinDB Functions
Server functions running inside RaisinDB reach the same transform suite through raisin.media.* (video.transcode, video.thumbnail, video.extractFrames, video.extractAudio, video.watermark, image.resize, image.watermark, image.detectFaces, image.ocr, doc.*, browser.*, video.render). Arguments are camelCase (logoKey, maxWidth, durationMs) and every call is submit-only: it returns { jobId, outputKey, status } immediately, and you track completion through the job node or the events above.
Submits are idempotent, and retries are bounded. The same request (same source key and options) while the previous job is pending, running or complete returns that job with deduplicated: true instead of enqueuing again, so a function that is retried — or a trigger that fires on every write — cannot pile up duplicate conversions. After a failure the same request is refused with { ok: false, reason: 'backoff', retryAfterSecs } until an exponential backoff (60 s, then ×4) has passed, then it is submitted once more. After three failed runs the result is terminal: { ok: false, reason: 'failed', terminal: true, detail } with the last job error. Record it and stop; the entry is forgotten after 24 hours.
Converted documents can become searchable text. raisin.media.doc.toMarkdown normally leaves a markdown file in storage. To make the source document findable in RaisinDB search, create the tracking job node with target_kind: 'extracted_text' and a target_node / target_workspace: when the conversion completes, the markdown is written back as that node’s extracted text, and RaisinDB chunks, embeds and indexes it exactly as it does for a PDF it read itself. The default, target_kind: 'resource', attaches the output as a file instead. An empty conversion is reported as empty_conversion rather than marked done.
Declarative Config
Declare transforms in maravilla.config.ts and Maravilla runs them every time a matching file is uploaded. No handler code to write.
// maravilla.config.ts
import { defineConfig } from '@maravilla-labs/platform';
export default defineConfig({
transforms: {
'uploads/videos/**': {
transcode: [{ format: 'mp4' }, { format: 'webm' }],
thumbnail: { at: '1s', width: 640, format: 'jpg' },
},
'uploads/photos/**': {
variants: [
{ width: 1600, format: 'webp', quality: 85 },
{ width: 400, format: 'webp', quality: 80 },
],
},
'uploads/avatars/**': {
detectFaces: true,
variants: [
{ width: 256, height: 256, format: 'webp', crop: 'focal' },
{ width: 1024, format: 'webp' },
],
},
'uploads/scans/**': {
ocr: { lang: 'eng' },
},
},
});
The variants shorthand is sugar for multiple resize calls — use it when you want several sizes of the same image (thumbnail + hero + full).
In the uploads/avatars/** pattern above, face detection runs first and the focal crop waits for it automatically, so the 256×256 square is always cut around the face rather than the geometric center. detectFaces accepts true for detector defaults, or an object to tune it: { min_score, max_faces, fast }.
Reacting to Uploads with onStorage
When you need custom logic on upload — conditional branching, enqueuing a workflow, posting to an external API — drop the declarative config and write an Event Handler instead:
// events/on-video-upload.ts
import { onStorage } from '@maravilla-labs/platform/events';
export const processVideo = onStorage(
{ keyPattern: 'uploads/videos/*', op: 'put' },
async ({ key, platform }) => {
const meta = await platform.media.transforms.probe(key);
if (meta.duration_secs && meta.duration_secs > 15) {
await platform.kv.invites.put(key, { rejected: 'too_long' });
return;
}
await Promise.all([
platform.media.transforms.transcode(key, { format: 'mp4' }),
platform.media.transforms.thumbnail(key, { at: '1s', width: 640, format: 'jpg' }),
]);
},
);
The same pattern works for documents — every uploaded contract auto-renders a PDF preview and a thumbnail:
// events/on-contract-upload.ts
import { onStorage } from '@maravilla-labs/platform/events';
export const processContract = onStorage(
{ keyPattern: 'uploads/contracts/*.docx', op: 'put' },
async ({ key, platform }) => {
await Promise.all([
platform.media.transforms.docToPdf(key),
platform.media.transforms.docThumbnail(key, { page: 1, width: 480, format: 'webp' }),
]);
},
);
Next Steps
- Storage — where source and derived files live
- Event Handlers — the full
onStoragetrigger surface - Real-Time Events — subscribe to
transform.*events in the browser - Platform Services Overview — how the pieces fit together