media

package
v2.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package media provides utilities for processing media content.

Package media provides utilities for processing media content.

Package media provides utilities for processing media content (images, video).

Index

Constants

View Source
const (
	AudioFormatWAV  = "wav"
	AudioFormatMP3  = "mp3"
	AudioFormatFLAC = "flac"
	AudioFormatOGG  = "ogg"
	AudioFormatM4A  = "m4a"
	AudioFormatAAC  = "aac"
	AudioFormatPCM  = "pcm"
	AudioFormatWebM = "webm"
)

Audio format constants.

View Source
const (
	MIMETypeAudioWAV  = "audio/wav"
	MIMETypeAudioMP3  = "audio/mpeg"
	MIMETypeAudioFLAC = "audio/flac"
	MIMETypeAudioOGG  = "audio/ogg"
	MIMETypeAudioM4A  = "audio/mp4"
	MIMETypeAudioAAC  = "audio/aac"
	MIMETypeAudioPCM  = "audio/pcm"
	MIMETypeAudioWebM = "audio/webm"
)

Audio MIME type constants.

View Source
const (
	DefaultFFmpegPath          = "ffmpeg"
	DefaultFFmpegTimeout       = 300  // 5 minutes
	DefaultFFmpegCheckTimeout  = 5    // seconds for availability check
	DefaultTempFilePermissions = 0600 // owner read/write only
)

Default configuration values.

View Source
const (
	FormatJPEG = "jpeg"
	FormatJPG  = "jpg"
	FormatPNG  = "png"
	FormatGIF  = "gif"
	FormatWebP = "webp"
)

Image format constants.

View Source
const (
	MIMETypeJPEG = "image/jpeg"
	MIMETypePNG  = "image/png"
	MIMETypeGIF  = "image/gif"
	MIMETypeWebP = "image/webp"
)

MIME type constants.

View Source
const (
	DefaultMaxWidth  = 1024
	DefaultMaxHeight = 1024
	DefaultQuality   = 85
	MinQuality       = 10
	QualityDecay     = 0.9
)

Default configuration values.

View Source
const (
	MIMETypeAudioXWAV = "audio/x-wav"
)

MIME type variant constants.

Variables

View Source
var (
	ErrFFmpegNotFound = fmt.Errorf("ffmpeg not found in PATH")
	ErrFFmpegTimeout  = fmt.Errorf("ffmpeg execution timed out")
)

FFmpeg error types.

Functions

func AudioFormatToMIMEType

func AudioFormatToMIMEType(format string) string

AudioFormatToMIMEType converts a format string to MIME type.

func CheckFFmpegAvailable

func CheckFFmpegAvailable(ffmpegPath string) error

CheckFFmpegAvailable checks if ffmpeg is available in PATH.

func IsFormatSupported

func IsFormatSupported(mimeType string, supportedFormats []string) bool

IsFormatSupported checks if a MIME type is in the list of supported formats.

func MIMETypeToAudioFormat

func MIMETypeToAudioFormat(mimeType string) string

MIMETypeToAudioFormat converts a MIME type to a format string.

func MIMETypeToFormat

func MIMETypeToFormat(mimeType string) string

MIMETypeToFormat converts a MIME type to format string.

func SelectTargetFormat

func SelectTargetFormat(supportedFormats []string) string

SelectTargetFormat selects the best target format from supported formats. Prefers lossless formats (WAV) when available, then common formats (MP3).

Types

type AudioConvertResult

type AudioConvertResult struct {
	Data         []byte
	Format       string
	MIMEType     string
	OriginalSize int64
	NewSize      int64
	WasConverted bool
}

AudioConvertResult contains the result of an audio conversion.

type AudioConverter

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

AudioConverter handles audio format conversion using ffmpeg.

func NewAudioConverter

func NewAudioConverter(config AudioConverterConfig) *AudioConverter

NewAudioConverter creates a new audio converter with the given config.

