ffmpeg

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PTS_UNDEFINED = ff.AV_NOPTS_VALUE
	TS_UNDEFINED  = -1.0
)
View Source
const (
	MetaArtwork = "artwork" // Metadata key for artwork, set the value as []byte
)

Variables

This section is empty.

Functions

func NewResampler

func NewResampler(par *Par, force bool) (*resampler, error)

Create a new audio resampler which will resample the input frame to the specified channel layout, sample rate and sample format.

func NewRescaler

func NewRescaler(par *Par, force bool) (*rescaler, error)

Create a new rescaler which will rescale the input frame to the specified format, width and height.

func SetLogging

func SetLogging(verbose bool, fn LogFn)

Set logging options, including a callback function

Types

type Channel

type Channel ff.AVChannel

func (*Channel) Description

func (ch *Channel) Description() string

func (*Channel) MarshalJSON

func (ch *Channel) MarshalJSON() ([]byte, error)

func (*Channel) Name

func (ch *Channel) Name() string

func (*Channel) String

func (ch *Channel) String() string

type ChannelLayout

type ChannelLayout ff.AVChannelLayout

func (*ChannelLayout) Channels

func (ch *ChannelLayout) Channels() []*Channel

func (*ChannelLayout) MarshalJSON

func (ch *ChannelLayout) MarshalJSON() ([]byte, error)

func (*ChannelLayout) Name

func (ch *ChannelLayout) Name() string

func (*ChannelLayout) NumChannels

func (ch *ChannelLayout) NumChannels() int

func (*ChannelLayout) Order

func (ch *ChannelLayout) Order() string

func (*ChannelLayout) String

func (ch *ChannelLayout) String() string

type Codec

type Codec ff.AVCodec

func (*Codec) ChannelLayouts

func (codec *Codec) ChannelLayouts() []string

Channel layouts supported by the codec. This is only valid for audio codecs.

func (*Codec) Description

func (codec *Codec) Description() string

The description of the codec

func (*Codec) MarshalJSON

func (codec *Codec) MarshalJSON() ([]byte, error)

func (*Codec) Name

func (codec *Codec) Name() string

The name the codec is referred to by

func (*Codec) PixelFormats

func (codec *Codec) PixelFormats() []string

Pixel formats supported by the codec. This is only valid for video codecs. The first pixel format is the default.

func (*Codec) Profiles

func (codec *Codec) Profiles() []string

Profiles supported by the codec. This is only valid for video codecs.

func (*Codec) SampleFormats

func (codec *Codec) SampleFormats() []string

Sample formats supported by the codec. This is only valid for audio codecs. The first sample format is the default.

func (*Codec) SampleRates

func (codec *Codec) SampleRates() []int

Sample rates supported by the codec. This is only valid for audio codecs. The first sample rate is the highest, sort the list in reverse order.

func (*Codec) String

func (codec *Codec) String() string

func (*Codec) Type

func (codec *Codec) Type() media.Type

Return the type of codec

type Context

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

Decoding context

func (*Context) C

func (decoder *Context) C(stream int) chan *Frame

func (*Context) Close

func (c *Context) Close() error

Release resources for the decoding context

type Decoder

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

func NewDecoder

func NewDecoder(stream *ff.AVStream, dest *Par, force bool) (*Decoder, error)

Create a stream decoder which can decode packets from the input stream

func (*Decoder) Close

func (d *Decoder) Close() error

Close the decoder and free any resources

type DecoderFrameFn

type DecoderFrameFn func(int, *Frame) error

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

type DecoderMapFunc func(int, *Par) (*Par, error)

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 Device

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

type Encoder

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

func NewEncoder

func NewEncoder(ctx *ff.AVFormatContext, stream int, par *Par) (*Encoder, error)

Create an encoder with the given parameters

func (*Encoder) Close

func (encoder *Encoder) Close() error

func (*Encoder) Encode

func (e *Encoder) Encode(frame *Frame, fn EncoderPacketFn) error

Encode a frame and pass packets to the EncoderPacketFn. If the frame is nil, then the encoder will flush any remaining packets. If io.EOF is returned then it indicates that the encoder has ended prematurely.

func (*Encoder) MarshalJSON

func (e *Encoder) MarshalJSON() ([]byte, error)

func (*Encoder) Par

func (e *Encoder) Par() *Par

Return the codec parameters

func (*Encoder) String

func (e *Encoder) String() string

type EncoderFrameFn

type EncoderFrameFn func(int) (*Frame, error)

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

type EncoderPacketFn func(*Packet) error

EncoderPacketFn is a function which is called for each packet encoded, with the stream timebase.

type Format

