mkv

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: 12 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 mkv writes Matroska files: the muxer behind bdtools' default output, taking an encoder's raw H.264 or HEVC stream and the audio and subtitle tracks a Blu-ray demux produces.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ADTSHeader added in v0.14.0

func ADTSHeader(asc []byte, n int) []byte

ADTSHeader makes an ADTS header for a raw AAC frame of n bytes with the AudioSpecificConfig asc (object type, rate index, channels from its first 13 bits).

func FourCC added in v0.7.0

func FourCC(s string) uint32

FourCC is a four-character code as a BlockAddIDType.

func HasCore

func HasCore(r io.Reader, format AudioFormat) bool

HasCore reports whether the stream carries a separate lossy core: the AC-3 frames of a TrueHD stream, the core of DTS-HD. (The AC-3 part of a 7.1 E-AC-3 track is not one: it is half of each E-AC-3 frame, and stays.)

func Mux

func Mux(w io.WriteSeeker, sources []Source, opts Options) error

Mux writes a Matroska file from the sources, interleaving their frames by decode time.

Types

type AudioFormat

type AudioFormat int

AudioFormat is the content of a demuxed audio file.

const (
	AC3    AudioFormat = iota // AC-3, or E-AC-3: whichever the frames are
	TrueHD                    // TrueHD access units, possibly with an AC-3 core interleaved
	DTS                       // DTS, or DTS-HD: a core frame then its extension substream
	WAV                       // PCM in a WAV file
	AAC                       // AAC in ADTS frames (given raw, the configuration in the codec private data)
)

type AudioSource

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

AudioSource reads a demuxed audio file frame by frame. A Blu-ray stream can carry a lossy core next to (or inside) its main coding — the AC-3 frames between TrueHD's access units, the AC-3 independent substream of a 7.1 E-AC-3 track, the DTS core of DTS-HD — and Core selects which of the two this source produces.

func NewAudioSource

func NewAudioSource(r io.Reader, format AudioFormat, core bool, lang string) (*AudioSource, error)

NewAudioSource probes the file. lang is the track's ISO 639-2 language.

func (*AudioSource) Delay

func (a *AudioSource) Delay() time.Duration

Delay is when the track starts, if the source starts it after the picture; Gaps is how far each later move to the source's timestamps shifted it.

func (*AudioSource) Gaps

func (a *AudioSource) Gaps() []time.Duration

Gaps reports the gaps in the source the track was moved across.

func (*AudioSource) Next

func (a *AudioSource) Next() (Frame, error)

Next returns the next frame.

func (*AudioSource) SampleStart added in v0.12.0

func (a *AudioSource) SampleStart() int64

SampleStart is how many samples came before the frame Next last gave, counted over the whole file (across any gap): where a decoder of the file, which knows nothing of the gaps, has it.

func (*AudioSource) SetDefault

func (a *AudioSource) SetDefault(d bool)

SetDefault marks the track as the one a player picks by default.

func (*AudioSource) SetName

func (a *AudioSource) SetName(n string)

SetName names the track.

func (*AudioSource) SetSyncPoints

func (a *AudioSource) SetSyncPoints(sp []SyncPoint)

SetSyncPoints gives the source's timestamps, in file order. Without them the track starts at zero and runs by its sample count.

func (*AudioSource) Track

func (a *AudioSource) Track() Track

Track describes the track.

type BlockAddition added in v0.7.0

type BlockAddition struct {
	Name      string
	Type      uint32
	ExtraData []byte
}

BlockAddition is a track's BlockAdditionMapping: what a kind of data alongside its frames is. Dolby Vision's names its configuration record: Type is the record's four-character code (dvcC or dvvC) and ExtraData the record.

type Chapter

type Chapter struct {
	Start time.Duration
	Name  string
}

Chapter is a chapter start, named.

type Codec

type Codec int

Codec of an elementary video stream.

const (
	H264 Codec = iota
	HEVC
	AV1
)

func (Codec) String

func (c Codec) String() string

String names the codec.

type Colour added in v0.7.0

type Colour struct {
	Matrix, Transfer, Primaries int
	FullRange                   bool
	Static                      hdr.Static
}

Colour is a video track's colour signalling (H.273 code points: 2 is unspecified) and its HDR10 metadata.

type Frame

type Frame struct {
	// PTS is the presentation time.
	PTS time.Duration
	// Order is when the frame goes into the file, relative to the other
	// tracks: its decode time. Equal to PTS for anything without frame
	// reordering.
	Order    time.Duration
	Keyframe bool
	Data     []byte
	// Duration, when set, is the frame's time on screen: a subtitle's,
	// written as its block's BlockDuration.
	Duration time.Duration
}

