avi

package
v0.0.0-...-3c35588 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: Apache-2.0, MIT Imports: 7 Imported by: 0

Documentation

Overview

Package avi muxes compressed video frames and PCM audio into an AVI file.

AVI is a RIFF container: a header list describing one video and one audio stream, a movi list of interleaved frame and audio chunks, and a trailing index. The per-frame video codec is pluggable through a FrameEncoder, so frames can be stored losslessly as PNG or smaller as MJPEG without changing the muxer; the audio is uncompressed little-endian PCM.

Use NewWriter over an io.WriteSeeker with the stream formats and an encoder, then Writer.WriteVideo and Writer.WriteAudio in playback order, and finally Writer.Close, which writes the index and patches the header totals that are known only once every chunk has been written.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidFormat = errors.New("avi: invalid format")

ErrInvalidFormat indicates a VideoFormat or AudioFormat whose dimensions, frame rate, or audio parameters are not positive.

Functions

This section is empty.

Types

type AudioFormat

type AudioFormat struct {
	// SampleRate is the number of frames per second.
	SampleRate int

	// Channels is the number of interleaved channels.
	Channels int

	// BitsPerSample is the width of one sample of one channel.
	BitsPerSample int
}

AudioFormat describes the PCM audio stream.

type FrameEncoder

type FrameEncoder interface {
	// FourCC is the codec tag written into the stream header, such as "MPNG".
	FourCC() string

	// Encode returns img compressed as one frame.
	Encode(img image.Image) ([]byte, error)
}

FrameEncoder compresses one decoded frame into the bytes of an AVI video chunk and names the codec they are stored under.

func MJPEG

func MJPEG(quality int) FrameEncoder

MJPEG returns a lossy FrameEncoder backed by the standard image/jpeg encoder at the given quality (1..100).

func PNG

func PNG() FrameEncoder

PNG returns a lossless FrameEncoder backed by the standard image/png encoder.

type VideoFormat

type VideoFormat struct {
	// Width and Height are the frame dimensions in pixels.
	Width  int
	Height int

	// FrameRateNum and FrameRateDen are the numerator and denominator of the
	// frame rate in frames per second.
	FrameRateNum int
	FrameRateDen int
}

VideoFormat describes the video stream. The codec tag comes from the FrameEncoder, not this struct.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer muxes video and audio chunks into an AVI over an io.WriteSeeker, patching the header totals on Writer.Close.

func NewWriter

func NewWriter(ws io.WriteSeeker, video VideoFormat, audio AudioFormat, enc FrameEncoder) (*Writer, error)

NewWriter returns a Writer that muxes frames described by video and audio, encoding each frame with enc. It writes the AVI header immediately and reports ErrInvalidFormat for a non-positive format, or any error from ws.

func (*Writer) Close

func (w *Writer) Close() error

Close writes the index and patches the header totals. The Writer must not be used afterward.

func (*Writer) WriteAudio

func (w *Writer) WriteAudio(pcm []byte) error

WriteAudio writes pcm as the next audio chunk. pcm holds interleaved little-endian samples.

func (*Writer) WriteVideo

func (w *Writer) WriteVideo(img image.Image) error

WriteVideo encodes img with the writer's FrameEncoder and writes it as the next video frame. It reports any encoding or write error.

Jump to

Keyboard shortcuts

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