reader

package
v1.9.2 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Protocols

func Protocols() []string

Protocols returns the name of every I/O protocol FFmpeg has registered for reading (e.g. "file", "http", "https", "rtmp", "udp", "tcp", ...) - the URL scheme a Reader's url actually travels over, which is a different axis from a container format: e.g. "rtsp://host/stream" is demuxed by the "rtsp" input format, but reaches the network via the "tcp"/"udp" protocol - "rtsp" itself is typically not in this list. Useful for validating a URL's scheme before passing it to Open/NewReader.

Types

type Decoder

type Decoder struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Decoder decodes packets from a set of registered streams into frames, passing each frame to fn. Pass a Decoder to Reader.Decode alongside a PacketFn to decode selected streams while still seeing every packet.

Streams are opted in with Add. A stream that isn't registered is discarded from the decoder's point of view: Reader.Decode still passes its packets to the PacketFn (if any), but they are never sent to a codec.

func NewDecoder

func NewDecoder(fn FrameFn) (*Decoder, error)

NewDecoder creates an empty Decoder that passes every frame it decodes to fn. Use Add to register which streams should be decoded before passing the Decoder to Reader.Decode.

func (*Decoder) Add

func (d *Decoder) Add(stream int) error

Add registers stream for decoding. Streams which are never registered are left alone by Reader.Decode - this is how you discard a stream from frame decoding without affecting the PacketFn.

type FrameFn

type FrameFn func(frame.Frame) error

FrameFn is called for each frame a Decoder produces - an *frame.AudioFrame or *frame.VideoFrame for audio/video streams, or a *frame.SubtitleFrame for subtitle streams (which FFmpeg decodes via a completely separate, legacy API). Returning io.EOF stops decoding early without being treated as an error.

type Opt

type Opt func(o *opts) error

func WithInput

func WithInput(format string, options ...string) Opt

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

type PacketFn

type PacketFn func(*ff.AVPacket) error

PacketFn is called for each packet read by Reader.Decode. Returning io.EOF stops decoding early without being treated as an error.

type Reader

type Reader struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Reader is a wrapper around an AVFormatContext that provides a higher-level interface for reading media files.

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

func (r *Reader) Close() error

Close the reader

func (*Reader) Decode

func (r *Reader) Decode(ctx context.Context, packetfn PacketFn, decoder *Decoder) error

Decode packets from the media stream, calling packetfn for every packet read from any stream - use this for stream copying or remuxing without transcoding. If decoder is non-nil, packets from streams registered with decoder.Add are also decoded into frames and passed to the Decoder's FrameFn; streams not registered with the Decoder are left as packets only. packetfn and decoder may not both be nil.

The reading can be interrupted by cancelling the context, or by either callback returning an error or io.EOF. The latter will end the reading 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) Format

func (r *Reader) Format() *profile.Format

Return the detected input format for the media stream, or nil if unknown

func (*Reader) Metadata

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

Return the metadata for the media stream, filtering by the specified keys if there are any. Artwork and chapters are excluded even when keys is empty ("no filter") - artwork because it's binary content rather than a descriptive tag, and chapters because each is a structured start/end/tags value rather than a flat string one, so a caller has to ask for the "artwork"/"chapter" key by name to get either back.

func (*Reader) Seek

func (r *Reader) Seek(stream int, d time.Duration) error

Seek to a specific time in the media stream

func (*Reader) Streams

func (r *Reader) Streams() map[int]profile.Profile

Return every stream in the media file as profiles, keyed by their real stream index — mirroring the shape writer.WithProfile expects, so a caller can remux the whole container directly without dropping any stream:

for i, p := range r.Streams() {
    opts = append(opts, writer.WithProfile(i, p))
}

Attached-pic (cover art) streams are omitted; artwork is available via Metadata's "artwork" key instead.

Jump to

Keyboard shortcuts

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