demux

package
v0.3.21 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 17 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package demux reads the tracks and packets of MP4 (ISO BMFF) and Matroska/WebM files, in pure Go.

Open sniffs the container, enumerates its tracks (codec, frame size or sample rate and channels, the codec's configuration record) and returns a File that reads packets in file order with their timestamps and seeks to keyframes: through the sample tables (stss) of an MP4, its fragments' sample flags, or a Matroska file's Cues. A Matroska file without Cues for the track seeks through an index built by scanning its clusters once, on the first seek.

f, err := demux.OpenFile("clip.mkv")
for _, t := range f.Tracks() { ... }
p, err := f.ReadPacket() // io.EOF after the last packet
at, err := f.Seek(30 * time.Second)

Every size and count read from the file is checked against the bytes around it before anything is allocated for it, so a malformed file fails with an error instead of exhausting memory.

Index

Constants

This section is empty.

Variables

View Source
var ErrMalformed = errors.New("malformed file")

ErrMalformed is a file whose structure is invalid; errors wrap it with what was being read and where.

View Source
var ErrUnknownFormat = errors.New("not an MP4 or Matroska file")

ErrUnknownFormat is a file that is neither MP4 nor Matroska.

Functions

func XiphHeaders

func XiphHeaders(config []byte) (id, comment, setup []byte, err error)

XiphHeaders splits a Vorbis track's Config, Xiph-laced as Matroska stores CodecPrivate, into its identification, comment and setup headers.

Types

type Codec

type Codec uint8

Codec is a coding format a track is in.

const (
	Unknown Codec = iota
	H264
	H265
	AV1
	VP8
	VP9
	Opus
	Vorbis
	AAC
	FLAC
	MP3
)

The codecs the demuxer recognises. Unknown is any other, named by the track's CodecName.

func (Codec) String

func (c Codec) String() string

type File

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

File is an open media file. A File belongs to one goroutine.

func Open

func Open(r io.ReaderAt, size int64, name string) (*File, error)

Open demuxes the size bytes of r. name is how errors refer to the file.

func OpenFile

func OpenFile(path string) (*File, error)

OpenFile opens and demuxes the file at path. Close closes it.

func (*File) Close

func (f *File) Close() error

Close closes the file OpenFile opened; for a File from Open it does nothing.

func (*File) Duration

func (f *File) Duration() time.Duration

Duration is the file's length; zero when the file does not say.

func (*File) Format

func (f *File) Format() Format

Format is the file's container format.

func (*File) Name

func (f *File) Name() string

Name is the name the file was opened with.

func (*File) ReadPacket

func (f *File) ReadPacket() (Packet, error)

ReadPacket returns the next packet of the selected tracks in file order, or io.EOF after the last.

func (*File) Seek

func (f *File) Seek(t time.Duration) (time.Duration, error)

Seek positions the file so the next packets start at the latest keyframe of the seek track presented at or before t less its SeekPreRoll: the first selected video track, else the first selected track. Other selected tracks resume at their packets presented from that keyframe's time less their own SeekPreRoll. It returns the keyframe's PTS; a caller wanting output from t decodes from there and drops what presents before t.

func (*File) Select

func (f *File) Select(tracks ...int) error

Select limits ReadPacket to the tracks given; the others' data is not read. Every track is selected after Open.

func (*File) Tracks

func (f *File) Tracks() []Track

Tracks are the file's tracks, in the container's order.

func (*File) Unsupported

func (f *File) Unsupported(track int) error

Unsupported is the error for a track whose codec the caller cannot decode, naming the codec and the file.

type Format

type Format uint8

Format is a container format.

const (
	MP4 Format = iota + 1
	Matroska
	WebM
)

The formats Open reads.

func (Format) String

func (f Format) String() string

type Kind

type Kind uint8

Kind is what a track carries.

const (
	Other Kind = iota
	Video
	Audio
	Subtitle
)

The kinds of track.

func (Kind) String

func (k Kind) String() string

type Packet

type Packet struct {
	Track int
	// PTS is when the packet's first frame is presented. Packets come
	// in decode order, so reordered video's PTS do not increase
	// monotonically.
	PTS time.Duration
	// Duration is how long the packet presents; zero when the
	// container does not say.
	Duration time.Duration
	// Key is set on packets a decoder can start at.
	Key bool
	// Data is the packet as the container stores it: length-prefixed
	// NAL units for H.264 and H.265 (the prefix size is in Config).
	// It is valid until the next ReadPacket or Seek.
	Data []byte
}

Packet is one access unit or audio frame of a track.

type Track

type Track struct {
	// Index is the track's position in Tracks, which Packet.Track
	// and Select name it by.
	Index int
	Kind  Kind
	Codec Codec
	// CodecName is the container's own name for the codec: an MP4
	// sample entry type ("avc1") or a Matroska codec ID
	// ("V_MPEG4/ISO/AVC").
	CodecName string
	// Config is the codec's configuration record: avcC for H.264,
	// hvcC for H.265, av1C for AV1, vpcC for VP9 in MP4 (empty in
	// Matroska), an OpusHead for Opus (built from dOps in MP4), the
	// Xiph-laced identification, comment and setup headers for Vorbis
	// (see XiphHeaders), the AudioSpecificConfig for AAC and the
	// FLAC stream info for FLAC.
	Config []byte
	// Width and Height are a video track's coded frame size.
	Width, Height int
	// SampleRate and Channels describe an audio track.
	SampleRate, Channels int
	// Duration is the track's length; zero when the file does not
	// say.
	Duration time.Duration
	// CodecDelay is the decoder output to drop at the stream's start
	// as the container states it: an MP4 edit list's media time (AAC
	// encoder priming, Opus pre-skip) or a Matroska CodecDelay. Packet
	// timestamps have it subtracted, so the first packet of such a
	// track starts before zero. An Opus track whose container states
	// none still has its pre-skip in the OpusHead.
	CodecDelay time.Duration
	// SeekPreRoll is how long before a seek target the decoder has
	// to start to converge (80 ms for Opus).
	SeekPreRoll time.Duration
}

Track is one stream of a file.

type UnsupportedCodecError

type UnsupportedCodecError struct {
	File  string
	Track int
	Codec string
}

UnsupportedCodecError is a track whose codec has no decoder.

func (*UnsupportedCodecError) Error

func (e *UnsupportedCodecError) Error() string

Jump to

Keyboard shortcuts

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