Documentation
¶
Index ¶
- Constants
- Variables
- func SetLogging(verbose bool, fn LogFn)
- type DecoderFrameFn
- type DecoderMapFunc
- type DecoderPacketFn
- type DecoderSubtitleFn
- type EncoderFrameFn
- type EncoderPacketFn
- type Filter
- type Frame
- func (frame *Frame) AllocateBuffers() error
- func (frame *Frame) Bytes(plane int) []byte
- func (frame *Frame) ChannelLayout() ff.AVChannelLayout
- func (frame *Frame) Close() error
- func (frame *Frame) Copy() (*Frame, error)
- func (frame *Frame) CopyPropsFromFrame(other *Frame) error
- func (frame *Frame) Float32(plane int) []float32
- func (frame *Frame) FrameRate() ff.AVRational
- func (frame *Frame) Height() int
- func (frame *Frame) IncPts(v int64)
- func (frame *Frame) Int16(plane int) []int16
- func (frame *Frame) IsAllocated() bool
- func (frame *Frame) MakeWritable() error
- func (frame *Frame) MarshalJSON() ([]byte, error)
- func (frame *Frame) MatchesFormat(other *Frame) bool
- func (frame *Frame) NumSamples() int
- func (frame *Frame) PixelFormat() ff.AVPixelFormat
- func (frame *Frame) Pts() int64
- func (frame *Frame) SampleAspectRatio() ff.AVRational
- func (frame *Frame) SampleFormat() ff.AVSampleFormat
- func (frame *Frame) SampleRate() int
- func (frame *Frame) SetFloat32(plane int, data []float32) error
- func (frame *Frame) SetPts(v int64)
- func (frame *Frame) SetTs(secs float64)
- func (frame *Frame) Stride(plane int) int
- func (frame *Frame) String() string
- func (frame *Frame) TimeBase() ff.AVRational
- func (frame *Frame) Ts() float64
- func (frame *Frame) Type() media.Type
- func (frame *Frame) Unref()
- func (frame *Frame) Width() int
- type LogFn
- type LogFunc
- type Metadata
- type Opt
- func OptCopy() Opt
- func OptForce() Opt
- func OptLog(verbose bool, fn LogFunc) Opt
- func OptMetadata(entry ...*Metadata) Opt
- func OptOutputFormat(name string) Opt
- func OptStream(stream int, par *Par) Opt
- func WithInput(format string, options ...string) Opt
- func WithInputFormat(iformat *ffmpeg.AVInputFormat, options ...string) Opt
- type Packet
- type Par
- func AudioPar(samplefmt string, channellayout string, samplerate int, opts ...media.Metadata) *Par
- func NewAudioPar(samplefmt string, channellayout string, samplerate int, opts ...media.Metadata) (*Par, error)
- func NewVideoPar(pixfmt string, size string, framerate float64, opts ...media.Metadata) (*Par, error)
- func VideoPar(pixfmt string, size string, framerate float64, opts ...media.Metadata) *Par
- func (par *Par) CopyToCodecContext(codec *ff.AVCodecContext) error
- func (par *Par) FrameRate() float64
- func (par *Par) MarshalJSON() ([]byte, error)
- func (par *Par) String() string
- func (par *Par) Type() media.Type
- func (par *Par) ValidateFromCodec(codec *ff.AVCodec) error
- func (par *Par) WidthHeight() string
- type Reader
- func (r *Reader) AVStreams() []*ff.AVStream
- func (r *Reader) BestStream(t media.Type) int
- func (r *Reader) Close() error
- func (r *Reader) Decode(ctx context.Context, packetfn DecoderPacketFn) error
- func (r *Reader) Demux(ctx context.Context, mapfn DecoderMapFunc, framefn DecoderFrameFn, ...) error
- func (r *Reader) Duration() time.Duration
- func (r *Reader) InputFormat() *ff.AVInputFormat
- func (r *Reader) MarshalJSON() ([]byte, error)
- func (r *Reader) Metadata(keys ...string) []*Metadata
- func (r *Reader) Seek(stream int, secs float64) error
- func (r *Reader) Streams(t media.Type) []*schema.Stream
- func (r *Reader) String() string
- func (r *Reader) Type() media.Type
- type Resampler
- type Writer
- func (w *Writer) Close() error
- func (w *Writer) EncodeFrame(stream int, frame *Frame) error
- func (w *Writer) EncodeFrames(stream int, frames <-chan *Frame) error
- func (w *Writer) MarshalJSON() ([]byte, error)
- func (w *Writer) Stream(stream int) *encoder
- func (w *Writer) String() string
- func (w *Writer) Type() media.Type
- func (w *Writer) Write(packet *Packet) error
- func (w *Writer) WritePackets(packets <-chan *Packet) error
Constants ¶
const ( PTS_UNDEFINED = ff.AV_NOPTS_VALUE TS_UNDEFINED = -1.0 )
const (
MetaArtwork = "artwork" // Metadata key for artwork, set the value as []byte
)
Variables ¶
var ErrCodecNotFound = errors.New("codec decoder not found")
ErrCodecNotFound is returned when a decoder cannot be found for a codec
Functions ¶
func SetLogging ¶ added in v1.6.10
Set logging options, including a callback function
Types ¶
type DecoderFrameFn ¶ added in v1.6.7
DecoderFrameFn is a function which is called to send a frame after decoding. It should return nil to continue decoding or io.EOF to stop.
type DecoderMapFunc ¶ added in v1.6.7
Return parameters if a stream should be decoded and either resampled or resized. Return nil if you want to ignore the stream, or pass back the stream parameters if you want to copy the stream without any changes.
type DecoderPacketFn ¶ added in v1.8.0
DecoderPacketFn is a function which is called to send a packet after demuxing. It should return nil to continue reading packets or io.EOF to stop. Use this for stream copying without decode/encode overhead.
type DecoderSubtitleFn ¶ added in v1.8.0
type DecoderSubtitleFn func(int, *ff.AVSubtitle) error
DecoderSubtitleFn is a function which is called to send a decoded subtitle. It should return nil to continue decoding or io.EOF to stop.
type EncoderFrameFn ¶
EncoderFrameFn is a function which is called to receive a frame to encode. It should return nil to continue encoding or io.EOF to stop encoding.
type EncoderPacketFn ¶
EncoderPacketFn is a function which is called for each packet encoded, with the stream timebase.
type Filter ¶ added in v1.8.0
type Filter struct {
// contains filtered or unexported fields
}
Filter applies a filter graph to frames for a single stream. Supports single input → filter chain → single output.
func NewFilter ¶ added in v1.8.0
Create a new filter which will apply the given filter specification to frames. filterSpec is an FFmpeg filter string (e.g., "scale=1280:720", "volume=0.5"). srcPar defines the input frame format, destPar defines the desired output format.
func (*Filter) Process ¶ added in v1.8.0
Process applies the filter to one frame and emits zero or more frames via fn. Pass src==nil to flush. fn may receive nil to signal end-of-batch.
IMPORTANT: The frame pointer passed to fn is only valid during the callback execution. The frame is freed immediately after fn returns. If you need to retain the frame data, you must clone it within the callback using AVUtil_frame_clone or copy its data. Do not store the frame pointer for later use.
type Frame ¶
func (*Frame) AllocateBuffers ¶ added in v1.6.7
Allocate buffers for the frame
func (*Frame) ChannelLayout ¶
func (frame *Frame) ChannelLayout() ff.AVChannelLayout
func (*Frame) Copy ¶ added in v1.6.7
Make a copy of the frame, which should be released by the caller
func (*Frame) CopyPropsFromFrame ¶ added in v1.6.7
Copy properties from another frame
func (*Frame) FrameRate ¶ added in v1.6.7
func (frame *Frame) FrameRate() ff.AVRational
func (*Frame) IsAllocated ¶ added in v1.6.7
Return true if the frame has allocated buffers
func (*Frame) MakeWritable ¶ added in v1.6.7
Make the frame writable
func (*Frame) MarshalJSON ¶
MarshalJSON implements json.Marshaler by delegating to the underlying AVFrame
func (*Frame) MatchesFormat ¶ added in v1.8.0
Returns true if this frame can be resampled/resized to match the other frame. Checks format compatibility (sample format, pixel format, channel layout, sample rate) but ignores sample count and framerate.
func (*Frame) NumSamples ¶
func (*Frame) PixelFormat ¶
func (frame *Frame) PixelFormat() ff.AVPixelFormat
func (*Frame) SampleAspectRatio ¶ added in v1.6.7
func (frame *Frame) SampleAspectRatio() ff.AVRational
func (*Frame) SampleFormat ¶
func (frame *Frame) SampleFormat() ff.AVSampleFormat
func (*Frame) SampleRate ¶ added in v1.6.7
func (*Frame) SetFloat32 ¶ added in v1.6.13
Set plane data from float32 slice
func (*Frame) TimeBase ¶ added in v1.6.7
func (frame *Frame) TimeBase() ff.AVRational
func (*Frame) Ts ¶ added in v1.6.7
Return the timestamp in seconds, or TS_UNDEFINED if the timestamp is undefined or timebase is not set
type LogFunc ¶ added in v1.6.7
type LogFunc func(string)
Logging function which is used to log messages
type Metadata ¶
type Metadata struct {
// contains filtered or unexported fields
}
func (*Metadata) MarshalJSON ¶ added in v1.8.0
type Opt ¶
type Opt func(*opts) error
Option which can affect the behaviour of ffmpeg
func OptCopy ¶ added in v1.8.0
func OptCopy() Opt
Enable stream copying mode (remuxing without encoding)
func OptForce ¶
func OptForce() Opt
Force resampling and resizing on decode, even if the input and output parameters are the same
func OptMetadata ¶
Append metadata to the output file, including artwork
func WithInput ¶ added in v1.8.0
WithInput sets the input format and format options for reading. The format parameter specifies the input format name (e.g., "s16le", "mp3"). If format is empty, only the options are set without changing the format. Additional options are key=value pairs (e.g., "sample_rate=22050", "channels=1").
Example:
WithInput("s16le", "sample_rate=22050", "channels=1", "sample_fmt=s16")
WithInput("", "analyzeduration=1000000") // Only set options
func WithInputFormat ¶ added in v1.8.1
func WithInputFormat(iformat *ffmpeg.AVInputFormat, options ...string) Opt
WithInputFormat sets the input format directly using an AVInputFormat pointer. This is useful for device formats that may not be found via AVFormat_find_input_format. Additional options are key=value pairs (e.g., "sample_rate=22050", "channels=1").
type Par ¶
type Par struct {
ff.AVCodecParameters
// contains filtered or unexported fields
}
func NewAudioPar ¶
func NewAudioPar(samplefmt string, channellayout string, samplerate int, opts ...media.Metadata) (*Par, error)
Create new audio parameters with sample format, channel layout and sample rate plus any additional options which is used for creating a stream
func NewVideoPar ¶
func NewVideoPar(pixfmt string, size string, framerate float64, opts ...media.Metadata) (*Par, error)
Create new video parameters with pixel format, frame size, framerate plus any additional options which is used for creating a stream
func (*Par) CopyToCodecContext ¶ added in v1.6.7
func (par *Par) CopyToCodecContext(codec *ff.AVCodecContext) error
func (*Par) MarshalJSON ¶
func (*Par) WidthHeight ¶ added in v1.6.7
type Reader ¶ added in v1.6.7
type Reader struct {
// contains filtered or unexported fields
}
Media reader which reads from a URL, file path or device
func (*Reader) BestStream ¶ added in v1.6.7
Return the "best stream" for a specific media type, or -1 if there is no "best stream" for that type.
func (*Reader) Decode ¶ added in v1.6.7
func (r *Reader) Decode(ctx context.Context, packetfn DecoderPacketFn) error
Decode packets from the media stream without decoding to frames. The packetfn is called for each packet read from any stream. Use this for stream copying or remuxing without transcoding.
The reading can be interrupted by cancelling the context, or by the packetfn returning an error or io.EOF. The latter will end the reading process early but will not return an error.
func (*Reader) Demux ¶ added in v1.8.0
func (r *Reader) Demux(ctx context.Context, mapfn DecoderMapFunc, framefn DecoderFrameFn, subtitlefn DecoderSubtitleFn) error
Demux and decode the media stream into frames and subtitles. The map function determines which streams to decode and what output parameters to use. The framefn is called for each decoded frame from any mapped stream. The optional subtitlefn is called for each decoded subtitle.
The decoding can be interrupted by cancelling the context, or by the framefn/subtitlefn returning an error or io.EOF. The latter will end the decoding process early but will not return an error.
func (*Reader) Duration ¶ added in v1.6.7
Return the duration of the media stream, returns zero if unknown
func (*Reader) InputFormat ¶ added in v1.8.0
func (r *Reader) InputFormat() *ff.AVInputFormat
Return the input format (for probing format info)
func (*Reader) MarshalJSON ¶ added in v1.6.7
Display the reader as a string
func (*Reader) Metadata ¶ added in v1.6.7
Return the metadata for the media stream, filtering by the specified keys if there are any. Artwork is returned with the "artwork" key.
func (*Reader) Streams ¶ added in v1.7.3
Return all streams of a specific type (video, audio, subtitle, data) Use media.ANY to return all streams regardless of type
type Resampler ¶ added in v1.8.0
type Resampler struct {
// contains filtered or unexported fields
}
Resampler handles audio resampling or video rescaling for a single stream.
func NewResampler ¶
Create a new resampler which will resample or rescale frames to the specified parameters
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer creates media files with multiple streams. Supports encoding frames or writing pre-encoded packets. For multiple streams, frames/packets are automatically interleaved by the muxer (AVFormat_interleaved_write_frame), but best results are achieved by sending frames in roughly temporal order to minimize buffering.
Multi-stream Encoding Best Practices ¶
When encoding multiple streams (e.g., video + audio), you have two options:
Temporal Interleaving (Recommended for streaming): Compare timestamps across streams and encode the frame with the earliest timestamp. This minimizes muxer buffering and is ideal for live streaming scenarios. See Test_encode_multiple_streams_interleaved_mp4 for an example.
Batch by Stream (Simpler, works for files): Encode all frames for one stream, then the next. The muxer will buffer and reorder. This is simpler but uses more memory and adds latency. See Test_encode_multiple_streams_mp4 for an example.
Both approaches work correctly - the muxer handles interleaving automatically. Choose based on your use case (streaming vs file output, memory constraints, etc).
func (*Writer) EncodeFrame ¶ added in v1.8.0
EncodeFrame encodes a single frame and writes the resulting packet(s) to the output (synchronous). The frame should have the correct format for the stream's encoder. Pass nil to flush the encoder. Returns nil on success or error. Automatically writes artwork on first frame if available.
func (*Writer) EncodeFrames ¶ added in v1.8.0
EncodeFrames encodes frames from a channel and writes the resulting packets to the output (asynchronous). This method blocks until the channel is closed or an error occurs. Returns nil when the channel is closed normally, or an error if encoding/writing fails. When the channel closes, the encoder is automatically flushed. Automatically writes artwork before first frame if available.
Ownership: EncodeFrames assumes ownership of frames received from the channel and will Close() each frame after it has been sent to the encoder.
func (*Writer) MarshalJSON ¶ added in v1.6.7
Display the writer as a string
func (*Writer) Write ¶
Write a packet to the output (synchronous). The packet should have the stream index, PTS, DTS, duration, and timebase set correctly. Returns nil on success or error. Packets are automatically interleaved for proper ordering.
func (*Writer) WritePackets ¶ added in v1.8.0
WritePackets reads packets from a channel and writes them to the output (asynchronous). This method blocks until the channel is closed or an error occurs. Returns nil when the channel is closed normally, or an error if writing fails. Packets are automatically interleaved for proper ordering.