createSplice
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
| Option | Type | Default | Description |
|---|---|---|---|
source | Blob | string | URL | HTMLVideoElement | - | What to read. A <video> is read through its currentSrc |
start | number | 0 | Start of the clip on the source's timeline, in seconds |
end | number | the end of the source | End of the clip, in seconds |
crop | CropOptions | no crop | See crop |
captions | cues, WebVTT text or a URL | - | Burned into the picture. See captions |
captionPosition | 'top' | 'middle' | 'bottom' | 'bottom' | Where the captions sit. See captions |
captionStyle | CaptionStyle | the boxed look | The captions' font, colour, size and box. See captions |
origin | SpliceOrigin | - | Written into the metadata. See origin |
endCard | EndCardOptions | - | A card after the clip. See end card |
stamp | StampOptions | - | A logo in a corner of every frame |
watermark | WatermarkOptions | - | A band of text across every frame |
audio | boolean | true | false 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) => void | console.warn | Something left out of a clip that was still made |
signal | AbortSignal | - | 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 with | Why |
|---|---|
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' }.