Frame is one block of a track.

type HEVCOrder added in v0.8.6

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

HEVCOrder puts an HEVC stream's access units, met in decoding order, in display order: Key gives each one a number that sorts it there, its picture order count past every earlier coded video sequence's. Decoders that give pictures back in the order of the timestamps they were given (VideoToolbox) need such timestamps.

func (*HEVCOrder) Key added in v0.8.6

func (o *HEVCOrder) Key(au []byte) (int64, error)

Key is an Annex B access unit's display key.

type Options

type Options struct {
	Chapters   []Chapter
	WritingApp string
	// Progress, when set, is called now and then with the time muxed so far.
	Progress func(time.Duration)
	// Spans, when set, receives each track's first and last presentation
	// time (the last frame's start), in source order, once the mux is done.
	Spans *[]Span
}

Options for Mux.

type PGSSource

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

PGSSource reads a .sup file — each presentation graphics segment behind "PG" and its PTS and DTS — and gives one frame per display set: the segments from a presentation composition to its end segment, at the composition's time.

func NewPGSSource

func NewPGSSource(r io.Reader, lang string) *PGSSource

NewPGSSource reads a .sup file.

func (*PGSSource) Next

func (p *PGSSource) Next() (Frame, error)

Next returns the next display set.

func (*PGSSource) SetDefault

func (p *PGSSource) SetDefault(d bool)

SetDefault marks the track the one a player picks among the subtitles.

func (*PGSSource) SetForced

func (p *PGSSource) SetForced(f bool)

SetForced marks the track forced: shown even with subtitles off, as for dialogue in another language.

func (*PGSSource) SetName

func (p *PGSSource) SetName(n string)

SetName names the track.

func (*PGSSource) Track

func (p *PGSSource) Track() Track

Track describes the track.

type Packet

type Packet struct {
	Track uint64
	Time  time.Duration // the block's timestamp: the presentation time
	// Duration is the block's, when it states one (a subtitle's time on
	// screen); 0 when it does not.
	Duration time.Duration
	Keyframe bool
	Data     []byte
}

Packet is one frame of a track.

type ReadTrack

type ReadTrack struct {
	Number       uint64
	Type         TrackType
	CodecID      string
	CodecPrivate []byte
	// Language is the ISO 639-2 code; Matroska's default, when the file
	// says nothing, is English.
	Language string
	// LanguageBCP47 is the IETF tag, when the file has one.
	LanguageBCP47 string
	Name          string
	Default       bool
	Forced        bool
	Commentary    bool // FlagCommentary
	// Video.
	Width, Height int
	StereoMode    int
	// DisplayWidth and DisplayHeight are zero when the file does not say.
	DisplayWidth, DisplayHeight int
	// DefaultDuration is a frame's duration, zero when the file does not
	// say.
	DefaultDuration time.Duration
	// BitsPerChannel is the Colour element's bit depth, zero when the file
	// does not say.
	BitsPerChannel int
	// Transfer is the Colour element's transfer characteristics (H.273: 16
	// is PQ, 18 HLG), zero when the file does not say.
	Transfer int
	// BlockAdditions are the track's BlockAdditionMappings (Dolby Vision's
	// configuration).
	BlockAdditions []BlockAddition
	// Audio.
	SampleRate float64
	Channels   int
	BitDepth   int
	// contains filtered or unexported fields
}

ReadTrack is a track of a Matroska file being read.

type Reader

type Reader struct {
	Tracks   []ReadTrack
	Chapters []time.Duration
	Duration time.Duration
	// contains filtered or unexported fields
}

Reader reads a Matroska file's blocks in order.

func NewReader

func NewReader(r io.Reader) (*Reader, error)

NewReader reads the file's headers: the EBML header, then the segment's info, tracks and chapters, up to the first cluster.

func (*Reader) Next

func (m *Reader) Next() (Packet, error)

Next returns the next frame in file order; io.EOF ends the file.

func (*Reader) Resync added in v0.14.0

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

Resync goes on reading at the first cluster that starts at or after byte off of r, the file NewReader read: frames come from there on, in file order. It is for reading parts of a file, looking at its pictures here and there, without reading what lies between.

func (*Reader) Track

func (m *Reader) Track(n uint64) *ReadTrack

Track returns the track with a number, or nil.

type Source

type Source interface {
	Track() Track
	Next() (Frame, error)
}

Source produces a track's frames in decode order; io.EOF ends it.

type Span

