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 ¶
NewMKV writes the file header for tracks to w and returns the muxer. Close does not close w.
func (*MKV) Close ¶
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 ¶
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 ¶
NewMP4 writes the file header for tracks to w and returns the muxer. Close does not close w.
func (*MP4) Close ¶
Close gives each track's last sample the duration of the one before it and writes the final fragment.
func (*MP4) WriteSample ¶
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 ¶
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 ¶
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.