mp4

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 4 Imported by: 0

README

mp4

Pure Go implementation of ISO Base Media File Format (ISO/IEC 14496-12) box codec.

Documentation

Overview

Package mp4 implements encoding and decoding of ISO Base Media File Format (MP4) boxes.

Index

Constants

This section is empty.

Variables

View Source
var (
	TypeFtyp = newBoxType("ftyp")
	TypeMoov = newBoxType("moov")
	TypeMvhd = newBoxType("mvhd")
	TypeTrak = newBoxType("trak")
	TypeTkhd = newBoxType("tkhd")
	TypeTref = newBoxType("tref")
	TypeTrgr = newBoxType("trgr")
	TypeEdts = newBoxType("edts")
	TypeElst = newBoxType("elst")
	TypeMdia = newBoxType("mdia")
	TypeMdhd = newBoxType("mdhd")
	TypeHdlr = newBoxType("hdlr")
	TypeElng = newBoxType("elng")
	TypeMinf = newBoxType("minf")
	TypeVmhd = newBoxType("vmhd")
	TypeSmhd = newBoxType("smhd")
	TypeHmhd = newBoxType("hmhd")
	TypeSthd = newBoxType("sthd")
	TypeNmhd = newBoxType("nmhd")
	TypeDinf = newBoxType("dinf")
	TypeDref = newBoxType("dref")
	TypeStbl = newBoxType("stbl")
	TypeStsd = newBoxType("stsd")
	TypeStts = newBoxType("stts")
	TypeCtts = newBoxType("ctts")
	TypeCslg = newBoxType("cslg")
	TypeStsc = newBoxType("stsc")
	TypeStsz = newBoxType("stsz")
	TypeStz2 = newBoxType("stz2")
	TypeStco = newBoxType("stco")
	TypeCo64 = newBoxType("co64")
	TypeStss = newBoxType("stss")
	TypeStsh = newBoxType("stsh")
	TypePadb = newBoxType("padb")
	TypeStdp = newBoxType("stdp")
	TypeSdtp = newBoxType("sdtp")
	TypeSbgp = newBoxType("sbgp")
	TypeSgpd = newBoxType("sgpd")
	TypeSubs = newBoxType("subs")
	TypeSaiz = newBoxType("saiz")
	TypeSaio = newBoxType("saio")
	TypeMvex = newBoxType("mvex")
	TypeMehd = newBoxType("mehd")
	TypeTrex = newBoxType("trex")
	TypeLeva = newBoxType("leva")
	TypeMoof = newBoxType("moof")
	TypeMfhd = newBoxType("mfhd")
	TypeTraf = newBoxType("traf")
	TypeTfhd = newBoxType("tfhd")
	TypeTfdt = newBoxType("tfdt")
	TypeTrun = newBoxType("trun")
	TypeMeta = newBoxType("meta")
	TypeUdta = newBoxType("udta")
	TypeMdat = newBoxType("mdat")
	TypeAvc1 = newBoxType("avc1")
	TypeAvcC = newBoxType("avcC")
	TypeMp4a = newBoxType("mp4a")
	TypeEsds = newBoxType("esds")
)

Known box types.

Functions

func Encode

func Encode(box *Box, buf []byte, offset int) (int, error)

Encode encodes the box into buf starting at offset. Returns the number of bytes written.

func EncodeToBuf

func EncodeToBuf(box *Box, buf []byte) ([]byte, error)

EncodeToBuf encodes the box into the provided buffer slice. The buffer will be grown if needed. Returns the slice containing the encoded data.

func EncodeToBytes

func EncodeToBytes(box *Box) ([]byte, error)

EncodeToBytes is a convenience that allocates a buffer and encodes the box.

func EncodingLength

func EncodingLength(box *Box) uint64

EncodingLength computes the total encoded size of the box; populates Size fields.

Types

type AudioSampleEntry

type AudioSampleEntry struct {
	DataReferenceIndex uint16
	ChannelCount       uint16
	SampleSize         uint16
	SampleRate         uint32
	Children           []*Box
}

