Skip to main content

Your First Video Encode

This tutorial will have you encoding video frames in under 5 minutes.
1

Install node-webcodecs

Make sure you’ve installed FFmpeg first. See Installation Guide if you haven’t.
2

Create a simple encoder

Create a file called encode.js:
3

Create and encode frames

Add frame generation code:
4

Run it!

Expected Output:
5

Verify the output

Play the encoded video with FFmpeg:
You should see a red screen for 1 second!

What Just Happened?

The output callback receives:
  • chunk: EncodedVideoChunk containing compressed data
  • metadata: Decoder configuration (if this is the first chunk)
  • codec: MIME type or fourCC (see API Reference)
  • bitrate: Target bits per second
  • width/height: Frame dimensions
Why .close() is critical:
  • VideoFrame wraps native C++ memory
  • JavaScript GC doesn’t see it
  • Forgetting = memory leak!
Rule: Call .close() immediately after encoding.
Timestamps are in microseconds, not milliseconds:
  • 30 fps = 33,333 μs per frame
  • 60 fps = 16,667 μs per frame
  • 1 second = 1,000,000 μs

Full Example (TypeScript)

Common Mistakes

Forgetting to close frames
This is the #1 cause of memory leaks. See Backpressure Guide.
Wrong timestamp units
WebCodecs uses microseconds, not milliseconds.
Not awaiting flush()

Next Steps

Decode Video

Learn to decode video frames

API Reference

Complete VideoEncoder documentation

Cookbook

Real-world recipes

Hardware Acceleration

8-15x faster encoding