UNPKG

webcodecs-encoder

Version:

A TypeScript library for browser environments to encode video (H.264/AVC, VP9, VP8) and audio (AAC, Opus) using the WebCodecs API and mux them into MP4 or WebM containers with real-time streaming support. New function-first API design.

67 lines (63 loc) 2.19 kB
type Frame = VideoFrame | HTMLCanvasElement | OffscreenCanvas | ImageBitmap | ImageData; interface VideoFile { file: File | Blob; type: string; } type VideoSource = Frame[] | AsyncIterable<Frame> | MediaStream | VideoFile; type QualityPreset = 'low' | 'medium' | 'high' | 'lossless'; interface VideoConfig { codec?: 'avc' | 'hevc' | 'vp9' | 'vp8' | 'av1'; bitrate?: number; hardwareAcceleration?: 'no-preference' | 'prefer-hardware' | 'prefer-software'; latencyMode?: 'quality' | 'realtime'; keyFrameInterval?: number; } interface AudioConfig { codec?: 'aac' | 'opus'; bitrate?: number; sampleRate?: number; channels?: number; bitrateMode?: 'constant' | 'variable'; } interface ProgressInfo { percent: number; processedFrames: number; totalFrames?: number; fps: number; stage: string; estimatedRemainingMs?: number; } interface EncodeOptions { width?: number; height?: number; frameRate?: number; quality?: QualityPreset; video?: VideoConfig | false; audio?: AudioConfig | false; container?: 'mp4' | 'webm'; firstTimestampBehavior?: "offset" | "strict"; latencyMode?: "quality" | "realtime"; maxVideoQueueSize?: number; maxAudioQueueSize?: number; backpressureStrategy?: "drop" | "wait"; onProgress?: (progress: ProgressInfo) => void; onError?: (error: EncodeError) => void; } type EncodeErrorType = 'not-supported' | 'initialization-failed' | 'configuration-error' | 'invalid-input' | 'encoding-failed' | 'video-encoding-error' | 'audio-encoding-error' | 'muxing-failed' | 'cancelled' | 'timeout' | 'worker-error' | 'filesystem-error' | 'unknown'; declare class EncodeError extends Error { type: EncodeErrorType; cause?: unknown; constructor(type: EncodeErrorType, message: string, cause?: unknown); } /** * Streaming encode function implementation */ /** * Streaming encode function * * @param source Video source to encode * @param options Encoding options * @returns AsyncGenerator of encoded chunks */ declare function encodeStream(source: VideoSource, options?: EncodeOptions): AsyncGenerator<Uint8Array>; export { encodeStream };