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 ¶
- Variables
- func XiphHeaders(config []byte) (id, comment, setup []byte, err error)
- type Codec
- type File
- func (f *File) Close() error
- func (f *File) Duration() time.Duration
- func (f *File) Format() Format
- func (f *File) Name() string
- func (f *File) ReadPacket() (Packet, error)
- func (f *File) Seek(t time.Duration) (time.Duration, error)
- func (f *File) Select(tracks ...int) error
- func (f *File) Tracks() []Track
- func (f *File) Unsupported(track int) error
- type Format
- type Kind
- type Packet
- type Track
- type UnsupportedCodecError
Constants ¶
This section is empty.
Variables ¶
var ErrMalformed = errors.New("malformed file")
ErrMalformed is a file whose structure is invalid; errors wrap it with what was being read and where.
var ErrUnknownFormat = errors.New("not an MP4 or Matroska file")
ErrUnknownFormat is a file that is neither MP4 nor Matroska.
Functions ¶
func XiphHeaders ¶
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.
The codecs the demuxer recognises. Unknown is any other, named by the track's CodecName.
type File ¶
type File struct {
// contains filtered or unexported fields
}
File is an open media file. A File belongs to one goroutine.
func (*File) ReadPacket ¶
ReadPacket returns the next packet of the selected tracks in file order, or io.EOF after the last.
func (*File) Seek ¶
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 ¶
Select limits ReadPacket to the tracks given; the others' data is not read. Every track is selected after Open.
func (*File) Unsupported ¶
Unsupported is the error for a track whose codec the caller cannot decode, naming the codec and the file.
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 ¶
UnsupportedCodecError is a track whose codec has no decoder.
func (*UnsupportedCodecError) Error ¶
func (e *UnsupportedCodecError) Error() string