media

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package media provides FFmpeg-based media processing capabilities.

Crop, progress, and timecode helpers live in nested packages. Package media re-exports their types and functions so importers keep using this path.

Index

Constants

View Source
const (

	// ClipQualityLow is the low quality preset.
	ClipQualityLow ClipQuality = "low"
	// ClipQualityMedium is the medium quality preset.
	ClipQualityMedium ClipQuality = "medium"
	// ClipQualityHigh is the high quality preset.
	ClipQualityHigh ClipQuality = "high"

	// MinCRF is the lowest allowed libx264 CRF.
	MinCRF = 0
	// MaxCRF is the highest allowed libx264 CRF.
	MaxCRF = 51
	// MinAudioKbps is the lowest allowed AAC bitrate.
	MinAudioKbps = 64
	// MaxAudioKbps is the highest allowed AAC bitrate.
	MaxAudioKbps = 640

	// OutputWidth720p is 1280px wide.
	OutputWidth720p = 1280
	// OutputWidth1080p is 1920px wide.
	OutputWidth1080p = 1920
	// OutputWidth1440p is 2560px wide.
	OutputWidth1440p = 2560
	// OutputWidth2160p is 3840px wide (4K).
	OutputWidth2160p = 3840
)

Variables

View Source
var EncoderPresets = []string{
	"ultrafast",
	"superfast",
	"veryfast",
	"faster",
	"fast",
	"medium",
	"slow",
	"slower",
	"veryslow",
}

EncoderPresets lists valid libx264 -preset values from fastest to slowest.

View Source
var ErrInvalidTimecode = timecode.ErrInvalidTimecode

ErrInvalidTimecode is returned when a timestamp string cannot be parsed.

OutputWidths lists selectable clip export widths.

View Source
var QualityPresets = map[ClipQuality]QualityPreset{
	ClipQualityLow: {
		CRF:       crfLowQuality,
		Preset:    "veryfast",
		AudioKbps: audioKbpsLow,
		MaxWidth:  OutputWidth720p,
	},
	ClipQualityMedium: {
		CRF:       crfMediumQuality,
		Preset:    "medium",
		AudioKbps: audioKbpsMedium,
		MaxWidth:  OutputWidth1080p,
	},
	ClipQualityHigh: {
		CRF:       crfHighQuality,
		Preset:    "slow",
		AudioKbps: audioKbpsHigh,
		MaxWidth:  OutputWidth2160p,
	},
}

QualityPresets maps built-in quality identifiers to presets.

Functions

func ChannelLayoutName

func ChannelLayoutName(channels int) string

ChannelLayoutName names common speaker layouts.

func DefaultFFmpegTimeout

func DefaultFFmpegTimeout() time.Duration

DefaultFFmpegTimeout returns the default FFmpeg timeout.

func NormalizeOutputWidth

func NormalizeOutputWidth(width int) int

NormalizeOutputWidth returns width if supported, otherwise 1080p.

func OutputWidthLabel

func OutputWidthLabel(width int) string

OutputWidthLabel is the UI label for an export width.

func ValidAudioKbps

func ValidAudioKbps(kbps int) bool

ValidAudioKbps reports whether kbps is in the allowed AAC range.

func ValidCRF

func ValidCRF(crf int) bool

ValidCRF reports whether crf is in the libx264 range.

func ValidEncoderPreset

func ValidEncoderPreset(name string) bool

ValidEncoderPreset reports whether name is a supported libx264 preset.

func ValidOutputWidth

func ValidOutputWidth(width int) bool

ValidOutputWidth reports whether width is a supported export width.

func WithProgress

func WithProgress(ctx context.Context, fn func(percent int)) context.Context

WithProgress attaches a progress callback to the context.

Parameters:

  • ctx: Parent context.
  • fn: Callback that receives a 0-99 percent complete value.

Returns:

  • ctx: A child context that carries fn.

Types

type AudioTrack

type AudioTrack struct {
	Index    int
	Codec    string
	Language string
	Title    string
	Channels int
}

AudioTrack is one audio stream on a source file.

type ClipQuality

type ClipQuality string

ClipQuality represents a built-in clip quality identifier.

type Clock

type Clock interface {
	Format(duration time.Duration) string
	Parse(value string) (time.Duration, error)
}

Clock formats and parses FFmpeg time durations.

See https://ffmpeg.org/ffmpeg-utils.html#Time-duration

var DefaultClock Clock = timecode.DefaultClock

DefaultClock is the FFmpeg duration clock.

type CropRect

type CropRect = crop.CropRect

CropRect is an ffmpeg crop=W:H:X:Y rectangle.

func ParseCropdetect

func ParseCropdetect(output string) (CropRect, bool)

ParseCropdetect returns the last crop=W:H:X:Y from cropdetect logs.

Parameters:

  • output: ffmpeg stderr text from a cropdetect pass.

Returns:

  • crop: The last parsed rectangle, or a zero value.
  • ok: True when a valid rectangle was found.

type ExecFFmpeg

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

ExecFFmpeg provides FFmpeg execution capabilities.

func NewExecFFmpeg

func NewExecFFmpeg(ffmpegPath, ffprobePath string) *ExecFFmpeg

NewExecFFmpeg creates a new FFmpeg executor.

