Making clips

createSplice

Cut a range out of a video and get back an MP4, with every option and what can go wrong.

createSplice(options) cuts [start, end) out of a source and resolves with a Blob: always an MP4 (video/mp4) with H.264 video and AAC audio.

const clip = await createSplice({
  source: video, // a URL, a File/Blob or a <video>
  start: 42,
  end: 52,
  crop: { aspect: '9:16' },
  captions: '/video/interview.en.vtt',
  origin: {
    url: location.href,
    title: document.title,
    publisher: 'Example News',
  },
  endCard: { logo: '/logo.svg' },
  onProgress: (fraction) => (progress.value = fraction),
  onWarning: (message) => console.warn(message),
  signal: controller.signal,
})

Options

OptionTypeDefaultDescription
sourceBlob | string | URL | HTMLVideoElement-What to read. A <video> is read through its currentSrc
startnumber0Start of the clip on the source's timeline, in seconds
endnumberthe end of the sourceEnd of the clip, in seconds
cropCropOptionsno cropSee crop
captionscues, WebVTT text or a URL-Burned into the picture. See captions
captionPosition'top' | 'middle' | 'bottom''bottom'Where the captions sit. See captions
captionStyleCaptionStylethe boxed lookThe captions' font, colour, size and box. See captions
originSpliceOrigin-Written into the metadata. See origin
endCardEndCardOptions-A card after the clip. See end card
stampStampOptions-A logo in a corner of every frame
watermarkWatermarkOptions-A band of text across every frame
audiobooleantruefalse drops the sound
onProgress(fraction: number) => void-From 0 to 1 as the clip is written; 1 only once the file is done
onWarning(message: string) => voidconsole.warnSomething left out of a clip that was still made
signalAbortSignal-Aborts the clip

How it works

Mediabunny reads the source, only the byte ranges it needs from a URL. WebCodecs decodes each frame, splice draws it onto one reused canvas (cropped, with captions and overlays on top) and an H.264 encoder takes the canvas with backpressure. The audio runs beside it into the same file: AAC is copied packet for packet, anything else is encoded as AAC. Every decoded frame is closed as soon as it's drawn.

From an HLS stream, only the smallest variant whose picture still covers the output is downloaded, with that variant's own audio.

Output size

Without a crop the output is the source's own frame; with one, the crop window. Either way it's at least the largest frame of its aspect that fits 1080x1920 (1920x1080 for landscape), so captions and cards are drawn at the size a phone shows them rather than upscaled. crop.height sets the size outright. Dimensions are always even, since H.264 works in 4:2:0.

Things left out

Some things are left out of a clip rather than failing it, each reported through onWarning:

  • The sound, when the source's audio isn't AAC and the browser has no AAC encoder (Firefox).
  • Captions that can't be fetched or aren't WebVTT.
  • A logo (the stamp's or the end card's) that can't be loaded, such as a cross-origin image without CORS headers.

When it rejects

createSplice rejects with a plain Error when no clip can be made:

Message starts withWhy
The source has no video track.An audio-only file
The video is encrypted (DRM) …A <video> playing encrypted media through EME
The video is a live MediaStream …A <video> showing a camera, a screen or a call
The video is fed by a MediaSource …A <video> played by hls.js, dash.js or Shaka: pass the stream's URL instead, which splice reads itself
This browser cannot decode the source's …A codec this browser has no decoder for, such as HEVC in Firefox; encrypted tracks
End (…) must be after start (…)An empty range after holding it inside the source
This browser has no H.264 encoder for …No H.264 encoder at the output's size
The video encoder stopped responding …The encoder went silent for 15 seconds, twice: in High profile, then again in Baseline

Anything else, such as a network or CORS failure fetching the file, comes through as Mediabunny or the browser reported it. An abort rejects with the signal's reason, even one that lands while the file is being finished.

Planning without a source

planOutput({ width, height, audioTrack, audio }) answers what createSplice would do for a given size and Mediabunny audio track: null without an H.264 encoder at that size, else { audio: 'copy' | 'encode' | 'none' | 'unavailable' }.

munsonlabs · MIT licensed · © 2026