type Format struct {
	Input   *ff.AVInputFormat  `json:"input,omitempty"`
	Output  *ff.AVOutputFormat `json:"output,omitempty"`
	Devices []*Device          `json:"devices,omitempty"`
	// contains filtered or unexported fields
}

func (*Format) Description

func (f *Format) Description() string

func (*Format) Name

func (f *Format) Name() string

func (*Format) String

func (f *Format) String() string

func (*Format) Type

func (f *Format) Type() media.Type

type Frame

type Frame ff.AVFrame

func NewFrame

func NewFrame(par *Par) (*Frame, error)

Create a new frame and optionally set audio or video parameters

func (*Frame) AllocateBuffers

func (frame *Frame) AllocateBuffers() error

Allocate buffers for the frame

func (*Frame) Bytes

func (frame *Frame) Bytes(plane int) []byte

Return plane data as a byte slice

func (*Frame) ChannelLayout

func (frame *Frame) ChannelLayout() ff.AVChannelLayout

func (*Frame) Close

func (frame *Frame) Close() error

Release frame resources

func (*Frame) Copy

func (frame *Frame) Copy() (*Frame, error)

Make a copy of the frame, which should be released by the caller

func (*Frame) CopyPropsFromFrame

func (frame *Frame) CopyPropsFromFrame(other *Frame) error

Copy properties from another frame

func (*Frame) Float32

func (frame *Frame) Float32(plane int) []float32

Return plane data as a float32 slice

func (*Frame) FrameRate

func (frame *Frame) FrameRate() ff.AVRational

func (*Frame) FrameSize

func (frame *Frame) FrameSize() int

func (*Frame) FromImage

func (frame *Frame) FromImage(src image.Image) error

Repurpose a frame and copy image into it. TODO: Support SampleAspectRatio?

func (*Frame) Height

func (frame *Frame) Height() int

func (*Frame) Image

func (frame *Frame) Image() (image.Image, error)

Create an image from a frame. The frame should not be unreferenced until the image is no longer required, but the image can be discarded TODO: Add a copy flag which copies the memory?

func (*Frame) IncPts

func (frame *Frame) IncPts(v int64)

Increment presentation timestamp

func (*Frame) Int16

func (frame *Frame) Int16(plane int) []int16

Return plane data as a int16 slice

func (*Frame) IsAllocated

func (frame *Frame) IsAllocated() bool

Return true if the frame has allocated buffers

func (*Frame) MakeWritable

func (frame *Frame) MakeWritable() error

Make the frame writable

func (*Frame) NumSamples

func (frame *Frame) NumSamples() int

func (*Frame) PixelFormat

func (frame *Frame) PixelFormat() ff.AVPixelFormat

func (*Frame) Pts

func (frame *Frame) Pts() int64

Return the presentation timestamp in int64

func (*Frame) SampleAspectRatio

func (frame *Frame) SampleAspectRatio() ff.AVRational

func (*Frame) SampleFormat

func (frame *Frame) SampleFormat() ff.AVSampleFormat

func (*Frame) SampleRate

func (frame *Frame) SampleRate() int

func (*Frame) SetFloat32

func (frame *Frame) SetFloat32(plane int, data []float32) error

Set plane data from float32 slice

func (*Frame) SetPts

func (frame *Frame) SetPts(v int64)

Set the presentation timestamp in int64

func (*Frame) SetTs

func (frame *Frame) SetTs(secs float64)

Set timestamp in seconds

func (*Frame) Stride

func (frame *Frame) Stride(plane int) int

Return the stride for a plane (number of bytes in a row)

func (*Frame) String

func (frame *Frame) String() string

func (*Frame) TimeBase

func (frame *Frame) TimeBase() ff.AVRational

func (*Frame) Ts

func (frame *Frame) Ts() float64

Return the timestamp in seconds, or TS_UNDEFINED if the timestamp is undefined or timebase is not set

func (*Frame) Type

func (frame *Frame) Type() media.Type

Return frame type - AUDIO or VIDEO. Other types are not yet identified and returned as UNKNOWN

func (*Frame) Unref

func (frame *Frame) Unref()

Unreference frame buffers

func (*Frame) Width

func (frame *Frame) Width() int

type LogFn

type LogFn func(text string)

Logging function

type LogFunc

type LogFunc func(string)

Logging function which is used to log messages

type Manager

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

func NewManager

func NewManager(opt ...Opt) (*Manager, error)

Create a new media manager which enumerates the available codecs, formats and devices

func (*Manager) ChannelLayouts

func (manager *Manager) ChannelLayouts() []media.Metadata

Return standard channel layouts which can be used for audio

func (*Manager) Codecs

func (manager *Manager) Codecs(t media.Type, name ...string) []media.Metadata

