A pure TypeScript AVIF encoder and decoder with zero native dependencies.
- 🚀 Pure TypeScript - no native dependencies
- 📦 Zero dependencies
- 🎨 HEIF/ISOBMFF container support
- 🔄 AV1 still image codec
- 📐 Alpha channel support
bun add ts-avif
# or
npm install ts-avifimport { decode } from 'ts-avif'
const buffer = await Bun.file('image.avif').arrayBuffer()
const { data, width, height, hasAlpha, bitDepth } = decode(new Uint8Array(buffer))
// data is RGBA pixel data (4 bytes per pixel)
console.log(`Image size: ${width}x${height}, bit depth: ${bitDepth}`)import { encode } from 'ts-avif'
const imageData = {
data: new Uint8Array(width * height * 4), // RGBA pixel data
width: 100,
height: 100,
}
const avifBuffer = encode(imageData, {
quality: 80,
lossless: false,
})
await Bun.write('output.avif', avifBuffer)import { parseISOBMFF, getAvifInfo } from 'ts-avif'
const buffer = await Bun.file('image.avif').arrayBuffer()
const boxes = parseISOBMFF(new Uint8Array(buffer))
const info = getAvifInfo(boxes)
console.log(info)
// {
// width: 1920,
// height: 1080,
// hasAlpha: false,
// bitDepth: 10,
// colorSpace: 'srgb',
// isSequence: false
// }Decodes an AVIF image buffer to RGBA pixel data.
Options:
format?: 'rgba' | 'rgb'- Output format (default: 'rgba')ignoreAlpha?: boolean- Ignore alpha channel
Returns:
data: Uint8Array- Pixel datawidth: number- Image width in pixelsheight: number- Image height in pixelshasAlpha?: boolean- Whether the image has an alpha channelbitDepth?: 8 | 10 | 12- Color bit depth
Encodes RGBA pixel data to AVIF format.
Options:
quality?: number- Quality (0-100, default: 80)lossless?: boolean- Use lossless encodingeffort?: number- Speed/effort trade-off (0-10, default: 6)alpha?: boolean- Enable alpha channelchromaSubsampling?: '4:2:0' | '4:2:2' | '4:4:4'- Chroma subsampling
The library fully supports parsing HEIF/ISOBMFF container format:
ftyp- File type boxmeta- Metadata containerhdlr- Handler boxpitm- Primary item boxiloc- Item location boxiinf- Item info boxiprp- Item properties boxmdat- Media data box
This is a pure TypeScript implementation of AVIF decoding and encoding. AV1 is a complex codec, and this implementation focuses on still image support (AVIF).
Key components:
- HEIF container parsing (ISO Base Media File Format)
- AV1 OBU (Open Bitstream Unit) parsing
- AV1 sequence header and frame decoding framework
- Full AV1 decoding is complex; this is a foundation implementation
- Animation support is not yet implemented
- Some advanced AV1 features may not be fully supported
MIT