RepositoryJavaScriptGBUnited Kingdom
dmnsgn

dmnsgn/canvas-record

Record a video in the browser or directly on the File System from a canvas region (2D/WebGL/WebGPU) as MP4, WebM, MKV, MOV, GIF, PNG/JPG Sequence using WebCodecs and Wasm when available.

437stars
27forks
3issues
0views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: webcodecs

#1 Repository of the month

1
TINIX TRENDING·Topic: gif

#1 Repository of the month

1
TINIX TRENDING·Topic: capture

#1 Repository of the month

canvas-record

npm version stability-stable npm minzipped size dependencies types Conventional Commits styled with prettier linted with eslint license

Record a video in the browser or directly on the File System from a canvas region (2D/WebGL/WebGPU) as MP4, WebM, MKV, MOV, GIF, PNG/JPG Sequence using WebCodecs and Wasm when available.

paypal coinbase twitter bluesky

Installation

npm install canvas-record

Usage

import { Recorder, RecorderStatus, Encoders } from "canvas-record";
import createCanvasContext from "canvas-context";
import { AVC } from "media-codecs";

// Setup
const pixelRatio = devicePixelRatio;
const width = 512;
const height = 512;
const { context, canvas } = createCanvasContext("2d", {
  width: width * pixelRatio,
  height: height * pixelRatio,
  contextAttributes: { willReadFrequently: true },
});
Object.assign(canvas.style, { width: `${width}px`, height: `${height}px` });

const mainElement = document.querySelector("main");
mainElement.appendChild(canvas);

// Animation
let canvasRecorder;

function render() {
  const width = canvas.width;
  const height = canvas.height;

  const t = canvasRecorder.frame / canvasRecorder.frameTotal || Number.EPSILON;

  context.clearRect(0, 0, width, height);
  context.fillStyle = "red";
  context.fillRect(0, 0, t * width, height);
}

const tick = async () => {
  render();

  if (canvasRecorder.status !== RecorderStatus.Recording) return;
  await canvasRecorder.step();

  if (canvasRecorder.status !== RecorderStatus.Stopped) {
    requestAnimationFrame(() => tick());
  }
};

canvasRecorder = new Recorder(context, {
  name: "canvas-record-example",
  encoderOptions: {
    codec: AVC.getCodec({ profile: "Main", level: "5.2" }),
  },
});

// Start and encode frame 0
await canvasRecorder.start();

// Animate to encode the rest
tick(canvasRecorder);

API

Encoder comparison:

EncoderExtensionRequired Web APIWASMSpeed
WebCodecsmp4 / webm / mkv / movWebCodecs❌Fast
MP4Wasmmp4WebCodecs✅ (embed)Fast
H264MP4mp4✅ (embed)Medium
FFmpegmp4 / webmSharedArrayBuffer✅ (need binary path)Slow
GIFgifWebWorkers (wip)❌Fast
Framepng / jpgFile System Access❌Fast
MediaCapturemkv / webmMediaStream❌Realtime

Note:

  • WebCodecs encoderOptions allow different codecs to be used: VP8/VP9/AV1/HEVC. See media-codecs to get a codec string from human readable options and check which ones are supported in your browser with github.io/media-codecs.
  • WebCodecs 5-10x faster than H264MP4Encoder and 20x faster than FFmpeg (it needs to mux files after writing png to virtual FS)
  • FFmpeg (mp4 and webm) and WebCodecs (mp4) have a AVC maximum frame size of 9437184 pixels. That's fine until a bit more than 4K 16:9 @ 30fps. So if you need 4K Square or 8K exports, be patient with H264MP4Encoder (which probably also has the 4GB memory limit) or use Frame encoder and mux them manually with FFmpeg CLI (ffmpeg -framerate 30 -i "%05d.jpg" -b:v 60M -r 30 -profile:v baseline -pix_fmt yuv420p -movflags +faststart output.mp4)
  • MP4Wasm is embedded from mp4-wasm for ease of use (FFmpeg will require encoderOptions.corePath)

Roadmap:

  • add debug logging
  • use WebWorkers for gifenc

