video

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 17 Imported by: ? 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.

README

Playback events

Options.OnEvent receives playback events on the application's main thread. Use Changed for changes to playback state such as pause, duration, video dimensions, or tracks. Use Progress for position, buffering state, and buffer percentage. Both carry an Event.State snapshot.

Ordinary progress updates are coalesced at a 250 ms interval by default. Set Options.ProgressInterval to a positive duration to set the interval or to a negative duration to disable throttling. A progress snapshot immediately precedes Loaded and Finished. A completed seek also publishes progress immediately. Player.State() returns the latest observed state even between progress events.

When upgrading a handler that updates time or buffering controls on Changed, move those updates to the Progress case. ui.Video schedules picture updates independently, so a page without progress controls can skip Progress in its redraw handler.

Documentation

Overview

Package video provides Experimental, window-independent media playback.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("video: player closed")

ErrClosed is returned by operations on a closed player.

Functions

This section is empty.

Types

type Diagnostics

type Diagnostics struct {
	Submitted, Evicted, Skipped uint64
	MPVDropped, MPVDelayed      uint64
	AVSync                      time.Duration
	Path                        string
	Err                         error
}

Diagnostics reports cumulative renderer counts and the current libmpv playback counters when enabled by Options.Diagnostics. AVSync is the signed current audio/video difference. Path identifies the selected output.

type Event

type Event struct {
	Kind  EventKind
	State State
	Err   error
}

Event carries the playback snapshot at the time of dispatch scheduling. Err is set for Error events. Consecutive Changed events and ordinary Progress events may be coalesced while the app's main thread is busy.

type EventKind

type EventKind uint8

EventKind identifies a playback notification.

const (
	// Changed reports changes such as pause, source, duration, dimensions,
	// HDR metadata, or track selection. Progress has its own event.
	Changed EventKind = iota
	Loaded
	Finished
	Error
	// Progress reports position, buffering state, or buffer percentage.
	// A snapshot also accompanies Loaded, Finished, and a completed seek.
	Progress
)

type HDR

type HDR struct {
	Active                                bool
	Transfer, Primaries                   string
	MinLuminance, MaxLuminance            float64
	MaxContentLight, MaxFrameAverageLight float64
}

HDR describes the source's reported color metadata. Luminance and light levels are in cd/m². Zero represents unavailable metadata.

type HardwareDecoding

type HardwareDecoding uint8

HardwareDecoding selects the decode policy.

const (
	HardwareAuto HardwareDecoding = iota
	HardwareOn
	HardwareOff
)

type OpenOptions

type OpenOptions struct {
	Headers   http.Header
	Start     time.Duration
	Subtitles []string
	// contains filtered or unexported fields
}

OpenOptions applies to one source. Headers can include User-Agent and Referer, which are applied as their dedicated mpv options.

type Options

type Options struct {

	// LibraryPath overrides the platform libmpv search. On macOS the default
	// looks in the app's Frameworks directory, then the Homebrew locations.
	LibraryPath string
	// OnEvent receives playback events on the app's main thread.
	OnEvent func(Event)
	// ProgressInterval is the minimum interval between ordinary Progress
	// events. Zero uses 250 ms and a negative duration disables throttling.
	// Load, end of playback, and completed seek snapshots bypass the interval.
	ProgressInterval time.Duration
	// Video enables frame output for ui.Video. The default is headless.
	Video bool
	// SoftwareVideo selects software rendering and BGRA upload on Windows.
	SoftwareVideo bool
	// Diagnostics enables renderer counters for Diagnostics.
	Diagnostics bool
	// contains filtered or unexported fields
}

Options configures a Player.

type Phase

type Phase uint8

Phase describes the lifecycle of the current source.

const (
	Idle Phase = iota
	Loading
	Ready
	Ended
	Failed
)

type Player

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

Player owns a playback instance. Its methods are safe to call concurrently. Call Close to release the instance, for example when the app quits.

func New

func New(app *tango.App, opts Options) (*Player, error)

New creates a player that dispatches events through app.Post. On macOS it and Linux or Windows it needs libmpv 0.37 or newer (client API version 2.2). Enable Options.Video to attach a frame output.

func (*Player) AddSubtitle

func (p *Player) AddSubtitle(path string, selectTrack bool) error

AddSubtitle loads an external subtitle file and optionally selects it.

