Documentation
¶
Overview ¶
Package media provides unified multi-provider image and video generation.
Index ¶
- Variables
- func AudioExtensionFromMIME(mime string) string
- func ConvertImage(data []byte, target Format) ([]byte, error)
- func DetectAudioMIMEFromBytes(data []byte) string
- func DetectMIMEFromBytes(data []byte) string
- func PCMToWAV(pcm []byte, sampleRate, channels, bitsPerSample int) []byte
- func RegisterImage(entry ImageProviderEntry)
- func RegisterTextToSpeech(entry TextToSpeechProviderEntry)
- func RegisterTranscription(entry TranscriptionProviderEntry)
- func RegisterVideo(entry VideoProviderEntry)
- func SlugifyPrompt(prompt string, maxLen int) string
- func StandardImageDimensions(ar AspectRatio) (width, height int)
- func StandardVideoDimensions(ar AspectRatio) (width, height int)
- func UniquePath(path string) string
- func ValidateAudioFormat(f AudioFormat) error
- func ValidateFormat(f Format) error
- type AspectRatio
- type AudioFormat
- type AudioResult
- type Config
- type Format
- type ImageEditor
- type ImageProvider
- type ImageProviderEntry
- type ImageProviderFactory
- type ImageResult
- func EditImage(ctx context.Context, prompt string, opts ...Option) (*ImageResult, error)
- func GenerateImage(ctx context.Context, prompt string, opts ...Option) (*ImageResult, error)
- func GenerateImageBatch(ctx context.Context, prompt string, opts ...Option) ([]*ImageResult, error)
- func GenerateImages(ctx context.Context, prompt string, opts ...Option) ([]*ImageResult, error)
- type ModelMatcher
- type Option
- func WithAspectRatio(ar AspectRatio) Option
- func WithAudioFormat(f AudioFormat) Option
- func WithAudioMIMEType(mimeType string) Option
- func WithCount(n int) Option
- func WithDuration(d time.Duration) Option
- func WithLanguage(language string) Option
- func WithModel(model string) Option
- func WithModels(models ...string) Option
- func WithOutputFormat(f Format) Option
- func WithReferenceImage(data []byte) Option
- func WithSpeechSpeed(speed float64) Option
- func WithTimeout(d time.Duration) Option
- func WithTranscriptionPrompt(prompt string) Option
- func WithVoice(voice string) Option
- func WithVoiceInstructions(instructions string) Option
- type Registry
- func (r *Registry) ImageEntries() []ImageProviderEntry
- func (r *Registry) RegisterImage(entry ImageProviderEntry)
- func (r *Registry) RegisterTextToSpeech(entry TextToSpeechProviderEntry)
- func (r *Registry) RegisterTranscription(entry TranscriptionProviderEntry)
- func (r *Registry) RegisterVideo(entry VideoProviderEntry)
- func (r *Registry) ResolveImage(model string) (ImageProvider, error)
- func (r *Registry) ResolveTextToSpeech(model string) (TextToSpeechProvider, error)
- func (r *Registry) ResolveTranscription(model string) (TranscriptionProvider, error)
- func (r *Registry) ResolveVideo(model string) (VideoProvider, error)
- func (r *Registry) TextToSpeechEntries() []TextToSpeechProviderEntry
- func (r *Registry) TranscriptionEntries() []TranscriptionProviderEntry
- func (r *Registry) VideoEntries() []VideoProviderEntry
- type TextToSpeechProvider
- type TextToSpeechProviderEntry
- type TextToSpeechProviderFactory
- type TranscriptionProvider
- type TranscriptionProviderEntry
- type TranscriptionProviderFactory
- type TranscriptionResult
- type VideoProvider
- type VideoProviderEntry
- type VideoProviderFactory
- type VideoResult
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoModel is returned when no model is specified. ErrNoModel = errors.New("media: no model specified") // ErrProviderNotFound is returned when no provider matches the model name. ErrProviderNotFound = errors.New("media: no provider found for model") // ErrEditNotSupported is returned when a provider does not support editing. ErrEditNotSupported = errors.New("media: provider does not support image editing") // ErrTimeout is returned when generation exceeds the timeout. ErrTimeout = errors.New("media: generation timed out") // ErrNoResult is returned when a provider returns no results. ErrNoResult = errors.New("media: provider returned no results") )
Functions ¶
func AudioExtensionFromMIME ¶ added in v1.7.0
AudioExtensionFromMIME returns a suitable file extension for a MIME type.
func ConvertImage ¶ added in v1.5.0
ConvertImage re-encodes image data to the target format. Supports PNG and JPEG targets. Returns an error for WebP target (no Go stdlib encoder). If source format matches target, returns data unchanged.
func DetectAudioMIMEFromBytes ¶ added in v1.7.0
DetectAudioMIMEFromBytes returns the MIME type based on common audio magic bytes. It falls back to audio/mpeg because MP3 is the most common compressed input.
func DetectMIMEFromBytes ¶ added in v1.5.0
DetectMIMEFromBytes returns the MIME type based on magic bytes.
func PCMToWAV ¶ added in v1.7.0
PCMToWAV wraps signed 16-bit little-endian PCM data in a WAV container.
func RegisterImage ¶ added in v1.5.0
func RegisterImage(entry ImageProviderEntry)
RegisterImage adds an image provider entry to the default registry.
func RegisterTextToSpeech ¶ added in v1.7.0
func RegisterTextToSpeech(entry TextToSpeechProviderEntry)
RegisterTextToSpeech adds a text-to-speech provider entry to the default registry.
func RegisterTranscription ¶ added in v1.7.0
func RegisterTranscription(entry TranscriptionProviderEntry)
RegisterTranscription adds a transcription provider entry to the default registry.
func RegisterVideo ¶ added in v1.5.0
func RegisterVideo(entry VideoProviderEntry)
RegisterVideo adds a video provider entry to the default registry.
func SlugifyPrompt ¶ added in v1.5.0
SlugifyPrompt generates a short filename-safe slug from a prompt string.
func StandardImageDimensions ¶ added in v1.5.0
func StandardImageDimensions(ar AspectRatio) (width, height int)
StandardImageDimensions returns default pixel dimensions for an aspect ratio. Returns (1024, 1024) for unrecognized ratios.
func StandardVideoDimensions ¶ added in v1.5.0
func StandardVideoDimensions(ar AspectRatio) (width, height int)
StandardVideoDimensions returns default pixel dimensions for video at the given aspect ratio. Returns (1920, 1080) for unrecognized ratios.
func UniquePath ¶ added in v1.5.0
UniquePath returns path unchanged if it does not exist. If it does exist, a numeric suffix is inserted before the extension (e.g., "photo1.png", "photo2.png") until an available name is found.
func ValidateAudioFormat ¶ added in v1.7.0
func ValidateAudioFormat(f AudioFormat) error
ValidateAudioFormat returns an error if the format is not recognized.
func ValidateFormat ¶ added in v1.5.0
ValidateFormat returns an error if the format is not a recognized value.
Types ¶
type AspectRatio ¶ added in v1.5.0
type AspectRatio string
AspectRatio represents the aspect ratio for generated media.
const ( // AspectAuto lets the provider choose a default aspect ratio. AspectAuto AspectRatio = "" Aspect1x1 AspectRatio = "1:1" Aspect16x9 AspectRatio = "16:9" Aspect9x16 AspectRatio = "9:16" Aspect4x3 AspectRatio = "4:3" Aspect3x4 AspectRatio = "3:4" Aspect4x1 AspectRatio = "4:1" Aspect1x4 AspectRatio = "1:4" )
func (AspectRatio) String ¶ added in v1.5.0
func (ar AspectRatio) String() string
String returns the string representation of the aspect ratio.
type AudioFormat ¶ added in v1.7.0
type AudioFormat string
AudioFormat represents an audio output format.
const ( AudioFormatMP3 AudioFormat = "mp3" AudioFormatOpus AudioFormat = "opus" AudioFormatAAC AudioFormat = "aac" AudioFormatFLAC AudioFormat = "flac" AudioFormatWAV AudioFormat = "wav" AudioFormatPCM AudioFormat = "pcm" )
func AudioFormatFromMIME ¶ added in v1.7.0
func AudioFormatFromMIME(mime string) AudioFormat
AudioFormatFromMIME returns the AudioFormat corresponding to a MIME type.
func (AudioFormat) FileExtension ¶ added in v1.7.0
func (f AudioFormat) FileExtension() string
FileExtension returns the file extension (with dot) for the audio format.
func (AudioFormat) MIMEType ¶ added in v1.7.0
func (f AudioFormat) MIMEType() string
MIMEType returns the MIME type string for the audio format.
func (AudioFormat) String ¶ added in v1.7.0
func (f AudioFormat) String() string
String returns the string representation.
type AudioResult ¶ added in v1.7.0
type AudioResult struct {
// Data is the raw audio bytes.
Data []byte
// Model is the model that generated this audio.
Model string
// Format is the audio container or codec format.
Format AudioFormat
// MimeType is the MIME type of the audio.
MimeType string
// Duration is the audio duration when known.
Duration time.Duration
// Metadata contains provider-specific metadata.
Metadata map[string]any
}
AudioResult is the output of a text-to-speech operation.
func TextToSpeech ¶ added in v1.7.0
TextToSpeech generates spoken audio from text.
func (*AudioResult) SetAudioFormat ¶ added in v1.7.0
func (r *AudioResult) SetAudioFormat(mimeType string)
SetAudioFormat sets format fields from a MIME type.
func (*AudioResult) WriteTo ¶ added in v1.7.0
func (r *AudioResult) WriteTo(path string) (string, error)
WriteTo writes the audio data to the given file path. If the path has no extension, the format's extension is appended. If the path already exists, a numeric suffix is appended to avoid overwriting.
type Config ¶ added in v1.5.0
type Config struct {
// Model is the model to use for generation.
Model string
// Models is used for fan-out generation across multiple models.
Models []string
// AspectRatio controls the output dimensions.
AspectRatio AspectRatio
// OutputFormat requests a specific image format (png, jpeg, webp).
OutputFormat Format
// AudioFormat requests a specific audio output format.
AudioFormat AudioFormat
// AudioMIMEType hints the MIME type of input audio bytes.
AudioMIMEType string
// Voice selects the voice for text-to-speech.
Voice string
// VoiceInstructions controls voice style for text-to-speech when supported.
VoiceInstructions string
// SpeechSpeed controls generated speech speed when supported.
SpeechSpeed *float64
// Language sets the transcription or speech language when supported.
Language string
// TranscriptionPrompt gives transcription models context.
TranscriptionPrompt string
// Count is the number of images to generate per model.
Count int
// ReferenceImages are input images for editing operations.
ReferenceImages [][]byte
// Duration is the target video duration.
Duration time.Duration
// Timeout is the maximum time to wait for generation.
// Defaults to 5 minutes for images, 15 minutes for video.
Timeout time.Duration
}
Config holds the resolved options for a media generation call.
type Format ¶ added in v1.5.0
type Format string
Format represents an image output format.
func DetectFormat ¶ added in v1.5.0
DetectFormat inspects magic bytes and returns the image format. Returns FormatPNG if the format cannot be determined.
func FormatFromMIME ¶ added in v1.5.0
FormatFromMIME returns the Format corresponding to a MIME type string.
func (Format) FileExtension ¶ added in v1.5.0
FileExtension returns the file extension (with dot) for the format.
type ImageEditor ¶ added in v1.5.0
type ImageEditor interface {
// EditImage edits reference images according to the prompt.
// Reference images are passed via config.ReferenceImages.
EditImage(ctx context.Context, prompt string, config *Config) ([]*ImageResult, error)
}
ImageEditor edits images using a text prompt and reference images. Providers that support editing implement this in addition to ImageProvider.
type ImageProvider ¶ added in v1.5.0
type ImageProvider interface {
// GenerateImage generates one or more images from a prompt.
// The number of images is controlled by config.Count.
GenerateImage(ctx context.Context, prompt string, config *Config) ([]*ImageResult, error)
}
ImageProvider generates images from text prompts.
type ImageProviderEntry ¶ added in v1.5.0
type ImageProviderEntry struct {
Name string
Match ModelMatcher
Factory ImageProviderFactory
}
ImageProviderEntry pairs a matcher with its factory.
type ImageProviderFactory ¶ added in v1.5.0
type ImageProviderFactory func(model string) ImageProvider
ImageProviderFactory creates an ImageProvider for a given model name.
type ImageResult ¶ added in v1.5.0
type ImageResult struct {
// Data is the raw image bytes.
Data []byte
// Model is the model that generated this image.
Model string
// Format is the detected image format (png, jpeg, webp).
Format Format
// MimeType is the MIME type of the image.
MimeType string
// Width is the image width in pixels.
Width int
// Height is the image height in pixels.
Height int
// Metadata contains provider-specific metadata.
Metadata map[string]any
// Err is non-nil if this result represents a provider failure
// during fan-out generation. Other fields may be empty.
Err error
}
ImageResult is the output of an image generation or edit operation.
func EditImage ¶ added in v1.5.0
EditImage edits a reference image using a text prompt. Requires WithReferenceImage(). Returns ErrEditNotSupported if the provider does not implement image editing.
func GenerateImage ¶ added in v1.5.0
GenerateImage generates an image using a single provider. Returns the first result when config.Count is 1, or call with WithCount(n) to generate multiple images.
func GenerateImageBatch ¶ added in v1.5.0
GenerateImageBatch generates multiple images using a single provider. Use WithCount(n) to control how many images are generated.
func GenerateImages ¶ added in v1.5.0
GenerateImages fans out the same prompt to multiple models concurrently. Requires WithModels(). Returns one result per model. Individual provider failures are captured in ImageResult.Err rather than failing the entire call.
func (*ImageResult) WriteTo ¶ added in v1.5.0
func (r *ImageResult) WriteTo(path string) (string, error)
WriteTo writes the image data to the given file path. If the path has no extension, the format's extension is appended. If the path already exists, a numeric suffix is appended to avoid overwriting (e.g., "photo1.png", "photo2.png").
type ModelMatcher ¶ added in v1.5.0
ModelMatcher determines if a model name matches a provider.
func PrefixMatcher ¶ added in v1.5.0
func PrefixMatcher(prefix string) ModelMatcher
PrefixMatcher returns a matcher that checks for a case-insensitive prefix.
func PrefixesMatcher ¶ added in v1.5.0
func PrefixesMatcher(prefixes ...string) ModelMatcher
PrefixesMatcher returns a matcher that checks for any of the given prefixes (case-insensitive).
type Option ¶ added in v1.5.0
type Option func(*Config)
Option configures a media generation call.
func WithAspectRatio ¶ added in v1.5.0
func WithAspectRatio(ar AspectRatio) Option
WithAspectRatio sets the aspect ratio.
func WithAudioFormat ¶ added in v1.7.0
func WithAudioFormat(f AudioFormat) Option
WithAudioFormat sets the desired audio output format.
func WithAudioMIMEType ¶ added in v1.7.0
WithAudioMIMEType sets the MIME type for input audio bytes.
func WithDuration ¶ added in v1.5.0
WithDuration sets the target video duration.
func WithLanguage ¶ added in v1.7.0
WithLanguage sets the language for transcription or text-to-speech.
func WithModels ¶ added in v1.5.0
WithModels sets multiple models for fan-out generation.
func WithOutputFormat ¶ added in v1.5.0
WithOutputFormat sets the desired output format.
func WithReferenceImage ¶ added in v1.5.0
WithReferenceImage adds a reference image for editing operations.
func WithSpeechSpeed ¶ added in v1.7.0
WithSpeechSpeed sets the generated speech speed when supported.
func WithTimeout ¶ added in v1.5.0
WithTimeout sets the maximum time to wait for generation.
func WithTranscriptionPrompt ¶ added in v1.7.0
WithTranscriptionPrompt gives transcription models context.
func WithVoiceInstructions ¶ added in v1.7.0
WithVoiceInstructions sets style instructions for text-to-speech.
type Registry ¶ added in v1.5.0
type Registry struct {
// contains filtered or unexported fields
}
Registry manages model-to-provider mappings for media generation.
func DefaultRegistry ¶ added in v1.5.0
func DefaultRegistry() *Registry
DefaultRegistry returns the default global registry.
func (*Registry) ImageEntries ¶ added in v1.5.0
func (r *Registry) ImageEntries() []ImageProviderEntry
ImageEntries returns a copy of all registered image provider entries.
func (*Registry) RegisterImage ¶ added in v1.5.0
func (r *Registry) RegisterImage(entry ImageProviderEntry)
RegisterImage adds an image provider entry to the registry.
func (*Registry) RegisterTextToSpeech ¶ added in v1.7.0
func (r *Registry) RegisterTextToSpeech(entry TextToSpeechProviderEntry)
RegisterTextToSpeech adds a text-to-speech provider entry to the registry.
func (*Registry) RegisterTranscription ¶ added in v1.7.0
func (r *Registry) RegisterTranscription(entry TranscriptionProviderEntry)
RegisterTranscription adds a transcription provider entry to the registry.
func (*Registry) RegisterVideo ¶ added in v1.5.0
func (r *Registry) RegisterVideo(entry VideoProviderEntry)
RegisterVideo adds a video provider entry to the registry.
func (*Registry) ResolveImage ¶ added in v1.5.0
func (r *Registry) ResolveImage(model string) (ImageProvider, error)
ResolveImage returns an ImageProvider for the given model name.
func (*Registry) ResolveTextToSpeech ¶ added in v1.7.0
func (r *Registry) ResolveTextToSpeech(model string) (TextToSpeechProvider, error)
ResolveTextToSpeech returns a TextToSpeechProvider for the given model name.
func (*Registry) ResolveTranscription ¶ added in v1.7.0
func (r *Registry) ResolveTranscription(model string) (TranscriptionProvider, error)
ResolveTranscription returns a TranscriptionProvider for the given model name.
func (*Registry) ResolveVideo ¶ added in v1.5.0
func (r *Registry) ResolveVideo(model string) (VideoProvider, error)
ResolveVideo returns a VideoProvider for the given model name.
func (*Registry) TextToSpeechEntries ¶ added in v1.7.0
func (r *Registry) TextToSpeechEntries() []TextToSpeechProviderEntry
TextToSpeechEntries returns a copy of all registered text-to-speech provider entries.
func (*Registry) TranscriptionEntries ¶ added in v1.7.0
func (r *Registry) TranscriptionEntries() []TranscriptionProviderEntry
TranscriptionEntries returns a copy of all registered transcription provider entries.
func (*Registry) VideoEntries ¶ added in v1.5.0
func (r *Registry) VideoEntries() []VideoProviderEntry
VideoEntries returns a copy of all registered video provider entries.
type TextToSpeechProvider ¶ added in v1.7.0
type TextToSpeechProvider interface {
// TextToSpeech generates audio from text.
TextToSpeech(ctx context.Context, text string, config *Config) (*AudioResult, error)
}
TextToSpeechProvider generates spoken audio from text.
type TextToSpeechProviderEntry ¶ added in v1.7.0
type TextToSpeechProviderEntry struct {
Name string
Match ModelMatcher
Factory TextToSpeechProviderFactory
}
TextToSpeechProviderEntry pairs a matcher with its factory.
type TextToSpeechProviderFactory ¶ added in v1.7.0
type TextToSpeechProviderFactory func(model string) TextToSpeechProvider
TextToSpeechProviderFactory creates a TextToSpeechProvider for a given model name.
type TranscriptionProvider ¶ added in v1.7.0
type TranscriptionProvider interface {
// Transcribe transcribes audio bytes into text.
Transcribe(ctx context.Context, audio []byte, config *Config) (*TranscriptionResult, error)
}
TranscriptionProvider transcribes speech audio into text.
type TranscriptionProviderEntry ¶ added in v1.7.0
type TranscriptionProviderEntry struct {
Name string
Match ModelMatcher
Factory TranscriptionProviderFactory
}
TranscriptionProviderEntry pairs a matcher with its factory.
type TranscriptionProviderFactory ¶ added in v1.7.0
type TranscriptionProviderFactory func(model string) TranscriptionProvider
TranscriptionProviderFactory creates a TranscriptionProvider for a given model name.
type TranscriptionResult ¶ added in v1.7.0
type TranscriptionResult struct {
// Text is the recognized transcript.
Text string
// Model is the model that transcribed the audio.
Model string
// Language is the recognized or requested language when known.
Language string
// Duration is the input audio duration when known.
Duration time.Duration
// Metadata contains provider-specific metadata.
Metadata map[string]any
}
TranscriptionResult is the output of a transcription operation.
func Transcribe ¶ added in v1.7.0
Transcribe transcribes speech audio bytes into text.
type VideoProvider ¶ added in v1.5.0
type VideoProvider interface {
// GenerateVideo generates a video from a prompt.
// The call blocks until generation is complete or ctx is cancelled.
GenerateVideo(ctx context.Context, prompt string, config *Config) (*VideoResult, error)
}
VideoProvider generates videos from text prompts.
type VideoProviderEntry ¶ added in v1.5.0
type VideoProviderEntry struct {
Name string
Match ModelMatcher
Factory VideoProviderFactory
}
VideoProviderEntry pairs a matcher with its factory.
type VideoProviderFactory ¶ added in v1.5.0
type VideoProviderFactory func(model string) VideoProvider
VideoProviderFactory creates a VideoProvider for a given model name.
type VideoResult ¶ added in v1.5.0
type VideoResult struct {
// Data is the raw video bytes.
Data []byte
// Model is the model that generated this video.
Model string
// Format is the video container format (mp4, webm).
Format string
// MimeType is the MIME type of the video.
MimeType string
// Width is the video width in pixels.
Width int
// Height is the video height in pixels.
Height int
// Duration is the video duration.
Duration time.Duration
// AspectRatio is the video aspect ratio.
AspectRatio AspectRatio
// Metadata contains provider-specific metadata.
Metadata map[string]any
}
VideoResult is the output of a video generation operation.
func GenerateVideo ¶ added in v1.5.0
GenerateVideo generates a video from a text prompt. Blocks until generation is complete or the context is cancelled.
func (*VideoResult) SetVideoFormat ¶ added in v1.5.0
func (r *VideoResult) SetVideoFormat(mimeType string)
SetVideoFormat sets format fields from a MIME type.
func (*VideoResult) WriteTo ¶ added in v1.5.0
func (r *VideoResult) WriteTo(path string) (string, error)
WriteTo writes the video data to the given file path. If the path has no extension, ".mp4" is appended. If the path already exists, a numeric suffix is appended to avoid overwriting (e.g., "clip1.mp4", "clip2.mp4").