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.
92 lines (88 loc) • 3.09 kB
TypeScript
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';
type AvcBitstreamFormatOption = "annexb" | "avc";
type HevcBitstreamFormatOption = "annexb" | "hevc";
type AacBitstreamFormatOption = "aac" | "adts";
interface VideoConfig {
codec?: 'avc' | 'hevc' | 'vp9' | 'vp8' | 'av1';
/** Override codec string passed to VideoEncoder (e.g. "avc1.640028"). */
codecString?: string;
bitrate?: number;
/**
* Optional quantizer hint. Browser support varies by codec/platform.
* When set, it is forwarded to VideoEncoderConfig.
*/
quantizer?: number;
/** AVC-specific options. */
avc?: {
format?: AvcBitstreamFormatOption;
};
/** HEVC-specific options. */
hevc?: {
format?: HevcBitstreamFormatOption;
};
hardwareAcceleration?: 'no-preference' | 'prefer-hardware' | 'prefer-software';
latencyMode?: 'quality' | 'realtime';
keyFrameInterval?: number;
}
type AudioCodec = 'aac' | 'opus' | 'flac' | 'mp3' | 'vorbis' | 'pcm' | 'ulaw' | 'alaw';
interface AudioConfig {
codec?: AudioCodec;
/** Override codec string passed to AudioEncoder (e.g. "mp4a.40.2"). */
codecString?: string;
bitrate?: number;
sampleRate?: number;
channels?: number;
bitrateMode?: 'constant' | 'variable';
/** AAC-specific options. */
aac?: {
format?: AacBitstreamFormatOption;
};
}
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 };