AudioSampleEntry represents an audio sample entry (e.g. mp4a).

type AvcC

type AvcC struct {
	MimeCodec string
	Buffer    []byte
}

AvcC represents the AVC configuration box.

type Box

type Box struct {
	Type       BoxType
	Size       uint64 // total size including header
	Version    uint8
	Flags      uint32
	HasFullBox bool

	// Container children (keyed by type). For array children, use Children.
	Children map[BoxType][]*Box
	// OtherBoxes holds children not recognized by the container definition.
	OtherBoxes []*Box

	// Raw buffer for unknown box types.
	Buffer []byte

	// Typed payload: only one of these is non-nil for leaf boxes.
	Ftyp   *Ftyp
	Mvhd   *Mvhd
	Tkhd   *Tkhd
	Mdhd   *Mdhd
	Vmhd   *Vmhd
	Smhd   *Smhd
	Stsd   *Stsd
	Stsz   *Stsz
	Stco   *Stco // also used for stss
	Co64   *Co64
	Stts   *Stts
	Ctts   *Ctts
	Stsc   *Stsc
	Dref   *DrefBox
	Elst   *Elst
	Hdlr   *Hdlr
	Mehd   *Mehd
	Trex   *Trex
	Mfhd   *Mfhd
	Tfhd   *Tfhd
	Tfdt   *Tfdt
	Trun   *Trun
	Mdat   *Mdat
	AvcC   *AvcC
	Visual *VisualSampleEntry
	Audio  *AudioSampleEntry
	Esds   *Esds
}

Box represents an MP4 box (atom).

func Decode

func Decode(buf []byte, start, end int) (*Box, error)

Decode decodes a box from buf[start:end].

func (*Box) Child

func (b *Box) Child(t BoxType) *Box

Child returns the first child box of the given type, or nil.

func (*Box) ChildList

func (b *Box) ChildList(t BoxType) []*Box

ChildList returns all child boxes of the given type.

type BoxType

type BoxType [4]byte

BoxType is a 4-byte box type identifier.

func (BoxType) String

func (t BoxType) String() string

type CTTSEntry

type CTTSEntry struct {
	Count             uint32
	CompositionOffset int32
}

CTTSEntry is a composition offset entry.

type Co64

type Co64 struct {
	Entries []uint64
}

Co64 represents the 64-bit chunk offset box.

type Ctts

type Ctts struct {
	Entries []CTTSEntry
}

Ctts represents the composition offset box.

type DrefBox

type DrefBox struct {
	Entries []DrefEntry
}

DrefBox represents the data reference box.

type DrefEntry

type DrefEntry struct {
	Type [4]byte
	Buf  []byte
}

DrefEntry is a data reference entry.

type Elst

type Elst struct {
	Entries []ElstEntry
}

Elst represents the edit list box.

type ElstEntry

type ElstEntry struct {
	TrackDuration uint32
	MediaTime     int32
	MediaRate     [4]byte // raw fixed 16.16
}

ElstEntry is an edit list entry.

type Esds

type Esds struct {
	MimeCodec string
	Buffer    []byte
}

Esds represents the elementary stream descriptor box.

type Ftyp

type Ftyp struct {
	Brand            [4]byte
	BrandVersion     uint32
	CompatibleBrands [][4]byte
}

Ftyp represents the file type box.

type Hdlr

type Hdlr struct {
	HandlerType [4]byte
	Name        string
}

Hdlr represents the handler reference box.

type Headers

type Headers struct {
	Size       uint64
	HeaderSize int
	ContentLen int
	Type       BoxType
	Version    uint8
	Flags      uint32
}

Headers holds parsed box header information.

func ReadHeaders

func ReadHeaders(buf []byte, start, end int) (Headers, error)

ReadHeaders parses box headers from buf[start:end]. Returns the headers and nil error, or if not enough data, returns an error.

type Mdat

type Mdat struct {
	Buffer        []byte
	ContentLength int
}

Mdat represents the media data box.

type Mdhd