type Span struct {
	First, Last time.Duration
	Frames      int
}

Span is when a track's frames start and end.

type SyncPoint

type SyncPoint struct {
	Offset int64
	At     time.Duration
}

SyncPoint says when the byte at Offset of a demuxed audio file is presented, on the output's timeline.

type Track

type Track struct {
	Type         TrackType
	CodecID      string // "V_MPEG4/ISO/AVC", "A_TRUEHD", "S_HDMV/PGS"
	CodecPrivate []byte
	Language     string // ISO 639-2; empty writes "und"
	Name         string
	Default      bool
	Forced       bool
	// DefaultDuration is the duration of a frame, for constant-rate tracks.
	DefaultDuration time.Duration
	// Video.
	Width, Height int
	// StereoMode is the Matroska stereo layout: 1 is side by side, left
	// eye first.
	StereoMode int
	// Colour, when set, is the video's colour signalling and HDR metadata,
	// written as Matroska's Colour element.
	Colour *Colour
	// DisplayWidth and DisplayHeight, when set, are the shape a frame is
	// shown at (zero leaves Matroska's default, the pixel size). For a
	// stereo frame, players built on ffmpeg (Kodi, mpv) and mkvmerge take
	// them as one eye's shape: see SetDisplaySize.
	DisplayWidth, DisplayHeight int
	// BlockAdditions describe data the track's frames carry for a purpose
	// of their own: Dolby Vision's configuration.
	BlockAdditions []BlockAddition
	// Audio.
	SampleRate int
	Channels   int
	BitDepth   int
}

Track describes a track's header.

type TrackType

type TrackType int

TrackType is a Matroska track type.

const (
	TypeVideo    TrackType = 1
	TypeAudio    TrackType = 2
	TypeSubtitle TrackType = 17
)

type VideoSource

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

VideoSource reads an encoder's raw Annex B output — in decode order, with no timestamps — and gives each frame its presentation time from its picture order count: within a coded video sequence, display order is POC order. The frames are passed through in decode order, NAL units length-prefixed as Matroska stores them, access unit delimiters dropped and the parameter sets kept in-band as well as in the codec private data.

func NewVideoSource

func NewVideoSource(r io.Reader, codec Codec, fpsNum, fpsDen int, stereoMode int) (*VideoSource, error)

NewVideoSource reads the first frames to learn the stream's parameters. fps is the frame rate as a fraction.

func (*VideoSource) Next

func (v *VideoSource) Next() (Frame, error)

Next returns the next frame in decode order.

func (*VideoSource) SetBlockAdditions added in v0.7.0

func (v *VideoSource) SetBlockAdditions(a []BlockAddition)

SetBlockAdditions sets the track's BlockAdditionMappings.

func (*VideoSource) SetColour added in v0.7.0

func (v *VideoSource) SetColour(c *Colour)

SetColour sets the track's colour signalling and HDR metadata.

func (*VideoSource) SetDelay

func (v *VideoSource) SetDelay(d time.Duration)

SetDelay starts the picture later: for a source whose first picture comes after the start of what plays (the audio's), as some discs do.

func (*VideoSource) SetDisplaySize

func (v *VideoSource) SetDisplaySize(w, h int)

SetDisplaySize sets the shape a frame is shown at. For a side-by-side frame the convention mkvmerge writes and ffmpeg's demuxer reads is one eye's shape: a full-width pair of 1920x1080 eyes (3840x1080) is 3840x2160, square pixels; a half-width pair squeezed into 1920x1080 is 1920x1080, each eye stretched back to 16:9. Left at the pixel size, ffmpeg takes a full-width pair for 7680x1080, which players then show and size their output for as if it were twice as wide.

func (*VideoSource) SetExtras added in v0.7.0

func (v *VideoSource) SetExtras(extras func(display int64, key bool) [][]byte)

SetExtras has extras give, for each frame by its display index, units to add to it: NAL units (without start codes) ahead of an H.264 or HEVC picture's, or OBUs after an AV1 temporal unit's sequence header. HDR metadata goes in this way, on the frame it belongs to whatever order the encoder put the frames in.

func (*VideoSource) SetTrailer added in v0.7.0

func (v *VideoSource) SetTrailer(trailer func(display int64) ([][]byte, error))

SetTrailer has trailer give, for each frame by its display index (asked in decoding order), NAL units to end it with: Dolby Vision's enhancement layer and RPU go last in its access unit. An error ends the source.

func (*VideoSource) Track

func (v *VideoSource) Track() Track

Track describes the video track.

Jump to

Keyboard shortcuts

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