thumbnailer

package
v1.11.2 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 15 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.

Documentation

Overview

Package thumbnailer finds the scenes of a video, and representative thumbnails of it.

Frames are sampled from the keyframes, when the video has them at a useful rate, or by seeking to evenly spaced times otherwise. Each sampled frame is measured for how good a thumbnail it makes (brightness, contrast, sharpness, colourfulness and so on), and compared with the one before it to find where scenes change. A thumbnail is then the best frame of a scene, and thumbnails are chosen from different scenes which don't look alike.

Index

Constants

View Source
const (
	DefaultWidth      = 640 // Default width of thumbnails
	DefaultMaxSamples = 300 // Default maximum number of frames sampled by seeking
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Analysis

type Analysis struct {
	Mode     Mode          // How frames were sampled, ModeKeyframes or ModeSeek
	Duration time.Duration // Duration of the video, or zero if it's unknown
	Samples  []Sample      // Frames sampled, in time order
	Scenes   []Scene       // Scenes, in time order
}

Analysis is the frames sampled from a video, and its scenes

type Mode

type Mode int

Mode is how frames are sampled from a video

const (
	ModeAuto      Mode = iota // Keyframes if the video has them at a useful rate, otherwise seek
	ModeKeyframes             // Decode only the keyframes
	ModeSeek                  // Seek to evenly spaced times, and decode the frame at each
)

How frames are sampled

func (Mode) String

func (m Mode) String() string

type Opt

type Opt func(*opts) error

Opt is a function which applies options to a Thumbnailer

func WithMaxSamples

func WithMaxSamples(n int) Opt

WithMaxSamples sets the maximum number of frames sampled by seeking, which sets how far apart they are. The default is DefaultMaxSamples.

func WithMode

func WithMode(mode Mode) Opt

WithMode sets how frames are sampled from the video.

func WithReaderOpt

func WithReaderOpt(opt reader.Opt) Opt

WithReaderOpt passes a reader.Opt to the reader.

func WithSize

func WithSize(width, height int) Opt

WithSize sets the size of thumbnails. Either can be zero, to keep the video's aspect ratio, and both zero is the video's own size. The default is DefaultWidth wide.

func WithStream

func WithStream(stream int) Opt

WithStream sets the index of the video stream. Without it, the video stream with the lowest index is used.

type Sample

type Sample struct {
	Time     time.Duration // Time of the frame
	Keyframe bool          // Whether it was sampled as a keyframe
	Change   float64       // How different it is from the sample before (0.0-1.0)
	Stats    Stats         // How good a thumbnail it makes
	// contains filtered or unexported fields
}

Sample is a frame sampled from a video, and its stats

type Scene

type Scene struct {
	Start      time.Duration // Time of its first sample
	Duration   time.Duration // Until the next scene starts, or the end
	Transition Transition    // How it starts
	First      int           // Index of its first sample
	Count      int           // Number of samples in it, which aren't blank
	Best       int           // Index of its best sample, or -1 if they're all blank
}

Scene is a part of a video between scene changes. As frames are sampled, its start is the time of its first sample, which is up to the time between samples after the scene changed

type Stats

type Stats struct {
	Brightness    float64 // Mean luma (0.0-1.0)
	Contrast      float64 // Standard deviation of luma (0.0-0.5)
	Sharpness     float64 // Variance of the Laplacian of luma, in 8-bit units, which is low for a blurred frame
	Colourfulness float64 // How colourful the frame is, from grey (0.0) to vivid (1.0)
	Entropy       float64 // Entropy of the luma histogram, from uniform (0.0) to detailed (1.0)
	EdgeDensity   float64 // Fraction of pixels on a strong edge (0.0-1.0), which is high for text and graphics
	Blank         bool    // Whether the frame is black, white or uniform, such as during a fade
	Score         float64 // How good a thumbnail the frame makes (0.0-1.0), which is zero when it's blank
}

Stats are measurements of a frame, of how good a thumbnail it makes. They are measured on a small copy of the frame, so they don't depend much on the video's size

type Thumbnail

type Thumbnail struct {
	Time  time.Duration // Time of the frame
	Scene int           // Index of the scene it's in
	Stats Stats         // How good a thumbnail it makes
	Image image.Image   // The frame, at the thumbnail size
}

Thumbnail is a frame of a video, as an image

type Thumbnailer

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

Thumbnailer finds the scenes and thumbnails of a video

func NewReader added in v1.11.2

func NewReader(r io.Reader, opt ...Opt) (*Thumbnailer, error)

NewReader opens a video from r, which needs to be an io.Seeker, such as a file, as frames are sampled by seeking it, so a reader which isn't one is an error.

func Open

func Open(path string, opt ...Opt) (*Thumbnailer, error)

Open opens a video from a file path or URL.

func (*Thumbnailer) Analyse

func (t *Thumbnailer) Analyse(ctx context.Context) (*Analysis, error)

Analyse samples frames from the video, measures them, and finds its scenes. It's only done once, and Thumbnails and Poster call it.

func (*Thumbnailer) Close

func (t *Thumbnailer) Close() error

Close releases the video.

func (*Thumbnailer) Frame

func (t *Thumbnailer) Frame(ctx context.Context, at time.Duration) (image.Image, error)

Frame returns the frame at a time, or the first one after it, at the thumbnail size, such as the best frame of a scene.

func (*Thumbnailer) Poster

func (t *Thumbnailer) Poster(ctx context.Context) (*Thumbnail, error)

Poster returns the one thumbnail which best represents the video.

func (*Thumbnailer) Size

func (t *Thumbnailer) Size() (int, int)

Size returns the size of the video as it's displayed, with square pixels, which is the size it's stored at with its frames' sample aspect ratio applied, once a frame has been decoded, such as by Analyse, or the size it's stored at before then.

func (*Thumbnailer) Thumbnails

func (t *Thumbnailer) Thumbnails(ctx context.Context, n int) ([]Thumbnail, error)

Thumbnails returns up to n thumbnails of the video, in time order, each the best frame of a different scene, from the scenes which make the best thumbnails and don't look alike.

type Transition

type Transition int

Transition is how a scene starts

const (
	TransitionStart Transition = iota // The start of the video
	TransitionCut                     // A cut from the scene before
	TransitionFade                    // A fade, through blank frames, from the scene before
)

func (Transition) String

func (t Transition) String() string

Jump to

Keyboard shortcuts

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