type Mdhd struct {
	CTime     [8]byte // 4 or 8 bytes depending on version
	MTime     [8]byte
	TimeScale uint32
	Duration  uint64
	Language  uint16
	Quality   uint16
	V1        bool // true if version 1
}

Mdhd represents the media header box.

type Mehd

type Mehd struct {
	FragmentDuration uint32
}

Mehd represents the movie extends header box.

type Mfhd

type Mfhd struct {
	SequenceNumber uint32
}

Mfhd represents the movie fragment header box.

type Mvhd

type Mvhd struct {
	CTime             [4]byte // raw 4-byte date
	MTime             [4]byte
	TimeScale         uint32
	Duration          uint32
	PreferredRate     [4]byte // raw 16.16 fixed
	PreferredVolume   [2]byte // raw 8.8 fixed
	Matrix            [36]byte
	PreviewTime       uint32
	PreviewDuration   uint32
	PosterTime        uint32
	SelectionTime     uint32
	SelectionDuration uint32
	CurrentTime       uint32
	NextTrackId       uint32
}

Mvhd represents the movie header box.

type STSCEntry

type STSCEntry struct {
	FirstChunk          uint32
	SamplesPerChunk     uint32
	SampleDescriptionId uint32
}

STSCEntry is a sample-to-chunk entry.

type STTSEntry

type STTSEntry struct {
	Count    uint32
	Duration uint32
}

STTSEntry is a time-to-sample entry.

type Smhd

type Smhd struct {
	Balance uint16
}

Smhd represents the sound media header box.

type Stco

type Stco struct {
	Entries []uint32
}

Stco represents the chunk offset box (also used for stss).

type Stsc

type Stsc struct {
	Entries []STSCEntry
}

Stsc represents the sample-to-chunk box.

type Stsd

type Stsd struct {
	Entries []*Box
}

Stsd represents the sample description box.

type Stsz

type Stsz struct {
	SampleSize uint32
	Entries    []uint32
}

Stsz represents the sample size box.

type Stts

type Stts struct {
	Entries []STTSEntry
}

Stts represents the time-to-sample box.

type Tfdt

type Tfdt struct {
	BaseMediaDecodeTime uint32
}

Tfdt represents the track fragment decode time box.

type Tfhd

type Tfhd struct {
	TrackId uint32
}

Tfhd represents the track fragment header box.

type Tkhd

type Tkhd struct {
	CTime          [4]byte
	MTime          [4]byte
	TrackId        uint32
	Duration       uint32
	Layer          uint16
	AlternateGroup uint16
	Volume         uint16
	Matrix         [36]byte
	TrackWidth     uint32
	TrackHeight    uint32
}

Tkhd represents the track header box.

type Trex

type Trex struct {
	TrackId                       uint32
	DefaultSampleDescriptionIndex uint32
	DefaultSampleDuration         uint32
	DefaultSampleSize             uint32
	DefaultSampleFlags            uint32
}

Trex represents the track extends box.

type Trun

type Trun struct {
	DataOffset int32
	Entries    []TrunEntry
}

Trun represents the track run box.

type TrunEntry

type TrunEntry struct {
	SampleDuration              uint32
	SampleSize                  uint32
	SampleFlags                 uint32
	SampleCompositionTimeOffset int32
}

TrunEntry is a track run entry.

type VisualSampleEntry

type VisualSampleEntry struct {
	DataReferenceIndex uint16
	Width              uint16
	Height             uint16
	HResolution        uint32
	VResolution        uint32
	FrameCount         uint16
	CompressorName     string
	Depth              uint16
	Children           []*Box
}

VisualSampleEntry represents a visual sample entry (e.g. avc1).

type Vmhd

type Vmhd struct {
	GraphicsMode uint16
	Opcolor      [3]uint16
}

Vmhd represents the video media header box.

Directories

Path Synopsis
cmd
mp4dump command
Command mp4dump reads an MP4 file and prints its box structure.
Command mp4dump reads an MP4 file and prints its box structure.

Jump to

Keyboard shortcuts

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