avif

package module
Version: v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2019 License: CC0-1.0 Imports: 6 Imported by: 10

README

go-avif Build Status GoDoc

go-avif implements AVIF (AV1 Still Image File Format) encoder for Go using libaom, the high quality AV1 codec.

Requirements

Make sure libaom is installed. On typical Linux distro just run:

sudo apt-get install libaom-dev

Usage

To use go-avif in your Go code:

import "github.com/Kagami/go-avif"

To install go-avif in your $GOPATH:

go get github.com/Kagami/go-avif

For further details see GoDoc documentation.

Example

package main

import (
	"image"
	_ "image/jpeg"
	"log"
	"os"

	"github.com/Kagami/go-avif"
)

func main() {
	if len(os.Args) != 3 {
		log.Fatalf("Usage: %s src.jpg dst.avif", os.Args[0])
	}

	srcPath := os.Args[1]
	src, err := os.Open(srcPath)
	if err != nil {
		log.Fatalf("Can't open sorce file: %v", err)
	}

	dstPath := os.Args[2]
	dst, err := os.Create(dstPath)
	if err != nil {
		log.Fatalf("Can't create destination file: %v", err)
	}

	img, _, err := image.Decode(src)
	if err != nil {
		log.Fatalf("Can't decode source file: %v", err)
	}

	err = avif.Encode(dst, img, nil)
	if err != nil {
		log.Fatalf("Can't encode source image: %v", err)
	}

	log.Printf("Encoded AVIF at %s", dstPath)
}

CLI

go-avif comes with handy CLI utility avif. It supports encoding of JPEG and PNG files to AVIF:

# Compile and put avif binary to $GOPATH/bin
go get github.com/Kagami/go-avif/...

# Encode JPEG to AVIF with default settings
avif -e cat.jpg -o kitty.avif

# Encode PNG with slowest speed
avif -e dog.png -o doggy.avif --best -q 15

# Lossless encoding
avif -e pig.png -o piggy.avif --lossless

# Show help
avif -h

Static 64-bit builds for Windows, macOS and Linux are available at releases page. They include latest libaom from git at the moment of build.

Display

To display resulting AVIF files take a look at software listed here. E.g. use avif.js web viewer.

License

go-avif is licensed under CC0.

Documentation

Overview

Package avif implements a AVIF image encoder.

The AVIF specification is at https://aomediacodec.github.io/av1-avif/.

Example
package main

import (
	"image"
	_ "image/jpeg"
	"log"
	"os"

	"github.com/Kagami/go-avif"
)

const usageHelp = "Usage: %s src.jpg dst.avif"

func main() {
	if len(os.Args) != 3 {
		log.Fatalf(usageHelp, os.Args[0])
	}

	srcPath := os.Args[1]
	src, err := os.Open(srcPath)
	if err != nil {
		log.Fatalf("Can't open sorce file: %v", err)
	}

	dstPath := os.Args[2]
	dst, err := os.Create(dstPath)
	if err != nil {
		log.Fatalf("Can't create destination file: %v", err)
	}

	img, _, err := image.Decode(src)
	if err != nil {
		log.Fatalf("Can't decode source file: %v", err)
	}

	err = avif.Encode(dst, img, nil)
	if err != nil {
		log.Fatalf("Can't encode source image: %v", err)
	}

	log.Printf("Encoded AVIF at %s", dstPath)
}
Output:

Index

Examples

Constants

View Source
const (
	MinSpeed   = 0
	MaxSpeed   = 8
	MinQuality = 0
	MaxQuality = 63
)

Encoder constants.

Variables

View Source
var DefaultOptions = Options{
	Threads:        0,
	Speed:          4,
	Quality:        25,
	SubsampleRatio: nil,
}

DefaultOptions defines default encoder config.

Functions

func Encode

func Encode(w io.Writer, m image.Image, o *Options) error

Encode writes the Image m to w in AVIF format with the given options. Default parameters are used if a nil *Options is passed.

NOTE: Image pixels are converted to RGBA first using standard Go library. This is no-op for PNG images and does the right thing for JPEG since they are normally stored as BT.601 full range with some chroma subsampling. Then pixels are converted to BT.709 limited range with specified chroma subsampling.

Alpha channel and monochrome are not supported at the moment. Only 4:2:0 8-bit images are supported at the moment.

Types

type EncoderError

type EncoderError int

An EncoderError reports that the encoder error has occured.

func (EncoderError) Error

func (e EncoderError) Error() string

func (EncoderError) ToString

func (e EncoderError) ToString() string

type MuxerError

type MuxerError string

A MuxerError reports that the muxer error has occured.

func (MuxerError) Error

func (e MuxerError) Error() string

type Options

type Options struct {
	Threads        int
	Speed          int
	Quality        int
	SubsampleRatio *image.YCbCrSubsampleRatio
}

Options are the encoding parameters. Threads ranges from 1, 0 means use all available cores. Speed ranges from MinSpeed to MaxSpeed. Quality ranges from MinQuality to MaxQuality, lower is better, 0 means lossless encoding. SubsampleRatio specifies subsampling of the encoded image, nil means 4:2:0.

type OptionsError

type OptionsError string

An OptionsError reports that the passed options are not valid.

func (OptionsError) Error

func (e OptionsError) Error() string

Source Files

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL