media

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 24 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 media is lys-ui's playback framework, GTK's GtkMediaStream in pure Go: a Source decodes frames (and, when Audible, sound), and a Stream plays one on the loop with play, pause, seek, loop, volume and a clock that follows the audio when there is any. widget.Video paints a Stream and widget.MediaControls drives it.

Built-in sources: animated images (NewAnimation), YUV4MPEG2 (NewY4M, decoded here), and any file ffmpeg reads (OpenFile, which runs ffmpeg and ffprobe from PATH as subprocesses). Sound plays through the PulseAudio native protocol, which PulseAudio and pipewire-pulse serve.

src, err := media.OpenFile("talk.webm")
stream := media.NewStream(src)
video := widget.NewVideo(face, 13, stream)
stream.Play()

Index

Examples

Constants

View Source
const (
	AudioRate     = 48000
	AudioChannels = 2
)

The PCM layout every Audible track delivers: interleaved signed 16-bit little-endian samples.

Variables

View Source
var ErrNotSeekable = errors.New("media: the stream cannot seek")

ErrNotSeekable reports a Seek on a Stream whose source cannot seek.

Functions

This section is empty.

Types

type Audible

type Audible interface {
	// OpenAudio returns the track from t as PCM in the AudioRate,
	// AudioChannels layout; closing it stops the decoding.
	OpenAudio(t time.Duration) (io.ReadCloser, error)
}

Audible is a Source with a sound track.

type Info

type Info struct {
	// Width and Height are the frame size in pixels.
	Width, Height int
	// Duration is the length; zero when the source cannot know it.
	Duration time.Duration
}

Info describes a source's video.

type Seeker

type Seeker interface {
	Seek(t time.Duration) error
}

Seeker is a Source that can reposition: after Seek(t) the next ReadFrame returns the frame showing at t.

type Source

type Source interface {
	Info() Info
	// ReadFrame decodes the next frame into dst, which is Info's size,
	// and returns its presentation time. It returns io.EOF after the
	// last frame.
	ReadFrame(dst *image.RGBA) (time.Duration, error)
	Close() error
}

Source decodes a video's frames in presentation order. ReadFrame and Seek are called from one goroutine; Close may be called from another at any time and must unblock a ReadFrame in progress.

func NewAnimation

func NewAnimation(a *render.Animation) (Source, error)

NewAnimation plays a decoded GIF or APNG (render.DecodeImage) as a seekable source; the image's own loop count is left to the Stream's SetLoop.

func NewY4M

func NewY4M(r io.Reader) (Source, error)

NewY4M reads a YUV4MPEG2 stream (8-bit 4:2:0, 4:2:2, 4:4:4 or mono, BT.601, limited range unless XCOLORRANGE=FULL). When r is also an io.ReadSeeker the source seeks and knows its duration; that needs every frame header to be a bare FRAME line, as ffmpeg writes. Close closes r when it is an io.Closer.

Example
raw := "YUV4MPEG2 W2 H2 F25:1 XCOLORRANGE=FULL\nFRAME\n" + strings.Repeat("\xff", 4) + "\x80\x80"
src, _ := NewY4M(strings.NewReader(raw))
dst := image.NewRGBA(image.Rect(0, 0, 2, 2))
pts, _ := src.ReadFrame(dst)
fmt.Printf("%v %v %v\n", src.Info().Duration, pts, dst.Pix[:4])
Output:
40ms 0s [255 255 255 255]

func OpenFile

func OpenFile(path string) (Source, error)

OpenFile opens a media file through ffmpeg, which decodes anything it knows (H.264, VP9, AV1, ...): ffprobe reads the streams, an ffmpeg subprocess decodes the first video stream, and a second one decodes the first audio stream when there is one, so the source is Audible exactly when the file has sound. Both binaries come from PATH; their absence is an error, as is a file without video. Seeking restarts the decoders at the new position.

func WithoutAudio

func WithoutAudio(src Source) Source

WithoutAudio returns src with its sound track hidden, for playback with no audio output at all (a muted Stream still opens one).

type Stream

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

Stream plays a Source, GTK's GtkMediaStream: it runs the decoder on its own goroutine a few frames ahead, presents the frame due at the playback position on the loop's frame clock, and, when the source is Audible, plays the sound through PulseAudio and follows its clock so picture and sound stay together. Every method runs on the loop goroutine. Subscribers hear every change: the frame, the position while playing, and the state.

func NewStream

func NewStream(src Source, name string) *Stream

NewStream plays src, named name in the desktop's volume mixer. It starts paused and decodes the first frame right away; Close releases the source and its decoders.

func (*Stream) Close

func (s *Stream) Close() error

Close stops playback and releases the source and decoders.

func (*Stream) Closed

func (s *Stream) Closed() bool

Closed reports whether Close was called.

func (*Stream) Duration

func (s *Stream) Duration() time.Duration

Duration returns the length, zero when unknown.

func (*Stream) Ended

func (s *Stream) Ended() bool

Ended reports whether playback reached the end (and is not looping).

func (*Stream) Err

func (s *Stream) Err() error

Err returns the failure that stopped the stream, nil while healthy.

func (*Stream) Frame

func (s *Stream) Frame() render.VersionedImage

Frame returns the frame to show, its RGBA nil before the first is decoded and after Close: a pooled buffer's pixels under the buffer's identity, at a version that changes whenever the frame does. The image belongs to the stream and stays valid until the next change or Close.

func (*Stream) HasAudio

func (s *Stream) HasAudio() bool

HasAudio reports whether the stream plays sound.

func (*Stream) Loop

func (s *Stream) Loop() bool

Loop reports whether playback restarts at the end.

func (*Stream) Muted

func (s *Stream) Muted() bool

Muted reports whether the sound is muted.

func (*Stream) Pause

func (s *Stream) Pause()

Pause pauses playback at the current position.

func (*Stream) Play

func (s *Stream) Play()

Play starts or resumes playback; at the end it starts over.

func (*Stream) Playing

func (s *Stream) Playing() bool

Playing reports whether the stream is playing.

func (*Stream) Position

func (s *Stream) Position() time.Duration

Position returns the playback position.

func (*Stream) Seek

func (s *Stream) Seek(t time.Duration) error

Seek moves playback to t, clamped to the stream. Playback continues from there if it was playing.

func (*Stream) Seekable

func (s *Stream) Seekable() bool

Seekable reports whether Seek works.

func (*Stream) SetLoop

func (s *Stream) SetLoop(on bool)

SetLoop makes playback restart at the end; it needs a seekable source and is ignored otherwise.

func (*Stream) SetMuted

func (s *Stream) SetMuted(on bool)

SetMuted mutes or unmutes the sound.

func (*Stream) SetVolume

func (s *Stream) SetVolume(v float64)

SetVolume sets the volume, 0 to 1 on a perceptual scale. Until it is called the desktop picks the stream's volume.

func (*Stream) Size

func (s *Stream) Size() (w, h int)

Size returns the frame size in pixels.

func (*Stream) Subscribe

func (s *Stream) Subscribe(fn func()) (cancel func())

Subscribe calls fn on the loop after every change; cancel stops it.

func (*Stream) Volume

func (s *Stream) Volume() float64

Volume returns the volume set through SetVolume, 1 until then.

Jump to

Keyboard shortcuts

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