Modules

canvas-record

Re-export Recorder, RecorderStatus, all Encoders and utils.

Classes

Recorder
Encoder
FFmpegEncoder
FrameEncoder
GIFEncoder
H264MP4Encoder
MediaCaptureEncoder
MP4WasmEncoder
WebCodecsEncoder

Constants

isWebCodecsSupported : boolean

Check for WebCodecs support on the current platform.

Functions

estimateBitRate(width, height, frameRate, motionRank, bitrateMode) ⇒ number

Estimate the bit rate of a video rounded to nearest megabit. Based on "H.264 for the rest of us" by Kush Amerasinghe.

Typedefs

onStatusChangeCb : function

A callback to notify on the status change. To compare with RecorderStatus enum values.

RecorderOptions : object

Options for recording. All optional.

RecorderStartOptions : object

Options for recording initialisation. All optional.

EncoderExtensions : "mp4" | "webm" | "png" | "jpg" | "gif" | "mkv" | "mov"
EncoderTarget : "in-browser" | "file-system"
FFmpegEncoderOptions : object
FFmpegEncoderEncoderOptions : module:@ffmpeg/ffmpeg/dist/esm/types.js~FFMessageLoadConfig
GIFEncoderOptions : object
GIFEncoderQuantizeOptions : object
GIFEncoderEncoderOptions : object
H264MP4EncoderOptions : object
H264MP4EncoderEncoderOptions : module:h264-mp4-encoder~H264MP4Encoder
MediaCaptureEncoderOptions : object
MediaCaptureEncoderEncoderOptions : MediaRecorderOptions
MP4WasmEncoderOptions : object
MP4WasmEncoderEncoderOptions : VideoEncoderConfig
WebCodecsEncoderOptions : object
WebCodecsEncoderEncoderOptions : VideoEncoderConfig
WebCodecsMuxerOptions : module:mediabunny~OutputOptions

canvas-record

Re-export Recorder, RecorderStatus, all Encoders and utils.

Recorder

Kind: global class

new Recorder(context, [options])

Create a Recorder instance

ParamTypeDefault
contextRenderingContext
[options]RecorderOptions{}

recorder.defaultOptions : RecorderOptions

Sensible defaults for recording so that the recorder "just works".

Kind: instance property of Recorder

recorder.mimeTypes : object

A mapping of extension to their mime types

Kind: instance property of Recorder

recorder.start([startOptions])

Start the recording by initializing and optionally calling the initial step.

Kind: instance method of Recorder

ParamTypeDefault
[startOptions]RecorderStartOptions{}

recorder.step()

Encode a frame and increment the time and the playhead. Calls await canvasRecorder.stop() when duration is reached.

Kind: instance method of Recorder

recorder.stop() ⇒ ArrayBuffer | Uint8Array | Array.<Blob> | undefined

Stop the recording and return the recorded buffer. If options.download is set, automatically start downloading the resulting file. Is called when duration is reached or manually.

Kind: instance method of Recorder

recorder.dispose()

Clean up the recorder and encoder

Kind: instance method of Recorder

Encoder

Kind: global class Properties

NameType
targetEncoderTarget
extensionEncoderExtensions
[encoderOptions]object
[muxerOptions]object

new Encoder(options)

Base Encoder class. All Encoders extend it and its methods are called by the Recorder.

ParamType
optionsobject

encoder.supportedExtensions : Array.<Extensions>

The extension the encoder supports

Kind: instance property of Encoder

encoder.supportedTargets : Array.<EncoderTarget>

The target to download the file to.

Kind: instance property of Encoder

encoder.init(options)

Setup the encoder: load binary, instantiate muxers, setup file system target...

Kind: instance method of Encoder

ParamType
optionsobject

encoder.encode(frame, [frameNumber])

Encode a single frame. The frameNumber is usually used for GOP (Group Of Pictures).

Kind: instance method of Encoder

ParamType
framenumber
[frameNumber]number

encoder.stop() ⇒ ArrayBuffer | Uint8Array | Array.<Blob> | undefined