func (*Player) Close

func (p *Player) Close() error

Close releases the player's native resources. It is safe to call repeatedly or from an event callback.

func (*Player) Diagnostics

func (p *Player) Diagnostics() Diagnostics

Diagnostics reads the current counters. A zero value is returned when diagnostics were disabled. Reading mpv properties is charged to the caller.

func (*Player) Open

func (p *Player) Open(source string, opts OpenOptions) error

Open replaces the current source with a local path or a URL. It returns after mpv accepts the request. Loaded and Error report the eventual result.

func (*Player) Pause

func (p *Player) Pause() error

Pause pauses playback.

func (*Player) Play

func (p *Player) Play() error

Play resumes or begins playback.

func (*Player) Screenshot

func (p *Player) Screenshot(path string) error

Screenshot writes the current video frame as a PNG, overwriting an existing file at path. The path must have a .png extension.

func (*Player) Seek

func (p *Player) Seek(position time.Duration, mode SeekMode) error

Seek moves to an absolute position from the start of the source.

func (*Player) SelectTrack

func (p *Player) SelectTrack(kind TrackKind, id int64) error

SelectTrack selects an audio or subtitle track by ID. Zero disables that track kind, and -1 restores mpv's automatic selection.

func (*Player) SetHardwareDecoding

func (p *Player) SetHardwareDecoding(mode HardwareDecoding) error

SetHardwareDecoding changes the decode policy. HardwareAuto selects among safe hardware decoders. HardwareOn tries all available hardware decoders. mpv may fall back to software for unsupported sources.

func (*Player) SetLoop

func (p *Player) SetLoop(loop bool) error

SetLoop repeats the current source until disabled.

func (*Player) SetMuted

func (p *Player) SetMuted(muted bool) error

SetMuted controls whether audio output is muted.

func (*Player) SetSpeed

func (p *Player) SetSpeed(rate float64) error

SetSpeed changes the playback rate. A rate of 1 plays at normal speed.

func (*Player) SetSubtitleOffset

func (p *Player) SetSubtitleOffset(offset time.Duration) error

SetSubtitleOffset applies a timing offset. A positive offset delays subtitles.

func (*Player) SetSubtitleStyle

func (p *Player) SetSubtitleStyle(style SubtitleStyle) error

SetSubtitleStyle applies subtitle styling to the player.

func (*Player) SetVolume

func (p *Player) SetVolume(percent float64) error

SetVolume sets the audio volume as a percentage from 0 to 100.

func (*Player) State

func (p *Player) State() State

State returns the latest playback snapshot.

func (*Player) Tracks

func (p *Player) Tracks() ([]Track, error)

Tracks returns the available audio and subtitle tracks.

func (*Player) VideoError

func (p *Player) VideoError() error

VideoError returns an output initialization or rendering error, when one has occurred. OnEvent reports initialization failures as well.

type SeekMode

type SeekMode uint8

SeekMode determines how a seek is resolved.

const (
	SeekExact SeekMode = iota
	SeekKeyframe
)

type State

type State struct {
	Source             string
	Phase              Phase
	Position, Duration time.Duration
	BufferPercent      float64
	Paused, Buffering  bool
	Width, Height      int
	HDR                HDR
}

State is a snapshot of playback. Position and Duration are zero when unavailable. BufferPercent is mpv's cache fill percentage before resuming playback, from 0 to 100, or zero when unavailable.

type SubtitleStyle

type SubtitleStyle struct {
	Font                string
	Size                float64
	Foreground, Outline *color.NRGBA
	OutlineWidth        *float64
	OverrideASS         bool
	// contains filtered or unexported fields
}

SubtitleStyle configures text subtitles. Unset fields leave their current value in place. OverrideASS also applies styling to authored ASS subtitles.

type Track

type Track struct {
	ID                                  int64
	Kind                                TrackKind
	Title, Language, Codec              string
	Selected, Default, Forced, External bool
}

Track describes an available audio or subtitle track in the current source.

type TrackKind

type TrackKind string

TrackKind identifies a type of media track.

const (
	Audio    TrackKind = "audio"
	Subtitle TrackKind = "sub"
)

Directories

Path Synopsis
internal
mpv
Package mpv adapts the libmpv client API to Go values.
Package mpv adapts the libmpv client API to Go values.

Jump to

Keyboard shortcuts

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