@gyeonghokim/vega 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +301 -0
- package/dist/assets/media-worker-DqCztg6H.js +3363 -0
- package/dist/assets/media-worker-DqCztg6H.js.map +1 -0
- package/dist/audio/audio-renderer.d.ts +76 -0
- package/dist/audio/audio-renderer.d.ts.map +1 -0
- package/dist/audio/audio-worklet-processor.d.ts +14 -0
- package/dist/audio/audio-worklet-processor.d.ts.map +1 -0
- package/dist/audio/ring-buffer.d.ts +71 -0
- package/dist/audio/ring-buffer.d.ts.map +1 -0
- package/dist/convert.d.ts +27 -0
- package/dist/convert.d.ts.map +1 -0
- package/dist/demuxer/mp4-demuxer.d.ts +133 -0
- package/dist/demuxer/mp4-demuxer.d.ts.map +1 -0
- package/dist/demuxer/mp4box-types.d.ts +149 -0
- package/dist/demuxer/mp4box-types.d.ts.map +1 -0
- package/dist/factory.d.ts +16 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/renderers/renderer-2d.d.ts +12 -0
- package/dist/renderers/renderer-2d.d.ts.map +1 -0
- package/dist/renderers/renderer-webgl.d.ts +11 -0
- package/dist/renderers/renderer-webgl.d.ts.map +1 -0
- package/dist/renderers/renderer-webgpu.d.ts +11 -0
- package/dist/renderers/renderer-webgpu.d.ts.map +1 -0
- package/dist/types/index.d.ts +24 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/vega.d.ts +189 -0
- package/dist/types/vega.d.ts.map +1 -0
- package/dist/types/worker-messages.d.ts +179 -0
- package/dist/types/worker-messages.d.ts.map +1 -0
- package/dist/vega.d.ts +13 -0
- package/dist/vega.d.ts.map +1 -0
- package/dist/vega.js +488 -0
- package/dist/vega.js.map +1 -0
- package/dist/vega.umd.cjs +42 -0
- package/dist/vega.umd.cjs.map +1 -0
- package/dist/worker/media-worker.d.ts +6 -0
- package/dist/worker/media-worker.d.ts.map +1 -0
- package/dist/worker/video-renderer.d.ts +63 -0
- package/dist/worker/video-renderer.d.ts.map +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 GyeongHoKim
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
# Vega
|
|
2
|
+
|
|
3
|
+
See more. See better. See Vega
|
|
4
|
+
|
|
5
|
+
A WebCodecs-based MP4 video player with **custom VideoFrame processing** support. Apply real-time effects like fisheye undistortion, super resolution, or any custom image processing while the player handles decoding, synchronization, and rendering.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **WebCodecs API**: Hardware-accelerated video decoding
|
|
10
|
+
- **Custom Frame Processing**: Inject your own VideoFrame adapter for real-time effects
|
|
11
|
+
- **Multiple Render Backends**: Canvas 2D, WebGL, and WebGPU
|
|
12
|
+
- **Web Worker Architecture**: Demuxing and decoding run in background threads
|
|
13
|
+
- **Audio Support**: WebAudio API with AudioWorklet (coming soon)
|
|
14
|
+
- **TypeScript**: Full type definitions included
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npm install @gyeonghokim/vega
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { createVega } from "@gyeonghokim/vega";
|
|
26
|
+
|
|
27
|
+
const canvas = document.getElementById("video-canvas") as HTMLCanvasElement;
|
|
28
|
+
|
|
29
|
+
// Create player
|
|
30
|
+
const player = createVega({ canvas });
|
|
31
|
+
|
|
32
|
+
// Load and play
|
|
33
|
+
await player.load("video.mp4");
|
|
34
|
+
player.play();
|
|
35
|
+
|
|
36
|
+
// Controls
|
|
37
|
+
player.pause();
|
|
38
|
+
await player.seek(10); // Seek to 10 seconds
|
|
39
|
+
player.setVolume(0.5);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Custom VideoFrame Adapter
|
|
43
|
+
|
|
44
|
+
The key feature of Vega is the ability to process every video frame before rendering. Use this for effects like lens correction, color grading, upscaling, or any custom image processing.
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { createVega, type VideoFrameAdapter } from "@gyeonghokim/vega";
|
|
48
|
+
|
|
49
|
+
// Example: Grayscale filter adapter
|
|
50
|
+
const grayscaleAdapter: VideoFrameAdapter = {
|
|
51
|
+
async process(frame: VideoFrame): Promise<VideoFrame> {
|
|
52
|
+
// Create an OffscreenCanvas for processing
|
|
53
|
+
const canvas = new OffscreenCanvas(frame.displayWidth, frame.displayHeight);
|
|
54
|
+
const ctx = canvas.getContext("2d")!;
|
|
55
|
+
|
|
56
|
+
// Draw the frame
|
|
57
|
+
ctx.drawImage(frame, 0, 0);
|
|
58
|
+
|
|
59
|
+
// Apply grayscale filter
|
|
60
|
+
ctx.filter = "grayscale(100%)";
|
|
61
|
+
ctx.drawImage(canvas, 0, 0);
|
|
62
|
+
|
|
63
|
+
// Create new VideoFrame from processed canvas
|
|
64
|
+
const processedFrame = new VideoFrame(canvas, {
|
|
65
|
+
timestamp: frame.timestamp,
|
|
66
|
+
duration: frame.duration ?? undefined,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Close original frame (adapter is responsible for this)
|
|
70
|
+
frame.close();
|
|
71
|
+
|
|
72
|
+
return processedFrame;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// Create player with adapter
|
|
77
|
+
const player = createVega({
|
|
78
|
+
canvas: document.getElementById("canvas") as HTMLCanvasElement,
|
|
79
|
+
adapter: grayscaleAdapter,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
await player.load("video.mp4");
|
|
83
|
+
player.play();
|
|
84
|
+
|
|
85
|
+
// You can also change the adapter at runtime
|
|
86
|
+
player.setAdapter(null); // Remove adapter
|
|
87
|
+
player.setAdapter(grayscaleAdapter); // Set new adapter
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## API Reference
|
|
91
|
+
|
|
92
|
+
### `createVega(options: VegaOptions): Vega`
|
|
93
|
+
|
|
94
|
+
Creates a new Vega player instance.
|
|
95
|
+
|
|
96
|
+
#### Options
|
|
97
|
+
|
|
98
|
+
| Option | Type | Default | Description |
|
|
99
|
+
|--------|------|---------|-------------|
|
|
100
|
+
| `canvas` | `HTMLCanvasElement \| OffscreenCanvas` | required | Target canvas for video rendering |
|
|
101
|
+
| `rendererType` | `"2d" \| "webgl" \| "webgpu"` | `"2d"` | Rendering backend |
|
|
102
|
+
| `adapter` | `VideoFrameAdapter` | `undefined` | Custom frame processor |
|
|
103
|
+
| `volume` | `number` | `1.0` | Initial volume (0.0 - 1.0) |
|
|
104
|
+
| `loop` | `boolean` | `false` | Loop playback |
|
|
105
|
+
| `autoplay` | `boolean` | `false` | Auto-start after loading |
|
|
106
|
+
|
|
107
|
+
### Vega Instance Methods
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
interface Vega {
|
|
111
|
+
// Loading
|
|
112
|
+
load(source: string | File | Blob): Promise<MediaInfo>;
|
|
113
|
+
|
|
114
|
+
// Playback control
|
|
115
|
+
play(): Promise<void>;
|
|
116
|
+
pause(): void;
|
|
117
|
+
seek(time: number): Promise<void>;
|
|
118
|
+
stop(): void;
|
|
119
|
+
|
|
120
|
+
// Properties (readonly)
|
|
121
|
+
readonly currentTime: number;
|
|
122
|
+
readonly duration: number;
|
|
123
|
+
readonly paused: boolean;
|
|
124
|
+
readonly ended: boolean;
|
|
125
|
+
readonly volume: number;
|
|
126
|
+
readonly muted: boolean;
|
|
127
|
+
readonly state: PlaybackState;
|
|
128
|
+
readonly mediaInfo: MediaInfo | null;
|
|
129
|
+
|
|
130
|
+
// Settings
|
|
131
|
+
setVolume(volume: number): void;
|
|
132
|
+
setMuted(muted: boolean): void;
|
|
133
|
+
setAdapter(adapter: VideoFrameAdapter | null): void;
|
|
134
|
+
getAdapter(): VideoFrameAdapter | null;
|
|
135
|
+
|
|
136
|
+
// Events
|
|
137
|
+
on(event: VegaEvent, callback: VegaEventCallback): void;
|
|
138
|
+
off(event: VegaEvent, callback: VegaEventCallback): void;
|
|
139
|
+
|
|
140
|
+
// Cleanup
|
|
141
|
+
destroy(): void;
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Events
|
|
146
|
+
|
|
147
|
+
| Event | Description |
|
|
148
|
+
|-------|-------------|
|
|
149
|
+
| `play` | Playback started |
|
|
150
|
+
| `pause` | Playback paused |
|
|
151
|
+
| `ended` | Playback ended |
|
|
152
|
+
| `seeking` | Seek operation started |
|
|
153
|
+
| `seeked` | Seek operation completed |
|
|
154
|
+
| `timeupdate` | Current time changed |
|
|
155
|
+
| `loadedmetadata` | Media info available |
|
|
156
|
+
| `canplay` | Ready to play |
|
|
157
|
+
| `volumechange` | Volume or muted state changed |
|
|
158
|
+
| `error` | An error occurred |
|
|
159
|
+
|
|
160
|
+
### MediaInfo
|
|
161
|
+
|
|
162
|
+
Returned by `load()` and available via `player.mediaInfo`:
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
interface MediaInfo {
|
|
166
|
+
duration: number;
|
|
167
|
+
videoTrack?: {
|
|
168
|
+
codec: string;
|
|
169
|
+
width: number;
|
|
170
|
+
height: number;
|
|
171
|
+
frameRate: number;
|
|
172
|
+
bitrate?: number;
|
|
173
|
+
};
|
|
174
|
+
audioTrack?: {
|
|
175
|
+
codec: string;
|
|
176
|
+
sampleRate: number;
|
|
177
|
+
channelCount: number;
|
|
178
|
+
bitrate?: number;
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## VideoFrameAdapter Interface
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
interface VideoFrameAdapter {
|
|
187
|
+
/**
|
|
188
|
+
* Process a VideoFrame before rendering.
|
|
189
|
+
* @param frame - The decoded VideoFrame to process
|
|
190
|
+
* @returns The processed VideoFrame (can be the same or a new one)
|
|
191
|
+
*/
|
|
192
|
+
process(frame: VideoFrame): VideoFrame | Promise<VideoFrame>;
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
**Important**: If your adapter creates a new VideoFrame, you must close the original frame to prevent memory leaks.
|
|
197
|
+
|
|
198
|
+
## Raw Frame Utilities
|
|
199
|
+
|
|
200
|
+
For working with raw video data (e.g., from ffmpeg or custom sources):
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
import {
|
|
204
|
+
rawToVideoFrame,
|
|
205
|
+
videoFrameToRaw,
|
|
206
|
+
getRawByteLength,
|
|
207
|
+
type SupportedPixelFormat,
|
|
208
|
+
} from "@gyeonghokim/vega";
|
|
209
|
+
|
|
210
|
+
// Raw buffer → VideoFrame
|
|
211
|
+
const raw = await (await fetch("frame_1920x1080_rgba.raw")).arrayBuffer();
|
|
212
|
+
const frame = rawToVideoFrame(raw, "RGBA", 1920, 1080, { timestamp: 0 });
|
|
213
|
+
|
|
214
|
+
// VideoFrame → raw buffer
|
|
215
|
+
const buffer = await videoFrameToRaw(frame);
|
|
216
|
+
|
|
217
|
+
// Calculate byte length for a format
|
|
218
|
+
const bytes = getRawByteLength("I420", 1920, 1080); // 3110400
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Supported formats: `I420`, `I420A`, `I422`, `I444`, `I444A`, `NV12`, `RGBA`, `RGBX`, `BGRA`, `BGRX`
|
|
222
|
+
|
|
223
|
+
## Browser Requirements
|
|
224
|
+
|
|
225
|
+
- **WebCodecs API**: Required for video decoding
|
|
226
|
+
- **Web Workers**: Required for background processing
|
|
227
|
+
- **SharedArrayBuffer**: Required for audio (needs COOP/COEP headers)
|
|
228
|
+
|
|
229
|
+
For SharedArrayBuffer support, your server must send these headers:
|
|
230
|
+
|
|
231
|
+
```
|
|
232
|
+
Cross-Origin-Opener-Policy: same-origin
|
|
233
|
+
Cross-Origin-Embedder-Policy: require-corp
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## Supported Formats
|
|
237
|
+
|
|
238
|
+
### Container
|
|
239
|
+
- MP4 (MPEG-4 Part 14)
|
|
240
|
+
|
|
241
|
+
### Video Codecs
|
|
242
|
+
- H.264 / AVC
|
|
243
|
+
- H.265 / HEVC
|
|
244
|
+
- VP8
|
|
245
|
+
- VP9
|
|
246
|
+
- AV1
|
|
247
|
+
|
|
248
|
+
### Audio Codecs
|
|
249
|
+
- AAC
|
|
250
|
+
- MP3
|
|
251
|
+
- Opus
|
|
252
|
+
|
|
253
|
+
## Development
|
|
254
|
+
|
|
255
|
+
```sh
|
|
256
|
+
npm install
|
|
257
|
+
npm run format # Format code
|
|
258
|
+
npm run lint # Lint code
|
|
259
|
+
npm run typecheck # TypeScript check
|
|
260
|
+
npm test # Run tests
|
|
261
|
+
npm run build # Build library
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## Architecture
|
|
265
|
+
|
|
266
|
+
```mermaid
|
|
267
|
+
flowchart TB
|
|
268
|
+
subgraph MainThread["Main Thread"]
|
|
269
|
+
Vega["Vega Player"]
|
|
270
|
+
Adapter["VideoFrame Adapter<br/>(optional)"]
|
|
271
|
+
Renderer["Renderer<br/>(2D/WebGL/WebGPU)"]
|
|
272
|
+
Canvas["Canvas"]
|
|
273
|
+
AudioCtx["AudioContext"]
|
|
274
|
+
WorkerBridge["Worker Bridge"]
|
|
275
|
+
|
|
276
|
+
Vega --> WorkerBridge
|
|
277
|
+
WorkerBridge --> Adapter
|
|
278
|
+
Adapter -->|"VideoFrame"| Renderer
|
|
279
|
+
Renderer --> Canvas
|
|
280
|
+
WorkerBridge -->|"AudioData"| AudioCtx
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
subgraph MediaWorker["Media Worker"]
|
|
284
|
+
Demuxer["Demuxer<br/>(MP4Box.js)"]
|
|
285
|
+
VideoDecoder["Video Decoder<br/>(WebCodecs)"]
|
|
286
|
+
AudioDecoder["Audio Decoder<br/>(WebCodecs)"]
|
|
287
|
+
FrameBuffer["Frame Buffer"]
|
|
288
|
+
|
|
289
|
+
Demuxer -->|"EncodedVideoChunk"| VideoDecoder
|
|
290
|
+
Demuxer -->|"EncodedAudioChunk"| AudioDecoder
|
|
291
|
+
VideoDecoder --> FrameBuffer
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
WorkerBridge <-->|"postMessage"| Demuxer
|
|
295
|
+
FrameBuffer -->|"VideoFrame"| WorkerBridge
|
|
296
|
+
AudioDecoder -->|"AudioData"| WorkerBridge
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
## License
|
|
300
|
+
|
|
301
|
+
MIT
|