Stop the encoding process and cleanup the temporary data.

Kind: instance method of Encoder

encoder.dispose()

Clean up the encoder

Kind: instance method of Encoder

FFmpegEncoder

Kind: global class

new FFmpegEncoder([options])

ParamType
[options]FFmpegEncoderOptions

FrameEncoder

Kind: global class

GIFEncoder

Kind: global class

new GIFEncoder([options])

ParamType
[options]GIFEncoderOptions

H264MP4Encoder

Kind: global class

new H264MP4Encoder([options])

ParamType
[options]H264MP4EncoderOptions

MediaCaptureEncoder

Kind: global class

new MediaCaptureEncoder([options])

ParamType
[options]MediaCaptureEncoderOptions

MP4WasmEncoder

Kind: global class

new MP4WasmEncoder([options])

ParamType
[options]MP4WasmEncoderOptions

WebCodecsEncoder

Kind: global class

new WebCodecsEncoder([options])

ParamType
[options]WebCodecsEncoderOptions

RecorderStatus : enum

Enum for recorder status

Kind: global enum Read only: true Example

// Check recorder status before continuing
if (canvasRecorder.status !== RecorderStatus.Stopped) {
  rAFId = requestAnimationFrame(() => tick());
}

isWebCodecsSupported : boolean

Check for WebCodecs support on the current platform.

Kind: global constant

estimateBitRate(width, height, frameRate, motionRank, bitrateMode) ⇒ number

Estimate the bit rate of a video rounded to nearest megabit. Based on "H.264 for the rest of us" by Kush Amerasinghe.

Kind: global function Returns: number - A bitrate value in bits per second

ParamTypeDefaultDescription
widthnumber
heightnumber
frameRatenumber30
motionRanknumber4A factor of 1, 2 or 4
bitrateMode"variable" | "constant"variable

Example

// Full HD (1080p)
const bitRate = estimateBitRate(1920, 1080, 30, "variable");
const bitRateMbps = bitRate * 1_000_000; // => 13 Mbps

onStatusChangeCb : function

A callback to notify on the status change. To compare with RecorderStatus enum values.

Kind: global typedef

ParamTypeDescription
RecorderStatusnumberthe status

RecorderOptions : object

Options for recording. All optional.

Kind: global typedef Properties

NameTypeDefaultDescription
[name]string"&quot;&quot;"A name for the recorder, used as prefix for the default file name.
[duration]number10The recording duration in seconds. If set to Infinity, await canvasRecorder.stop() needs to be called manually.
[frameRate]number30The frame rate in frame per seconds. Use await canvasRecorder.step(); to go to the next frame.
[rect]Array[]Sub-region [x, y, width, height] of the canvas to encode from bottom left. Default to 0, 0 and context.drawingBufferWidth/drawingBufferHeight or canvas.width/height.
[download]booleantrueAutomatically download the recording when duration is reached or when await canvasRecorder.stop() is manually called.
[extension]string"&quot;mp4&quot;"Default file extension: infers which Encoder is selected.
[target]string"&quot;in-browser&quot;"Default writing target: in-browser or file-system when available.
[encoder]objectA specific encoder. Default encoder based on options.extension: GIF > WebCodecs > H264MP4.
[encoderOptions]objectSee src/encoders or individual packages for a list of options.
[muxerOptions]objectSee "mediabunny" for a list of options.
[frameOptions]objectOptions for createImageBitmap(), VideoFrame, getImageData() or canvas-screenshot.
[onStatusChange]onStatusChangeCb

RecorderStartOptions : object

Options for recording initialisation. All optional.

Kind: global typedef Properties

NameTypeDescription
[filename]stringOverwrite the file name completely.
[initOnly]booleanOnly initialised the recorder and don't call the first await recorder.step().

EncoderExtensions : "mp4" | "webm" | "png" | "jpg" | "gif" | "mkv" | "mov"

Kind: global typedef

EncoderTarget : "in-browser" | "file-system"

Kind: global typedef

FFmpegEncoderOptions : object

Kind: global typedef Properties

