mux

package
v0.3.13 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 9 Imported by: 1 module, 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 mux writes timestamped H.264, H.265 and Opus samples into fragmented MP4 (NewMP4) and Matroska (NewMKV) files, in pure Go.

Video samples are Annex-B access units without B-frames, so presentation and decode order agree; audio samples are single Opus packets at 48 kHz. Every sample's duration runs to the next one's PTS on the same track, and the last repeats the one before it. Both muxers write whole fragments or clusters at a time, so a file cut short by a crash still plays up to the last one written.

m, err := mux.NewMP4(f, mux.H264{SPS: sps, PPS: pps}, mux.Opus{Channels: 2, PreSkip: 312})
err = m.WriteSample(mux.Sample{Track: 0, PTS: pts, Data: accessUnit})
err = m.Close()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type H264

type H264 struct{ SPS, PPS []byte }

H264 is an H.264 video track. SPS and PPS are the parameter sets as raw NAL units (header byte included, no start code); the frame size comes from the SPS.

type H265

type H265 struct{ VPS, SPS, PPS []byte }

H265 is an H.265 video track. VPS, SPS and PPS are the parameter sets as raw NAL units (two-byte header included, no start code); the frame size comes from the SPS.

type MKV

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

MKV writes a Matroska file: the EBML header, then a Segment with a SeekHead, Info and Tracks, one Cluster of SimpleBlocks per stretch between video keyframes (at most a second), and Cues at Close. Each Cluster is written whole with a single Write. When w is an io.WriteSeeker, Close also patches the Segment size, the SeekHead's entry for the Cues and the Duration; otherwise the Segment keeps an unknown size.

func NewMKV

func NewMKV(w io.Writer, tracks ...Track) (*MKV, error)

NewMKV writes the file header for tracks to w and returns the muxer. Close does not close w.

func (*MKV) Close

func (m *MKV) Close() error

Close writes the last cluster and the Cues and, when the writer can seek, patches the header and returns the writer to the file's end.

func (*MKV) WriteSample

func (m *MKV) WriteSample(s Sample) error

WriteSample adds s to the current cluster, first writing the cluster out when s starts a new one. After an error the muxer only returns that error.

type MP4

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

MP4 writes a fragmented ISO BMFF file: ftyp and a moov describing the tracks up front, then one moof and mdat per fragment. A fragment closes before each video keyframe and at least once a second, and is written with a single Write.

func NewMP4

func NewMP4(w io.Writer, tracks ...Track) (*MP4, error)

NewMP4 writes the file header for tracks to w and returns the muxer. Close does not close w.

func (*MP4) Close

func (m *MP4) Close() error

Close gives each track's last sample the duration of the one before it and writes the final fragment.

func (*MP4) WriteSample

func (m *MP4) WriteSample(s Sample) error

WriteSample adds s to the current fragment, first writing the fragment out when s starts a new one. After an error the muxer only returns that error.

type Opus

type Opus struct {
	Channels uint8
	PreSkip  uint16
}

Opus is a 48 kHz Opus track with channel mapping family 0. PreSkip is the encoder's priming in 48 kHz samples, dropped on playback.

type Sample

type Sample struct {
	Track int
	PTS   time.Duration
	Data  []byte
}

Sample is one access unit or packet. Track indexes the tracks the muxer was made with. PTS is the presentation time on a clock shared by all tracks, starting at or after zero, strictly increasing within a track; an Opus packet's PTS counts its pre-skip samples too. Data is an Annex-B access unit for video (in-band parameter sets must match the track's and are dropped, as are access unit delimiters) and one Opus packet for audio. The muxer copies Data.

type Track

type Track interface {
	// contains filtered or unexported methods
}

Track describes one stream of a file: H264, H265 or Opus.

Jump to

Keyboard shortcuts

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