mp4

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 16 Imported by: ? 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 mp4 is a pure-Go ISO Base Media File Format (ISO/IEC 14496-12) demuxer and muxer for MP4/MOV/M4A/M4V. It registers behind the goav format extension point as the av.FormatMP4 demuxer and muxer.

The muxer writes H.264, HEVC, AV1, VP8, VP9, AAC, Opus, and FLAC tracks. To a seekable output it writes a progressive file, moov after the samples; to a pipe or a realtime output it writes fragmented MP4, a moof and mdat per fragment cut at video keyframes, which plays while it grows. Edit lists keep the tracks in sync and Opus pre-skip and B-frame delay out of presentation, exactly as the demuxer reads them back.

The demuxer is random-access: it walks the top-level boxes to locate moov (which may follow mdat in non-faststart files), parses each track's sample tables into a flat sample index, and reads samples in decode-time order across tracks (ties by file offset). Edit lists are honored as encoders write them: a leading empty edit delays a track and the first media edit starts its presentation (hiding B-frame delay and AAC priming), then every track lifts together so no sample presents before zero. Fragmented (fMP4/CMAF) files are supported too: when a track carries no moov sample table, its samples are read from the moof/traf/ trun fragment runs using the mvex/trex defaults. It needs an io.ReaderAt; a plain io.Reader is buffered in memory as a fallback.

Supported sample entries map to av codec ids: avc1/avc3 (H264), hvc1/hev1 (HEVC), av01 (AV1), vp08/vp09 (VP8/VP9), mp4a (AAC), Opus, and fLaC (FLAC). Codec configuration (avcC, hvcC, av1C, vpcC, esds AudioSpecificConfig, dOps) is exposed as stream ExtraData. Tracks whose codec goav has no id for are skipped, so a file's supported tracks still demux.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilReader   = errors.New("mp4: nil reader")
	ErrNilPacket   = errors.New("mp4: nil packet")
	ErrClosed      = errors.New("mp4: closed")
	ErrInvalidData = errors.New("mp4: invalid data")
	ErrTruncated   = errors.New("mp4: truncated box")
	ErrNoMovie     = errors.New("mp4: no moov box")
	ErrNoTracks    = errors.New("mp4: no readable tracks")
	ErrUnsupported = errors.New("mp4: unsupported")
	// ErrPayloadTooSmall reports that the caller's packet payload buffer cannot
	// hold a sample.
	ErrPayloadTooSmall = errors.New("mp4: packet data capacity too small")
)

Functions

func Descriptor

func Descriptor() format.Descriptor

func Register

func Register(registry *format.SimpleRegistry)

Register installs the MP4 demuxer and muxer for av.FormatMP4. The default prober already detects ISO BMFF (ftyp magic and the .mp4/.mov family of extensions), so no prober is registered here.

Types

type Demuxer

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

Demuxer reads samples from a parsed MP4 in decode-time order across tracks (ties broken by file offset), so files whose chunks are not interleaved by time still deliver audio and video together.

func NewDemuxer

func NewDemuxer(r io.ReaderAt, size int64) (*Demuxer, error)

NewDemuxer parses the movie header and builds the demux order. size is the file size; pass a large value when it is unknown (size-to-end boxes then cannot be bounded).

func (*Demuxer) ReadInto

func (d *Demuxer) ReadInto(dst []byte, out *Sample) error

ReadInto reads the next sample into dst (reusing its capacity, growing only when a sample does not fit) and fills out. It returns io.EOF after the last sample.

func (*Demuxer) Tracks

func (d *Demuxer) Tracks() []Track

Tracks returns the track set in declaration order; the index into this slice is the Sample.TrackIndex ReadInto reports.

type DemuxerFactory

type DemuxerFactory struct{}

DemuxerFactory opens MP4 demuxers behind the goav format extension point.

func (DemuxerFactory) NewDemuxer

func (DemuxerFactory) NewDemuxer(ctx context.Context, result format.ProbeResult) (format.Demuxer, error)

NewDemuxer returns an MP4 demuxer for a probed MP4 result.

type FormatDemuxer

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

FormatDemuxer adapts the random-access MP4 Demuxer to the format.Demuxer interface.

func (*FormatDemuxer) Close

func (d *FormatDemuxer) Close() error

func (*FormatDemuxer) Format

func (d *FormatDemuxer) Format() av.FormatID

func (*FormatDemuxer) Open

func (*FormatDemuxer) ReadInto

func (d *FormatDemuxer) ReadInto(ctx context.Context, out *format.ReadResult) error

func (*FormatDemuxer) Seek

func (d *FormatDemuxer) Seek(ctx context.Context, position time.Duration) error

Seek implements format.Seeker. The sample tables are in memory and every input is random-access (a ReaderAt or a buffered reader), so MP4 inputs are always seekable: the next ReadInto resumes at the latest video keyframe that presents at or before position (the latest sample, for audio-only files), with every track continuing from there in time order.

func (*FormatDemuxer) Streams

func (d *FormatDemuxer) Streams() []av.Stream

type FormatMuxer

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

FormatMuxer writes MP4 (ISO/IEC 14496-12 and -14). To a seekable output it writes a progressive file: ftyp, every sample in one mdat, then moov with the sample tables, patching the mdat size at Close. To any other output, or a realtime one, it writes fragmented MP4: ftyp and a moov without samples, then a moof and mdat per fragment, each fragment starting at a video keyframe, so the file plays while it grows and survives a crash up to its last fragment. Edit lists place each track: a track that starts later is delayed by an empty edit, B-frame and Opus pre-roll stay before presentation, and the earliest track starts at zero.

func (*FormatMuxer) Close

func (m *FormatMuxer) Close() error

Close finalizes the file: the moov of a progressive file, or a fragmented file's last fragment. It does not close the writer.

func (*FormatMuxer) Format

func (m *FormatMuxer) Format() av.FormatID

Format reports av.FormatMP4.

func (*FormatMuxer) Open

func (m *FormatMuxer) Open(ctx context.Context, output format.Output, streams []av.Stream, options format.OpenOptions) error

Open binds the output and the streams, one track each.

func (*FormatMuxer) Write

func (m *FormatMuxer) Write(ctx context.Context, packet *av.Packet, _ *format.WriteResult) error

Write adds one packet as a sample of its stream's track.

type MuxerFactory

type MuxerFactory struct{}

MuxerFactory opens MP4 muxers behind the goav format extension point.

func (MuxerFactory) NewMuxer

func (MuxerFactory) NewMuxer(ctx context.Context, id av.FormatID) (format.Muxer, error)

NewMuxer returns an MP4 muxer.

type Sample

type Sample struct {
	TrackIndex int
	Data       []byte
	DTS        int64
	CTS        int64
	Keyframe   bool
	Timescale  uint32
}

Sample is one demuxed sample. Data is filled into the buffer the caller hands to ReadInto (grown if the sample does not fit), and DTS/CTS are in the track's Timescale.

type Track

type Track struct {
	ID        uint32
	Media     av.MediaType
	Codec     av.CodecParameters
	Timescale uint32
}

Track is one demuxable track's identity and codec parameters.

Jump to

Keyboard shortcuts

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