Show / Hide Table of Contents

Class MediaSink

Provides a high-level interface for encoding and writing media to an output container.

Inheritance
object
MediaSink
Implements
IDisposable
Inherited Members
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: FFmpeg
Assembly: FFmpeg.dll
Syntax
public class MediaSink : IDisposable
Remarks

MediaSink combines one or more CodecContext instances with a MuxerContext to simplify media encoding and muxing.

Streams are added using AddStream(CodecContext?) or AddStream(Codec). Frames can then be encoded and written using WriteFrame(AVFrame, int), subtitles using WriteSubtitle(Subtitle, int), or pre-encoded packets using WritePacket(IPacket).

The container header is written automatically before the first packet is written. Close() or Dispose() drains any remaining encoded packets, writes the trailer, and releases all associated resources.

Constructors

View Source

MediaSink(MuxerContext)

Initializes a new MediaSink using the specified muxer context.

Declaration
public MediaSink(MuxerContext context)
Parameters
Type Name Description
MuxerContext context

The muxer context used to write the output container.

Properties

View Source

CodecContexts

Gets the encoder contexts associated with the output streams.

Declaration
public IReadOnlyList<CodecContext?> CodecContexts { get; }
Property Value
Type Description
IReadOnlyList<CodecContext>
Remarks

The list contains one entry for each stream in Streams. Entries may be null for streams that are written using pre-encoded packets instead of an encoder.

View Source

FormatContext

Gets the underlying muxer context.

Declaration
public MuxerContext FormatContext { get; }
Property Value
Type Description
MuxerContext
View Source

Metadata

Gets the metadata associated with the output container.

Declaration
public AVDictionary_ref Metadata { get; }
Property Value
Type Description
AVDictionary_ref
View Source

Streams

Gets the streams contained in the output media.

Declaration
public IReadOnlyList<AVStream> Streams { get; }
Property Value
Type Description
IReadOnlyList<AVStream>

Methods

View Source

AddStream(Codec)

Adds a new stream for the specified codec.

Declaration
public AVStream AddStream(Codec encoder)
Parameters
Type Name Description
Codec encoder

The codec associated with the stream.

Returns
Type Description
AVStream

The newly created output stream.

Remarks

No encoder context is created automatically. Call SetCodecContext(CodecContext, int) before encoding frames, or write encoded packets directly using WritePacket(IPacket).

View Source

AddStream(CodecContext?)

Adds a new stream using an existing encoder context.

Declaration
public AVStream AddStream(CodecContext? encoderContext)
Parameters
Type Name Description
CodecContext encoderContext

The encoder context used to initialize the stream, or null to create a stream without an associated encoder.

Returns
Type Description
AVStream

The newly created output stream.

Remarks

The codec parameters and time base are copied from the encoder context when one is supplied.

View Source

AddStream(AVStream)

Adds a stream based on the codec parameters of the

Declaration
public AVStream AddStream(AVStream copyStream)
Parameters
Type Name Description
AVStream copyStream

The stream that contains the codec parameters we want to copy

Returns
Type Description
AVStream

The added stream.

View Source

Close()

Finalizes the output container and releases all associated resources.

Declaration
public void Close()
Remarks

This method writes the trailer if it has not already been written and then disposes the MediaSink.

View Source

Create(IOContext, OutputFormat)

Creates a new MediaSink using a custom I/O context.

Declaration
public static MediaSink? Create(IOContext ioContext, OutputFormat outputFormat)
Parameters
Type Name Description
IOContext ioContext

The custom I/O context.

OutputFormat outputFormat

The output format.

Returns
Type Description
MediaSink

A new MediaSink, or null if the output could not be created.

View Source

Create(Stream, OutputFormat, bool)

Creates a new MediaSink for writing media to a managed stream.

Declaration
public static MediaSink? Create(Stream stream, OutputFormat outputFormat, bool leaveOpen = false)
Parameters
Type Name Description
Stream stream

The destination stream.

OutputFormat outputFormat

The output format.

bool leaveOpen

False if the stream should be closed when the MediaSink gets disposed.

Returns
Type Description
MediaSink

A new MediaSink, or null if the output could not be created.

View Source

Create(string)

