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 ¶
- Variables
- func Descriptor() format.Descriptor
- func Register(registry *format.Registry)
- type Demuxer
- type DemuxerFactory
- type FormatDemuxer
- func (d *FormatDemuxer) Close() error
- func (d *FormatDemuxer) Format() av.FormatID
- func (d *FormatDemuxer) Open(ctx context.Context, input format.Input, _ format.OpenOptions) error
- func (d *FormatDemuxer) ReadInto(ctx context.Context, out *format.ReadResult) error
- func (d *FormatDemuxer) Seek(ctx context.Context, position time.Duration) error
- func (d *FormatDemuxer) Streams() []av.Stream
- type FormatMuxer
- type MuxerFactory
- type Sample
- type Track
Constants ¶
This section is empty.
Variables ¶
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
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 ¶
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).
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 (d *FormatDemuxer) Open(ctx context.Context, input format.Input, _ format.OpenOptions) error
func (*FormatDemuxer) ReadInto ¶
func (d *FormatDemuxer) ReadInto(ctx context.Context, out *format.ReadResult) error
func (*FormatDemuxer) Seek ¶
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.