mp4

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 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 writes ISO base media files (MP4, QuickTime): a video track of HEVC or MV-HEVC with Apple's stereo signalling, and audio tracks of AC-3 or E-AC-3, from the same sources the Matroska muxer takes.

Index

Constants

View Source
const LiveTimescale = 90000

LiveTimescale is the stream's timescale: the 90 kHz of its timestamps.

Variables

This section is empty.

Functions

func AVCC added in v0.21.0

func AVCC(sps, pps []byte) []byte

AVCC is an avcC box's body for an SPS and a PPS (NAL units without start codes): a decoder's configuration.

func AVCCodec added in v0.20.0

func AVCCodec(sps []byte) string

AVCCodec is the codec string a browser wants for an H.264 stream (avc1.PPCCLL: the SPS's profile, constraints and level).

func AVCSample added in v0.21.0

func AVCSample(au []byte) []byte

AVCSample is an H.264 access unit (Annex B) as an MP4 sample: its NAL units each after its length (4 bytes), the parameter sets left out (an avcC carries them).

func MovieAt added in v0.13.0

func MovieAt(r io.ReaderAt, size int64) ([]byte, error)

MovieAt finds and reads the movie box of a file of size bytes through r, reading only box headers on the way: with the movie box at the end (after the media data), the file's first and last pieces are enough.

func Mux

func Mux(f io.WriteSeeker, sources []mkv.Source, o Options) error

Mux writes the sources (the video first, then audio tracks) to f, in decode order interleaved by time; the movie header goes last, so f is written once through, then its media data box is sized.

func SplitAnnexB added in v0.20.0

func SplitAnnexB(au []byte) [][]byte

SplitAnnexB is the NAL units of an Annex B access unit, without their start codes.

Types

type Live added in v0.20.0

type Live struct {
	W io.Writer
	// contains filtered or unexported fields
}

Live writes one H.264 video stream as a fragmented MP4 as its frames are made, for a browser's media source (MSE) reading it live: the movie header once (Init), then a fragment a frame (moof and mdat), each decoded as it arrives (no B-frames). The frames are Annex B access units; their parameter sets go into the avcC, not the samples.

func (*Live) Frame added in v0.20.0

func (l *Live) Frame(au []byte, pts int64, key bool) error

Frame writes an access unit (Annex B) shown, and decoded, at pts (90 kHz), key when it is a keyframe. Its parameter sets are left out (the header carries them).

func (*Live) Init added in v0.20.0

func (l *Live) Init(sps, pps []byte, w, h int) error

Init writes the file's header for a picture w x h whose parameter sets are sps and pps (NAL units, without start codes).

type Options

type Options struct {
	// FPSNum and FPSDen are the video's frame rate, which sets its
	// timescale (24000/1001 for film).
	FPSNum, FPSDen int
	// Spatial, when set, marks the video track as a stereo pair.
	Spatial *Spatial
	// QuickTime writes a QuickTime (.mov) brand instead of MP4's.
	QuickTime bool
}

Options for Mux.

type Reader added in v0.13.0

type Reader struct {
	Tracks   []mkv.ReadTrack
	Duration time.Duration
	// LayerParams is, for an MV-HEVC track, its second layer's parameter
	// sets (lhvC) as Annex B NAL units.
	LayerParams map[uint64][]byte
	// Stereo is, for a video track Apple's stereo boxes describe, what
	// they say: "both" when vexu's eyes box names both eyes, "one" when it
	// names one.
	Stereo map[uint64]string
	// contains filtered or unexported fields
}

Reader reads an MP4 or QuickTime file's samples in file order, each as a Matroska-style packet, its tracks described as a Matroska reader describes them (codec ids, avcC or hvcC as the codec private data), so the conversion takes either alike.

Video: H.264 (avc1, avc3), HEVC (hvc1, hev1; MV-HEVC's second layer with it, lhvC), Dolby Vision's configuration. Audio: AC-3, E-AC-3, AAC (described, for the caller to leave or code), and PCM (sowt, twos, in24, in32, lpcm), given little-endian, a chunk a packet. The edit list's start is kept: a track that starts late does.

func NewReader added in v0.13.0

func NewReader(f io.ReadSeeker) (*Reader, error)

NewReader reads the file's movie box.

func ParseMovie added in v0.13.0

func ParseMovie(moov []byte) (*Reader, error)

ParseMovie describes a file from its movie box alone: its tracks, with no samples to read (Next fails).

func (*Reader) Next added in v0.13.0

func (r *Reader) Next() (mkv.Packet, error)

Next returns the next sample in file order, its time the presentation time (the edit list applied).

func (*Reader) Resync added in v0.14.0

func (r *Reader) Resync(_ io.ReadSeeker, off int64) error

Resync goes on reading at the first sample stored at or after byte off of the file: samples come from there on, in file order. It is for reading parts of a file; r is the file NewReader read.

func (*Reader) Track added in v0.13.0

func (r *Reader) Track(n uint64) *mkv.ReadTrack

Track is the track numbered n, nil if there is none.

type Spatial

type Spatial struct {
	// FOV is the horizontal field of view of each eye's picture, in
	// degrees (an iPhone's spatial video says 63.4, Vision Pro's 71.6).
	FOV float64
	// Baseline is the distance between the two cameras, in millimetres
	// (an iPhone's 19.24, Vision Pro's 63.76).
	Baseline float64
	// Disparity is a shift of the two views, -1 to 1 of the picture's
	// width, 0 for none.
	Disparity float64
	// HeroRight makes the right eye the hero (the one shown in 2D); the
	// left is the base layer and the hero otherwise.
	HeroRight bool
}

Spatial is what a stereo (MV-HEVC) video track says of itself to a player: Apple's video extended usage, which visionOS and the Quest read to show the two views to the two eyes. Photos marks a file "spatial" only with a field of view and a baseline or disparity adjustment.

Jump to

Keyboard shortcuts

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