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
- Variables
- func AudioFormatToMIMEType(format string) string
- func CheckFFmpegAvailable(ffmpegPath string) error
- func IsFormatSupported(mimeType string, supportedFormats []string) bool
- func MIMETypeToAudioFormat(mimeType string) string
- func MIMETypeToFormat(mimeType string) string
- func SelectTargetFormat(supportedFormats []string) string
- type AudioConvertResult
- type AudioConverter
- type AudioConverterConfig
- type ContentConverter
- func (c *ContentConverter) ConvertMediaContentIfNeeded(ctx context.Context, media *types.MediaContent, contentType string, ...) (*types.MediaContent, error)
- func (c *ContentConverter) ConvertMessageForProvider(ctx context.Context, msg *types.Message, provider providers.Provider) (*types.Message, error)
- type ImageResizeConfig
- type ResizeResult
Constants ¶
const ( AudioFormatWAV = "wav" AudioFormatMP3 = "mp3" AudioFormatFLAC = "flac" AudioFormatOGG = "ogg" AudioFormatM4A = "m4a" AudioFormatAAC = "aac" AudioFormatPCM = "pcm" AudioFormatWebM = "webm" )
Audio format constants.
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.
const ( DefaultFFmpegPath = "ffmpeg" DefaultFFmpegTimeout = 300 // 5 minutes DefaultFFmpegCheckTimeout = 5 // seconds for availability check DefaultTempFilePermissions = 0600 // owner read/write only )
Default configuration values.
const ( FormatJPEG = "jpeg" FormatJPG = "jpg" FormatPNG = "png" FormatGIF = "gif" FormatWebP = "webp" )
Image format constants.
const ( MIMETypeJPEG = "image/jpeg" MIMETypePNG = "image/png" MIMETypeGIF = "image/gif" MIMETypeWebP = "image/webp" )
MIME type constants.
const ( DefaultMaxWidth = 1024 DefaultMaxHeight = 1024 DefaultQuality = 85 MinQuality = 10 QualityDecay = 0.9 )
Default configuration values.
const (
MIMETypeAudioXWAV = "audio/x-wav"
)
MIME type variant constants.
Variables ¶
var ( ErrFFmpegNotFound = fmt.Errorf("ffmpeg not found in PATH") ErrFFmpegTimeout = fmt.Errorf("ffmpeg execution timed out") )
FFmpeg error types.
Functions ¶
func AudioFormatToMIMEType ¶ added in v1.1.10
AudioFormatToMIMEType converts a format string to MIME type.
func CheckFFmpegAvailable ¶ added in v1.1.10
CheckFFmpegAvailable checks if ffmpeg is available in PATH.
func IsFormatSupported ¶ added in v1.1.10
IsFormatSupported checks if a MIME type is in the list of supported formats.
func MIMETypeToAudioFormat ¶ added in v1.1.10
MIMETypeToAudioFormat converts a MIME type to a format string.
func MIMETypeToFormat ¶
MIMETypeToFormat converts a MIME type to format string.
func SelectTargetFormat ¶ added in v1.1.10
SelectTargetFormat selects the best target format from supported formats. Prefers lossless formats (WAV) when available, then common formats (MP3).
Types ¶
type AudioConvertResult ¶ added in v1.1.10
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 ¶ added in v1.1.10
type AudioConverter struct {
// contains filtered or unexported fields
}
AudioConverter handles audio format conversion using ffmpeg.
func NewAudioConverter ¶ added in v1.1.10
func NewAudioConverter(config AudioConverterConfig) *AudioConverter
NewAudioConverter creates a new audio converter with the given config.
func (*AudioConverter) CanConvert ¶ added in v1.1.10
func (c *AudioConverter) CanConvert(fromMIME, toMIME string) bool
CanConvert checks if the converter can convert between the given formats.
func (*AudioConverter) ConvertAudio ¶ added in v1.1.10
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 ¶ added in v1.1.10
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 ¶ added in v1.1.10
func DefaultAudioConverterConfig() AudioConverterConfig
DefaultAudioConverterConfig returns sensible defaults for audio conversion.
type ContentConverter ¶ added in v1.1.10
type ContentConverter struct {
// contains filtered or unexported fields
}
ContentConverter handles conversion of MediaContent to match provider requirements.
func NewContentConverter ¶ added in v1.1.10
func NewContentConverter(config AudioConverterConfig) *ContentConverter
NewContentConverter creates a new content converter.
func (*ContentConverter) ConvertMediaContentIfNeeded ¶ added in v1.1.10
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 ¶ added in v1.1.10
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.