NameTypeDefault
[encoderOptions]FFmpegEncoderEncoderOptions{}

FFmpegEncoderEncoderOptions : module:@ffmpeg/ffmpeg/dist/esm/types.js~FFMessageLoadConfig

Kind: global typedef See: FFmpeg#load

GIFEncoderOptions : object

Kind: global typedef Properties

NameTypeDefault
[maxColors]number256
[quantizeOptions]GIFEncoderQuantizeOptions
[encoderOptions]GIFEncoderEncoderOptions{}

GIFEncoderQuantizeOptions : object

Kind: global typedef See: QuantizeOptions Properties

NameTypeDefault
[format]"rgb565" | "rgb444" | "rgba4444""rgb565"
[oneBitAlpha]boolean | numberfalse
[clearAlpha]booleantrue
[clearAlphaThreshold]number0
[clearAlphaColor]number0x00

GIFEncoderEncoderOptions : object

Kind: global typedef See: WriteFrameOpts Properties

NameTypeDefault
[palette]Array.<Array.<number>>
[first]booleanfalse
[transparent]boolean0
[transparentIndex]number0
[delay]number0
[repeat]number0
[dispose]number-1

H264MP4EncoderOptions : object

Kind: global typedef Properties

NameTypeDefault
[debug]boolean
[encoderOptions]H264MP4EncoderEncoderOptions{}

H264MP4EncoderEncoderOptions : module:h264-mp4-encoder~H264MP4Encoder

Kind: global typedef See: h264-mp4-encoder#api

MediaCaptureEncoderOptions : object

Kind: global typedef Properties

NameTypeDefault
[flushFrequency]number10
[encoderOptions]MediaCaptureEncoderEncoderOptions{}

MediaCaptureEncoderEncoderOptions : MediaRecorderOptions

Kind: global typedef See: MediaRecorder#options

MP4WasmEncoderOptions : object

Kind: global typedef Properties

NameTypeDefault
[groupOfPictures]number20
[flushFrequency]number10
[encoderOptions]MP4WasmEncoderEncoderOptions{}

MP4WasmEncoderEncoderOptions : VideoEncoderConfig

Kind: global typedef See: VideoEncoder.configure

WebCodecsEncoderOptions : object

Kind: global typedef Properties

NameTypeDefault
[groupOfPictures]number20
[flushFrequency]number10
[encoderOptions]WebCodecsEncoderEncoderOptions{}

WebCodecsEncoderEncoderOptions : VideoEncoderConfig

Kind: global typedef See: VideoEncoder.configure

WebCodecsMuxerOptions : module:mediabunny~OutputOptions

Kind: global typedef See: mediabunny#output-formats

License

All MIT:

MIT. See license file.

Similar Projects

Discover other similar projects and AI models.

RepositoryJavaScript
faressoft/terminalizer

🦄 Record your terminal and generate animated gif images or share a web player

16.2k
Aug 29, 2024
RepositoryC#
Tyrrrz/YoutubeDownloader

Downloads videos and playlists from YouTube

16.8k
Dec 2, 2026
RepositoryC#
MathewSachin/Captura

Capture Screen, Audio, Cursor, Mouse Clicks and Keystrokes

10.9k
May 25, 2026
RepositoryC
FFmpeg/FFmpeg

Mirror of https://git.ffmpeg.org/ffmpeg.git

68.4k
Nov 29, 2026
RepositoryJavaScript
zumerlab/snapdom

High-performance engine for capturing, modifying, and converting DOM elements into any format.

8.3k
Dec 1, 2026
RepositoryC#
NickeManarin/ScreenToGif

🎬 ScreenToGif allows you to record a selected area of your screen, edit and save it as a gif or video.

28.0k
Oct 14, 2026
RepositoryGo
charmbracelet/vhs

Your CLI home video recorder 📼

21.5k
Dec 2, 2026
RepositoryPython
Zulko/moviepy

Video editing with Python

15.2k
Aug 26, 2026
RepositoryTypeScript
piskelapp/piskel

A simple web-based tool for Spriting and Pixel art.

13.0k
Nov 25, 2026