Getting started
Install
Splice isn't on npm yet. In this repo it's the @munsonlabs/splice workspace.
npm install @munsonlabs/splice
It depends on mediabunny, and on @munsonlabs/sigil for the editor's icons. The core needs nothing else. The editor needs vue and @munsonlabs/video-player, peer dependencies it shares with the page, since the page's player is its preview. They're optional so a page that only uses the core isn't asked for them, as with another player.
npm install @munsonlabs/video-player vue # for the editor, /vue or /element
npm install hls.js # optional, the player's: HLS playback outside Safari
With the Vue editor, import both stylesheets once. The /element build puts its own stylesheet on the page, so it needs only the player's:
import '@munsonlabs/video-player/style'
import '@munsonlabs/splice/style'
HLS playback is the player's. Thumbnails and export never use it: splice reads the stream itself with Mediabunny.
Requirements
A secure context (HTTPS or localhost) in a browser with WebCodecs and OffscreenCanvas: current Chrome, Edge, Safari and Firefox.
A first clip
Ask first
canSplice opens the source and plans the output without encoding anything. It never throws, so use it to decide whether to offer clipping at all.
import { canSplice } from '@munsonlabs/splice'
const check = await canSplice('/video/interview.mp4') // a URL, a File/Blob or a <video>
if (!check.ok) console.info(check.message)
Make the clip
import { createSplice } from '@munsonlabs/splice'
const controller = new AbortController()
const clip = await createSplice({
source: '/video/interview.mp4',
start: 42,
end: 52,
crop: {
aspect: '9:16',
focus: { x: 0.4 },
},
onProgress: (fraction) => (progress.value = fraction),
signal: controller.signal,
})
// clip is a Blob: 'video/mp4', H.264 video with AAC audio
Link back to the moment
createClipLink is the origin's URL with #ml-t=start,end in its hash, the link video-player's deep links open at that moment. Pass origin to createSplice too, so the MP4's metadata carries it.
import { createClipLink } from '@munsonlabs/splice'
const origin = {
url: location.href,
title: document.title,
publisher: 'Example News',
}
// https://example.com/interview#ml-t=42,52
const link = createClipLink(origin, 42, 52)
Save or share it
Web Share takes the file and the link. Where files can't be shared (most desktop browsers), download instead.
const file = new File([clip], 'interview-42-52.mp4', { type: clip.type })
const data = { files: [file], url: link }
if (navigator.canShare?.(data)) {
await navigator.share(data)
} else {
const a = document.createElement('a')
a.href = URL.createObjectURL(file)
a.download = file.name
a.click()
}
Or skip all four and drop in the editor, which does this with a UI.
Load it late
The core pulls in Mediabunny. Import it when someone asks for a clip, not with the page:
button.addEventListener('click', async () => {
const { createSplice } = await import('@munsonlabs/splice')
// ...
})
The editor does this for you.