|
| 1 | +# Canvas Encoder |
| 2 | + |
| 3 | +The `CanvasEncoder` is a powerful tool for creating video recordings directly from a canvas element in the browser, ideal for capturing canvas-based games or animations without the need for our `Composition` object. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To use the `CanvasEncoder`, import it into your project: |
| 8 | + |
| 9 | +```typescript |
| 10 | +import { CanvasEncoder } from '@diffusionstudio/core'; |
| 11 | +``` |
| 12 | + |
| 13 | +## Basic Usage |
| 14 | + |
| 15 | +Start by creating a canvas element and setting its dimensions to match your desired video resolution: |
| 16 | + |
| 17 | +```typescript |
| 18 | +// Make sure to assign video dimensions |
| 19 | +const canvas = new OffscreenCanvas(1920, 1080); |
| 20 | + |
| 21 | +const encoder = new CanvasEncoder(canvas); |
| 22 | +``` |
| 23 | + |
| 24 | +### Configuration Options |
| 25 | + |
| 26 | +The `CanvasEncoder` constructor accepts an optional second argument to configure the output settings. The default configurations are: |
| 27 | + |
| 28 | +```typescript |
| 29 | +{ |
| 30 | + sampleRate: 44100, // Audio sample rate in Hz |
| 31 | + numberOfChannels: 2, // Number of audio channels |
| 32 | + videoBitrate: 10e6, // Video bitrate in bits per second |
| 33 | + fps: 30, // Frames per second for the video |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +## Video Encoding |
| 38 | + |
| 39 | +After setting up the encoder, you can encode individual frames from the canvas to create your video. The following example creates a 3-second video by encoding 90 frames: |
| 40 | + |
| 41 | +```typescript |
| 42 | +const ctx = canvas.getContext("2d")!; |
| 43 | + |
| 44 | +for (let i = 0; i < 90; i++) { |
| 45 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 46 | + // background |
| 47 | + ctx.fillStyle = "blue"; |
| 48 | + ctx.fillRect(0, 0, canvas.width, canvas.height); |
| 49 | + // text |
| 50 | + ctx.fillStyle = "white"; |
| 51 | + ctx.font = "50px serif"; // animated Hello World |
| 52 | + ctx.fillText("Hello world", 10 + i * 20, 10 + i * 12); |
| 53 | + |
| 54 | + // Encode the current canvas state |
| 55 | + await encoder.encodeVideo(); |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +## Audio Encoding (Optional) |
| 60 | + |
| 61 | +To add audio to your video, you can use the Web Audio API. The `CanvasEncoder` supports encoding audio buffers along with the video: |
| 62 | + |
| 63 | +```typescript |
| 64 | +const response = await fetch('https://diffusion-studio-public.s3.eu-central-1.amazonaws.com/audio/sfx/tada.mp3'); |
| 65 | +const arrayBuffer = await response.arrayBuffer(); |
| 66 | +const context = new OfflineAudioContext(2, 1, 48e3); |
| 67 | + |
| 68 | +// Decode the audio data to get an AudioBuffer |
| 69 | +const audioBuffer = await context.decodeAudioData(arrayBuffer); |
| 70 | + |
| 71 | +// Encode the audio buffer |
| 72 | +await encoder.encodeAudio(audioBuffer); |
| 73 | +``` |
| 74 | + |
| 75 | +The audio will be automatically resampled to match the output configuration, so you don't need to worry about sample rate differences. |
| 76 | + |
| 77 | +> Note: By adding the audio, the resulting video duration will be 6 seconds, as that's the duration of the sound effect. In production you want to keep the video and audio durations synced. |
| 78 | +
|
| 79 | +## Exporting the Video |
| 80 | + |
| 81 | +Once you've encoded all the video frames and audio data, finalize the encoding process and export the result as an MP4 file: |
| 82 | + |
| 83 | +```typescript |
| 84 | +const blob = await encoder.export(); |
| 85 | +``` |
| 86 | + |
| 87 | +The `export` method returns a `Blob` containing the video with a `video/mp4` MIME type. You can then save or process this blob as needed. |
0 commit comments