Creates a new MediaSink for writing media to a file.

Declaration
public static MediaSink? Create(string url)
Parameters
Type Name Description
string url

The output filename or URL.

Returns
Type Description
MediaSink

A new MediaSink, or null if the output could not be created.

View Source

Create(string?, OutputFormat?)

Creates a new MediaSink for writing media to a file.

Declaration
public static MediaSink? Create(string? url, OutputFormat? outputFormat)
Parameters
Type Name Description
string url

The output filename or URL.

OutputFormat? outputFormat

The output format, or null to determine the format from the filename.

Returns
Type Description
MediaSink

A new MediaSink, or null if the output could not be created.

Exceptions
Type Condition
ArgumentNullException

Thrown when both url and outputFormat are null.

View Source

Dispose()

Releases all resources used by the current instance.

Declaration
public void Dispose()
Remarks

If the trailer has not yet been written, this method attempts to write it before releasing the underlying resources.

View Source

Dispose(bool)

Releases the resources used by the current instance.

Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type Name Description
bool disposing

true to release managed resources; otherwise, false.

View Source

OpenCodecs()

Opens all encoder contexts that have not already been opened.

Declaration
public void OpenCodecs()
Remarks

Calling this method is optional. If an encoder has not been opened manually, it will be opened automatically when the first frame is encoded.

View Source

SetCodecContext(CodecContext, int)

Associates an encoder context with an existing stream.

Declaration
public void SetCodecContext(CodecContext codec, int streamIndex)
Parameters
Type Name Description
CodecContext codec

The encoder context.

int streamIndex

The zero-based index of the stream.

Remarks

The codec parameters are copied to the corresponding stream. If either the stream or codec context does not define a valid time base, it is initialized from the other.

View Source

WriteFrame(AVFrame, int)

Encodes a frame and writes the resulting packets to the output container.

Declaration
public AVResult32 WriteFrame(AVFrame frame, int streamIndex)
Parameters
Type Name Description
AVFrame frame

The frame to encode.

int streamIndex

The index of the destination stream.

Returns
Type Description
AVResult32

An AVResult32 indicating the result of the operation.

Remarks

The frame is sent to the encoder and all immediately available packets are written to the output container.

View Source

WriteHeader()

Writes the container header.

Declaration
public AVResult32 WriteHeader()
Returns
Type Description
AVResult32

An AVResult32 indicating the result of the operation.

Remarks

After the header has been written, the packet time base of each associated encoder is synchronized with the corresponding output stream, since some muxers may adjust stream parameters while writing the header.

View Source

WriteHeader(IDictionary<string, string>)

Writes the container header using the specified muxer options.

Declaration
public AVResult32 WriteHeader(IDictionary<string, string> dic)
Parameters
Type Name Description
IDictionary<string, string> dic

A dictionary containing muxer options.

Returns
Type Description
AVResult32

An AVResult32 indicating the result of the operation.

Remarks

Any options that are not recognized by the muxer remain in the supplied dictionary after the call returns.

View Source

WritePacket(IPacket)

Writes an encoded packet to the output container.

Declaration
public AVResult32 WritePacket(IPacket packet)
Parameters
Type Name Description
IPacket packet

The packet to write.

Returns
Type Description
AVResult32

An AVResult32 indicating the result of the operation.

Remarks

If the container header has not yet been written, it is written automatically before the packet is muxed.

View Source

WriteSubtitle(Subtitle, int)

Encodes and writes a subtitle to the output container.

Declaration
public AVResult32 WriteSubtitle(Subtitle subtitle, int streamIndex)
Parameters
Type Name Description
Subtitle subtitle

The subtitle to encode.

int streamIndex

The destination stream.

Returns
Type Description
AVResult32

An AVResult32 indicating the result of the operation.

View Source

WriteTrailer()

Flushes all encoders and writes the trailer of the output container.

Declaration
public AVResult32 WriteTrailer()
Returns
Type Description
AVResult32

An AVResult32 indicating the result of the operation.

Remarks

All audio and video encoders are drained before the trailer is written. Subsequent calls after a successful write return immediately without writing the trailer again.

Implements

IDisposable
  • View Source
In this article
Back to top Generated by DocFX