func (*ExecFFmpeg) DetectCrop

func (execFFmpeg *ExecFFmpeg) DetectCrop(
	ctx context.Context,
	input string,
	start, duration float64,
) (CropRect, error)

DetectCrop samples the source with cropdetect and returns a crop rectangle.

func (*ExecFFmpeg) ExtractClip

func (execFFmpeg *ExecFFmpeg) ExtractClip(
	ctx context.Context,
	input, output string,
	start, duration float64,
	preset QualityPreset,
	audioIndex int,
	rect CropRect,
) error

ExtractClip extracts a clip from a video.

func (*ExecFFmpeg) ExtractGIF

func (execFFmpeg *ExecFFmpeg) ExtractGIF(
	ctx context.Context,
	input, output string,
	start, duration float64,
	width, fps int,
	rect CropRect,
) error

ExtractGIF extracts a GIF from a video.

func (*ExecFFmpeg) ExtractPreview

func (execFFmpeg *ExecFFmpeg) ExtractPreview(
	ctx context.Context,
	input, output string,
	start, duration float64,
	audioIndex int,
	rect CropRect,
	preset QualityPreset,
) error

ExtractPreview writes a short, downscaled, browser-safe preview segment.

func (*ExecFFmpeg) ExtractScreenshot

func (execFFmpeg *ExecFFmpeg) ExtractScreenshot(
	ctx context.Context,
	input, output string,
	timestamp float64,
	rect CropRect,
) error

ExtractScreenshot extracts a screenshot from a video.

func (*ExecFFmpeg) Probe

func (execFFmpeg *ExecFFmpeg) Probe(ctx context.Context, path string) (MediaInfo, error)

Probe probes a media file for information.

func (*ExecFFmpeg) SetTimeout

func (execFFmpeg *ExecFFmpeg) SetTimeout(d time.Duration)

SetTimeout sets the FFmpeg timeout.

type FFmpeg

type FFmpeg interface {
	Probe(ctx context.Context, path string) (MediaInfo, error)
	ExtractClip(
		ctx context.Context,
		input, output string,
		start, duration float64,
		preset QualityPreset,
		audioIndex int,
		crop CropRect,
	) error
	DetectCrop(ctx context.Context, input string, start, duration float64) (CropRect, error)
	ExtractGIF(
		ctx context.Context,
		input, output string,
		start, duration float64,
		width, fps int,
		crop CropRect,
	) error
	ExtractPreview(
		ctx context.Context,
		input, output string,
		start, duration float64,
		audioIndex int,
		crop CropRect,
		preset QualityPreset,
	) error
	ExtractScreenshot(
		ctx context.Context,
		input, output string,
		timestamp float64,
		crop CropRect,
	) error
}

FFmpeg provides FFmpeg media processing capabilities.

type FFmpegClock

type FFmpegClock = timecode.FFmpegClock

FFmpegClock is the FFmpeg time-duration clock.

type MediaInfo

type MediaInfo struct {
	Duration   float64 `json:"duration"`
	Width      int     `json:"width"`
	Height     int     `json:"height"`
	VideoCodec string  `json:"video_codec"`
	AudioCodec string  `json:"audio_codec"`
	Format     string  `json:"format"`
	BitRate    int64   `json:"bit_rate"`
	// ColorTransfer is ffprobe color_transfer of the first video stream.
	ColorTransfer string       `json:"color_transfer"`
	AudioTracks   []AudioTrack `json:"audio_tracks"`
}

MediaInfo represents media file information.

type QualityPreset

type QualityPreset struct {
	CRF       int
	Preset    string
	AudioKbps int
	MaxWidth  int
	// WebSafeColor tone-maps HDR to Rec.709 when true.
	WebSafeColor bool
}

QualityPreset represents ffmpeg clip encode settings.

func NormalizePreset

func NormalizePreset(preset QualityPreset) QualityPreset

NormalizePreset fills missing or invalid encode settings with Medium.

func ResolvePreset

func ResolvePreset(quality string, lookup func(string) (QualityPreset, bool)) QualityPreset

ResolvePreset maps a stored quality id onto ffmpeg settings.

Lookup is tried first so user-defined profiles win over built-in names.

type Timecode

type Timecode = timecode.Timecode

Timecode is a media timestamp.

func FromSeconds

func FromSeconds(seconds float64) Timecode

FromSeconds builds a timecode from a floating-point second count.

Parameters:

  • seconds: Timestamp in seconds. Negative, NaN, Inf, and values outside the time.Duration range become zero.

Returns:

  • timecode: The timestamp, or a zero value when seconds is not finite.

func Parse

func Parse(value string) (Timecode, error)

Parse builds a timecode from an FFmpeg time-duration string.

Parameters:

  • value: An FFmpeg time-duration string.

Returns:

  • parsed: The parsed timestamp.
  • err: Non-nil when value is not a valid duration.

Directories

Path Synopsis
Package crop parses ffmpeg cropdetect rectangles.
Package crop parses ffmpeg cropdetect rectangles.
Package progress reports ffmpeg encode completion from stderr.
Package progress reports ffmpeg encode completion from stderr.
Package timecode formats and parses FFmpeg time durations.
Package timecode formats and parses FFmpeg time durations.

Jump to

Keyboard shortcuts

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