func (*AudioConverter) CanConvert

func (c *AudioConverter) CanConvert(fromMIME, toMIME string) bool

CanConvert checks if the converter can convert between the given formats.

func (*AudioConverter) ConvertAudio

func (c *AudioConverter) ConvertAudio(
	ctx context.Context,
	data []byte,
	fromMIME, toMIME string,
) (*AudioConvertResult, error)

ConvertAudio converts audio data from one format to another. If the source format matches the target, returns the original data unchanged.

type AudioConverterConfig

type AudioConverterConfig struct {
	// FFmpegPath is the path to the ffmpeg binary.
	// Default: "ffmpeg" (uses PATH).
	FFmpegPath string

	// FFmpegTimeout is the maximum time for FFmpeg execution.
	// Default: 5 minutes.
	FFmpegTimeout int // seconds

	// SampleRate is the output sample rate in Hz.
	// 0 means preserve original.
	SampleRate int

	// Channels is the number of output channels.
	// 0 means preserve original.
	Channels int

	// BitRate is the output bitrate for lossy formats (e.g., "128k").
	// Empty means use ffmpeg default.
	BitRate string
}

AudioConverterConfig configures audio conversion behavior.

func DefaultAudioConverterConfig

func DefaultAudioConverterConfig() AudioConverterConfig

DefaultAudioConverterConfig returns sensible defaults for audio conversion.

type ContentConverter

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

ContentConverter handles conversion of MediaContent to match provider requirements.

func NewContentConverter

func NewContentConverter(config AudioConverterConfig) *ContentConverter

NewContentConverter creates a new content converter.

func (*ContentConverter) ConvertMediaContentIfNeeded

func (c *ContentConverter) ConvertMediaContentIfNeeded(
	ctx context.Context,
	media *types.MediaContent,
	contentType string,
	targetFormats []string,
) (*types.MediaContent, error)

ConvertMediaContentIfNeeded converts media content to a supported format if necessary.

func (*ContentConverter) ConvertMessageForProvider

func (c *ContentConverter) ConvertMessageForProvider(
	ctx context.Context,
	msg *types.Message,
	provider providers.Provider,
) (*types.Message, error)

ConvertMessageForProvider converts all media parts in a message to formats supported by the provider. Returns a new message with converted content (original is not modified).

type ImageResizeConfig

type ImageResizeConfig struct {
	// MaxWidth is the maximum width in pixels (0 = no limit).
	MaxWidth int

	// MaxHeight is the maximum height in pixels (0 = no limit).
	MaxHeight int

	// MaxSizeBytes is the maximum encoded size in bytes (0 = no limit).
	// If exceeded after resize, quality is reduced iteratively.
	MaxSizeBytes int64

	// Quality is the encoding quality (1-100). Used for JPEG and WebP.
	// Default: 85.
	Quality int

	// Format is the output format ("jpeg", "png", "" = preserve original).
	Format string

	// PreserveAspectRatio maintains the original aspect ratio when resizing.
	// Default: true.
	PreserveAspectRatio bool

	// SkipIfSmaller skips processing if the image is already within limits.
	// Default: true.
	SkipIfSmaller bool
}

ImageResizeConfig configures image resizing behavior.

func DefaultImageResizeConfig

func DefaultImageResizeConfig() ImageResizeConfig

DefaultImageResizeConfig returns sensible defaults for image resizing.

type ResizeResult

type ResizeResult struct {
	Data         []byte
	Format       string
	MIMEType     string
	Width        int
	Height       int
	OriginalSize int64
	NewSize      int64
	WasResized   bool
}

ResizeResult contains the result of an image resize operation.

func ResizeImage

func ResizeImage(data []byte, config ImageResizeConfig) (*ResizeResult, error)

ResizeImage resizes an image to fit within the configured dimensions. Returns the resized image data, format, and any error.

Jump to

Keyboard shortcuts

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