Darkgrade Slate's VideoTake records one take on several cameras: it prepares and starts them together, stops them, and copies every clip off the cards. Its one state object is ready to show in a UI and save to disk.
For one camera, startRecording() and stopRecording() are all you need. Use VideoTake when a take has to succeed or fail as a whole.
import { VideoTake } from '@darkgrade/slate'
const take = new VideoTake('take-1', 'Interview, wide and close', cameras)
await take.start() // prepares, then starts every camera
// … the scene plays …
await take.stop() // stops every camera and finds its clip
await take.collect() // copies the clips off the cards
console.log(take.state.stage) // 'complete'cameras is a Map from a name you choose to a TakeCamera: a small adapter you write over each camera.
Write the adapter
VideoTake never talks to a camera itself. It calls five methods on each TakeCamera, so a take can mix Slate cameras with anything else that records.
| Method | Must do |
|---|---|
prepare() | Get the camera ready to record. Throw if it can't. Called once, before the start. |
start() | Start recording. Resolve to { recordingConfirmed, evidence }: whether the camera confirmed it, and how you know. Throw if it didn't start. Called once. |
stop() | Stop recording. Resolve to { stopped, media, warning? } with the clips it wrote, each a TakeMedia. stopped: false or a throw marks the camera uncertain. Called when the take or this camera stops. |
collect(media, progress) | Reopen the camera, copy the files and resolve to the same list with verified: true and a downloadUrl on each. Call progress(bytes, total) as you go. Called after every camera stopped and was closed. |
close() | Release the camera. Called whenever the camera is not recording: after it stops, and again after its clips are collected. Make a second call harmless. |
This adapter wraps a Slate Camera and saves clips to a folder:
import { writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import {
Camera,
VideoTake,
type TakeCamera,
type TakeMedia,
} from '@darkgrade/slate'
function takeCamera(camera: Camera, folder: string): TakeCamera {
let open = false
const connect = async () => {
if (open) return
await camera.connect()
open = true
}
return {
prepare: connect,
async start() {
// Resolves once the camera reports recording.
await camera.startRecording()
return {
recordingConfirmed: true,
evidence: 'camera reported recording',
}
},
async stop() {
const clip = await camera.stopRecording()
const file = {
id: clip.mediaId,
filename: clip.name,
bytes: clip.sizeBytes ?? 0,
verified: false,
}
return { stopped: true, media: [file] }
},
async collect(media, progress) {
await connect() // the take closed this camera after it stopped
const saved: TakeMedia[] = []
for (const file of media) {
const data = await camera.downloadMedia(file.id, {
onProgress: progress,
})
const path = join(folder, file.filename)
await writeFile(path, data)
const verified = data.byteLength === file.bytes
saved.push({ ...file, downloadUrl: path, verified })
}
return saved
},
async close() {
if (!open) return
open = false
await camera.disconnect()
},
}
}
const cameras = new Map([
['wide', takeCamera(new Camera(), './footage')],
// Add one entry per camera.
])
const take = new VideoTake(crypto.randomUUID(), 'Interview', cameras)collect() accepts a camera's files only if every one comes back with the same id, filename and bytes, a bytes above zero, verified: true and a downloadUrl. Anything else leaves that camera with an error: Transferred footage did not match the camera files. A clip over 4 GiB has no sizeBytes on Clip, so an adapter for long takes must read the size another way.
To open each of several cameras by serial number, see Choose a camera.
Constructor
new VideoTake(
id: string,
name: string,
devices: Map<string, TakeCamera>,
maximumMinutes?: number, // default 30
publish?: (state: VideoTakeState) => void
)| Argument | Description |
|---|---|
id | Your identifier for the take. Must not be empty. |
name | A label. Trimmed to 100 characters; 'Untitled take' when empty. |
devices | 1 to 64 cameras, by the name each has in state.cameras. |
maximumMinutes | The take's safety stop, 1 to 120. VideoTake records it in state.maximumMinutes; it runs no timer, so your code calls stop() when it elapses. |
publish | Called with a copy of the state, a VideoTakeState, after every change. Use it to redraw a UI or save the take. |
The constructor throws Choose 1–64 cameras and a safety stop of 1–120 minutes when an argument is out of range.
The life of a take
- 1
Prepare
prepare()readies every camera at once. When all are ready the take isarmed. If one fails, the others are released and the take needsattention.start()calls it for you when you haven't. - 2
Start
start()starts every camera at once. When every adapter'sstart()resolves, the take isrecording,state.startedAtis the earliest start andstate.startSkewMillisecondsis the gap between the first and last. If anystart()throws, every camera is stopped and the take needsattention.A camera whose adapter resolved with
recordingConfirmed: falsestill counts as recording: the take records what you reported and does not judge it. - 3
Stop
stop()stops every camera that started.stopCamera(name)stops one and leaves the rest recording. When every camera has confirmed its stop, the take is inreview. - 4
Collect
collect()copies each camera's clips, one camera at a time, and closes each camera as it finishes. When every camera iscollected, the take iscomplete.
VideoTake starts the cameras together, but it does not synchronise their frames. startSkewMilliseconds tells you how far apart the starts were confirmed; align the clips in your editor.
Take stages
state.stage is one of:
| Stage | Meaning |
|---|---|
preparing | Created; cameras are not all ready yet. |
armed | Every camera is ready. Nothing is recording. |
recording | At least one camera is recording. |
stopping | A stop is in progress. |
review | Every camera has stopped. Ready to collect. |
collecting | Clips are being copied. |
complete | Every camera's clips are copied and verified. |
attention | Something needs a person: a camera failed to prepare or start, a stop was not confirmed, or a transfer failed. Read each camera's error. |
Camera states
Each entry of state.cameras is a TakeCameraState with its own state:
| State | Meaning |
|---|---|
preparing, ready | Before the start. |
recording | Started. recordingConfirmed and evidence are what your adapter returned; startedAt is when. |
stopping | A stop was requested (stopRequestedAt). |
stopped | The camera confirmed the stop; media lists its clips and stoppedAt is when they were found. Also a camera released before it started, with a warning saying so. |
collected | Its clips are copied and verified. |
error | It failed to prepare. |
uncertain | A start or stop was sent and not confirmed. The camera may still be recording. |
During a transfer, transferredBytes and totalBytes track progress, and transferRecovery holds a message while a retry is under way.
When something goes wrong
- A stop is not confirmed. The camera becomes
uncertainand the take needsattention. Callstop()again: it retries only the cameras still unstopped.hasUnstoppedCameras()says whether any remain.collect()refuses to run until none do. - A transfer fails.
collect()retries a camera once, and only when the error your adapter threw hasretryable: true. Any other failure is kept as that camera'serror, the other cameras still collect, and the take ends inattention. Callcollect()again to retry the cameras that are not yetcollected. - A camera has no clips. It stays
stopped, so the take ends inattention, notcomplete. - You need to cancel while it prepares or starts.
requestStop()sets a flag thatprepare()andstart()honour: cameras that had not started are released, and any that had are stopped. - Two calls overlap. While another call is in progress,
stop()andstopCamera()throwA take operation is still running, andcollect()throwsConfirm all cameras stopped before collecting footage.
Resume after a restart
Save the state your publish callback receives. After a restart, rebuild the take to finish collecting:
import { readFile, writeFile } from 'node:fs/promises'
import { VideoTake, type VideoTakeState } from '@darkgrade/slate'
const save = (state: VideoTakeState) =>
writeFile('take.json', JSON.stringify(state))
const text = await readFile('take.json', 'utf8')
const saved: VideoTakeState = JSON.parse(text)
// Saved mid-transfer: nothing is copying now, so collect again.
if (saved.stage === 'collecting') saved.stage = 'review'
const take = VideoTake.resume(saved, cameras, save)
await take.collect()VideoTake.resume only resumes collection, and has three limits:
- It throws
Confirm all recording stopped before resuming collectionif any camera was saved aspreparing,ready,recording,stoppingoruncertain. A restart can't tell whether such a camera is still recording, so check it yourself withgetRecordingState()first. - It throws
A camera from this take is unavailablewhen a camera from the saved take is missing fromdevices. - It keeps the saved
stage. A take saved while it wascollectingstayscollecting, andcollect()refuses that stage; set it back toreviewfirst, as above. Cameras alreadycollectedare skipped.