Return all supported codecs, of a specific type or all if ANY is used. If any names is provided, then only the codecs with those names are returned. Codecs can be AUDIO, VIDEO and SUBTITLE

func (*Manager) Create

func (manager *Manager) Create(url string, format media.Format, meta []media.Metadata, streams ...media.Par) (media.Media, error)

func (*Manager) Decode

func (manager *Manager) Decode(ctx context.Context, m media.Media, mapFunc media.MapFunc, frameFunc media.FrameFunc) error

func (*Manager) Errorf

func (manager *Manager) Errorf(v string, args ...any)

Log error messages

func (*Manager) Formats

func (manager *Manager) Formats(t media.Type, name ...string) []media.Format

func (*Manager) Infof

func (manager *Manager) Infof(v string, args ...any)

Log info messages

func (*Manager) Open

func (manager *Manager) Open(url string, format media.Format, opts ...string) (media.Media, error)

func (*Manager) PixelFormats

func (manager *Manager) PixelFormats() []media.Metadata

Return all supported pixel formats

func (*Manager) Read

func (manager *Manager) Read(r io.Reader, format media.Format, opts ...string) (media.Media, error)

func (*Manager) SampleFormats

func (manager *Manager) SampleFormats() []media.Metadata

Return all supported sample formats

func (*Manager) Warningf

func (manager *Manager) Warningf(v string, args ...any)

Log warning messages

type Metadata

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

func NewMetadata

func NewMetadata(key string, value any) *Metadata

Metadata implementation

func (*Metadata) Any

func (m *Metadata) Any() any

Returns the value as an interface

func (*Metadata) Bytes

func (m *Metadata) Bytes() []byte

Returns the value as a byte slice

func (*Metadata) Image

func (m *Metadata) Image() image.Image

Returns the value as an image

func (*Metadata) Key

func (m *Metadata) Key() string

func (*Metadata) String

func (m *Metadata) String() string

func (*Metadata) Value

func (m *Metadata) Value() string

Value returns the value as a string. If the value is a byte slice, it will return the mimetype of the byte slice.

type Opt

type Opt func(*opts) error

Option which can affect the behaviour of ffmpeg

func OptContext

func OptContext(context *Context) Opt

New streams with parameters from the context

func OptForce

func OptForce() Opt

Force resampling and resizing on decode, even if the input and output parameters are the same

func OptInputFormat

func OptInputFormat(name string) Opt

Input format from name or url

func OptInputOpt

func OptInputOpt(opt ...string) Opt

Input format options

func OptLog

func OptLog(verbose bool, fn LogFunc) Opt

Set a logging function

func OptMetadata

func OptMetadata(entry ...*Metadata) Opt

Append metadata to the output file, including artwork

func OptOutputFormat

func OptOutputFormat(name string) Opt

Output format from name or url

func OptStream

func OptStream(stream int, par *Par) Opt

New stream with parameters

type Packet

type Packet ff.AVPacket

func (*Packet) String

func (packet *Packet) String() string

func (*Packet) Ts

func (packet *Packet) Ts() float64

Return the timestamp in seconds, or TS_UNDEFINED if the timestamp is undefined or timebase is not set

type Par

type Par struct {
	ff.AVCodecParameters
	// contains filtered or unexported fields
}

func AudioPar

func AudioPar(samplefmt string, channellayout string, samplerate int, opts ...media.Metadata) *Par

Create audio parameters. If there is an error, then this function will panic

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 VideoPar

func VideoPar(pixfmt string, size string, framerate float64, opts ...media.Metadata) *Par

Create video parameters. If there is an error, then this function will panic

func (*Par) CopyToCodecContext

func (ctx *Par) CopyToCodecContext(codec *ff.AVCodecContext) error

func (*Par) FrameRate

func (ctx *Par) FrameRate() float64

func (*Par) MarshalJSON

func (ctx *Par) MarshalJSON() ([]byte, error)

func (*Par) String

func (ctx *Par) String() string

func (*Par) Type

func (ctx *Par) Type() media.Type

func (*Par) ValidateFromCodec

func (ctx *Par) ValidateFromCodec(codec *ff.AVCodec) error

func (*Par) WidthHeight

func (ctx *Par) WidthHeight() string

type PixelFormat

type PixelFormat ff.AVPixelFormat

func (*PixelFormat) IsPlanar

func (pixfmt *PixelFormat) IsPlanar() bool

func (*PixelFormat) MarshalJSON

func (pixfmt *PixelFormat) MarshalJSON() ([]byte, error)

func (*PixelFormat) Name

func (pixfmt *PixelFormat) Name() string

func (*PixelFormat) NumPlanes

func (pixfmt *PixelFormat) NumPlanes() int

func (*PixelFormat) String

func (pixfmt *PixelFormat) String() string

type Re

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

