format

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package format defines WaxTap's stream-format model and the rules for picking an audio source from a candidate list.

The package keeps YouTube details at the edge. The youtube package fills Format values from player responses, while this package owns comparison and selection. A Format is only a candidate; signed media URLs and expiry metadata live in youtube.ResolvedStream.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoMatch = errors.New("format: no matching audio format")

ErrNoMatch reports that selection found no audio candidate satisfying the request. Selector-specific errors wrap it, so callers should check with errors.Is.

Functions

func BestForTarget

func BestForTarget(candidates []Format, policy SourcePolicy, target Target) (int, error)

BestForTarget chooses the source audio for a transcode target and returns its index in candidates. It is the BestAudio path:

  • zero Target or a lossless target: use normal best-audio ranking
  • MinimizeLoss: prefer a source in the target codec family
  • BestNative: ignore the target codec and use normal best-audio ranking
  • PreferCodec: prefer the policy codec family

Codec preference sits below original-track selection, so it never chooses a dubbed track over the original for codec compatibility. When no candidate matches the preferred codec, selection falls back to normal ranking. It returns ErrNoMatch only when there are no selectable audio candidates.

Types

type AudioSelector

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

AudioSelector picks one Format from a candidate list. The zero value selects the best available audio (equivalent to BestAudio()).

The selector stores caller intent; concrete selection is performed by the extraction/download pipeline.

func BestAudio

func BestAudio() AudioSelector

BestAudio selects the best audio stream, preferring the original track, non-DRC audio, then higher effective bitrate.

func Codec

func Codec(codec string) AudioSelector

Codec selects the best stream whose codec matches (e.g. "opus", "aac").

func Itag

func Itag(itag int) AudioSelector

Itag selects the stream with the exact itag.

func (AudioSelector) Select

func (s AudioSelector) Select(candidates []Format, policy SourcePolicy, target Target) (int, error)

Select resolves an AudioSelector against candidates and returns the chosen index. The index matters because an itag can repeat across language and DRC variants.

Itag and Codec first filter to matching audio candidates, then use normal audio ranking within that set. BestAudio, including the zero selector, delegates to BestForTarget so SourcePolicy and Target can influence the source. Select returns ErrNoMatch when no audio candidate satisfies the selector.

func (AudioSelector) String

func (s AudioSelector) String() string

type AudioTrack

type AudioTrack struct {
	ID          string
	DisplayName string
	IsOriginal  Tri
}

AudioTrack holds the raw audioTrack metadata YouTube attaches to dubbed or multi-language renditions.

type Format

type Format struct {
	Itag int

	// Codec / container.
	MIMEType  string // raw mimeType, e.g. `audio/webm; codecs="opus"`
	Codec     string // normalized codec id, e.g. "opus", "mp4a.40.2"
	Extension string // canonical file extension without dot, e.g. "webm", "m4a"

	// Audio characteristics.
	Bitrate        int // declared / peak bits per second
	AverageBitrate int // average bits per second (preferred for comparison)
	SampleRate     int // Hz
	Channels       int

	// Multi-language / dubbed audio metadata.
	Language   string      // audioTrack language tag, "" if single-track
	AudioTrack *AudioTrack // raw audioTrack metadata, nil if none

	// Tri-state quality hints (YouTube is inconsistent about exposing these).
	IsDRC      Tri // dynamic-range-compressed rendition
	IsOriginal Tri // original (non-dubbed) audio

	// Size / length when known (0 == unknown).
	ContentLength int64
	Duration      time.Duration
}

Format describes a playable stream candidate. Most WaxTap callers deal with audio, but video formats can appear in unfiltered player responses and are excluded by audio selectors.

func (Format) EffectiveBitrate

func (f Format) EffectiveBitrate() int

EffectiveBitrate returns AverageBitrate when known, else the declared Bitrate. It is the value selectors compare on.

func (Format) IsAudio

func (f Format) IsAudio() bool

IsAudio reports whether MIMEType is explicitly audio/*.

func (Format) String

func (f Format) String() string

type SourcePolicy

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

SourcePolicy controls the source-stream tradeoff when a transcode target is set. The zero value is MinimizeLoss().

func BestNative

func BestNative() SourcePolicy

BestNative ignores target codec matching and uses normal best-audio ranking.

func MinimizeLoss

func MinimizeLoss() SourcePolicy

MinimizeLoss prefers a source in the target codec family, avoiding a cross-codec transcode when possible.

func PreferCodec

func PreferCodec(codec string) SourcePolicy

PreferCodec prefers a source in the named codec family when policy is active.

func (SourcePolicy) String

func (p SourcePolicy) String() string

type Target

type Target struct {
	// Codec is the normalized output codec id, such as "aac" or "opus".
	// MinimizeLoss uses it only when a YouTube source may exist in the same
	// family. Leave it empty for targets with no native source equivalent, such
	// as MP3.
	Codec string
	// Lossless is true for FLAC, ALAC, and WAV targets. Since these preserve the
	// decoded samples, source codec matching does not help.
	Lossless bool
}

Target describes the transcode output BestForTarget is selecting for. A zero Target means keep or copy the source, so normal best-audio ranking applies.

type Tri

type Tri uint8

Tri is a three-valued flag for metadata YouTube does not always expose. Unknown should stay distinct from Yes and No.

const (
	Unknown Tri = iota
	Yes
	No
)

func (Tri) String

func (t Tri) String() string

Jump to

Keyboard shortcuts

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