Re implements a resampler and rescaler for audio and video frames. May need to extend it for subtitles later on

func NewRe

func NewRe(par *Par, force bool) (*Re, error)

Return a new resampler or rescaler, with the destination parameters. If force is true, then the resampler will always resample, even if the destination parameters are the same as the source parameters.

func (*Re) Close

func (re *Re) Close() error

Release resources

func (*Re) Frame

func (re *Re) Frame(src *Frame) (*Frame, error)

Resample or rescale the source frame and return the destination frame

type Reader

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

Media reader which reads from a URL, file path or device

func NewReader

func NewReader(r io.Reader, opt ...Opt) (*Reader, error)

Create a new reader from an io.Reader

func Open

func Open(url string, opt ...Opt) (*Reader, error)

Open media from a url, file path or device

func (*Reader) BestStream

func (r *Reader) BestStream(t media.Type) int

Return the "best stream" for a specific media type, or -1 if there is no "best stream" for that type.

func (*Reader) Close

func (r *Reader) Close() error

Close the reader

func (*Reader) Decode

func (r *Reader) Decode(ctx context.Context, mapfn DecoderMapFunc, decodefn DecoderFrameFn) error

func (*Reader) DecodeWithContext

func (r *Reader) DecodeWithContext(ctx context.Context, decoders *Context, decodefn DecoderFrameFn) error

Decode the media stream into frames. The decodefn is called for each frame decoded from the stream.

The decoding can be interrupted by cancelling the context, or by the decodefn returning an error or io.EOF. The latter will end the decoding process early but will not return an error.

func (*Reader) Duration

func (r *Reader) Duration() time.Duration

Return the duration of the media stream, returns zero if unknown

func (*Reader) Map

func (r *Reader) Map(fn DecoderMapFunc) (*Context, error)

Map streams to decoders, and return the decoding context The map function is called for each stream and should return the parameters for the destination frame. If any parameters are returned as null, then that stream is ignored

func (*Reader) MarshalJSON

func (r *Reader) MarshalJSON() ([]byte, error)

Display the reader as a string

func (*Reader) Metadata

func (r *Reader) Metadata(keys ...string) []*Metadata

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) Seek

func (r *Reader) Seek(stream int, secs float64) error

Seek to a specific time in the media stream, in seconds

func (*Reader) Streams

func (r *Reader) Streams(t media.Type) []*Stream

Return all streams of a specific type (video, audio, subtitle, data)

func (*Reader) String

func (r *Reader) String() string

Display the reader as a string

func (*Reader) Type

func (r *Reader) Type() media.Type

Return the media type

type SampleFormat

type SampleFormat ff.AVSampleFormat

func (*SampleFormat) BytesPerSample

func (samplefmt *SampleFormat) BytesPerSample() int

func (*SampleFormat) IsPlanar

func (samplefmt *SampleFormat) IsPlanar() bool

func (*SampleFormat) MarshalJSON

func (samplefmt *SampleFormat) MarshalJSON() ([]byte, error)

func (*SampleFormat) Name

func (samplefmt *SampleFormat) Name() string

func (*SampleFormat) String

func (samplefmt *SampleFormat) String() string

type Stream

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

func (*Stream) Index

func (s *Stream) Index() int

Return the stream index

func (*Stream) MarshalJSON

func (s *Stream) MarshalJSON() ([]byte, error)

func (*Stream) Type

func (s *Stream) Type() media.Type

Return the stream type

type Writer

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

Create media from io.Writer

func Create

func Create(url string, opt ...Opt) (*Writer, error)

Create a new writer with a URL and options

func NewWriter

func NewWriter(w io.Writer, opt ...Opt) (*Writer, error)

Create a new writer with an io.Writer and options

func (*Writer) Close

func (w *Writer) Close() error

Close a writer and release resources

func (*Writer) Encode

func (w *Writer) Encode(ctx context.Context, in EncoderFrameFn, out EncoderPacketFn) error

Encode frames from all encoders, calling the callback function to encode the frame. If the callback function returns io.EOF then the encoding for that encoder is stopped after flushing. If the second callback is nil, then packets are written to the output.

func (*Writer) MarshalJSON

func (w *Writer) MarshalJSON() ([]byte, error)

Display the writer as a string

func (*Writer) Stream

func (w *Writer) Stream(stream int) *Encoder

Return a "stream" for encoding

func (*Writer) String

func (w *Writer) String() string

Display the writer as a string

func (*Writer) Type

func (w *Writer) Type() media.Type

Return a "stream" for encoding

func (*Writer) Write

func (w *Writer) Write(packet *Packet) error

Write a packet to the output. If you intercept the packets in the Encode method, then you can use this method to write packets to the output.

Jump to

Keyboard shortcuts

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