Class MuxerContext
Represents an output media container used for muxing encoded packets into a media file or stream.
Inherited Members
Namespace: FFmpeg.Formats
Assembly: FFmpeg.dll
Syntax
public class MuxerContext : FormatContext, IOptionQueryable, ILoggingContext, IDisposable, IAVPointer<_AVFormatContext>
Remarks
MuxerContext is the output counterpart to DemuxerContext. It provides functionality for creating media containers, adding streams, writing container headers, writing encoded packets, and finalizing the output.
A muxer can write to a file, a Stream, or a custom IOContext. After opening a muxer, one or more streams should be added and configured before calling WriteHeader(). Encoded packets can then be written using WritePacket(IPacket?) or WritePacketInterleaved(IPacket?). Once all packets have been written, WriteTrailer() should be called to finalize the container.
This class wraps FFmpeg's AVFormatContext for output operations and
corresponds to the FFmpeg muxing API.
Constructors
View SourceMuxerContext(_AVFormatContext*)
Creates a muxer context that wraps around the provided ffmpeg ibject
Declaration
protected MuxerContext(_AVFormatContext* context)
Parameters
| Type | Name | Description |
|---|---|---|
| _AVFormatContext* | context | The pointer to the _AVFormatContext |
Properties
View SourceOutputFormat
Gets the output format associated with this context, if available.
Declaration
public OutputFormat? OutputFormat { get; }
Property Value
| Type | Description |
|---|---|
| OutputFormat? |
Methods
View SourceAddStream(Codec)
Adds a new stream to the output container.
Declaration
public AVStream AddStream(Codec codec)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec | codec | The codec associated with the new stream. |
Returns
| Type | Description |
|---|---|
| AVStream | An AVStream representing the newly created stream. |
Remarks
This method creates a new AVStream using FFmpeg.AutoGen.ffmpeg.avformat_new_stream(FFmpeg.AutoGen._AVFormatContext*, FFmpeg.AutoGen._AVCodec*) and initializes its codec identifier and media type.
The returned stream should be configured before calling WriteHeader().
Exceptions
| Type | Condition |
|---|---|
| OutOfMemoryException | Thrown if FFmpeg fails to allocate the new stream. |
AddStream(Codec, ICodecParameters)
Adds a new stream to the output container and copies codec parameters from an existing source.
Declaration
public AVStream AddStream(Codec codec, ICodecParameters codecParameters)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec | codec | The codec associated with the new stream. |
| ICodecParameters | codecParameters | The codec parameters to copy into the stream. |
Returns
| Type | Description |
|---|---|
| AVStream | An AVStream representing the newly created stream. |
Remarks
The codec parameters are copied using FFmpeg.AutoGen.ffmpeg.avcodec_parameters_copy(FFmpeg.AutoGen._AVCodecParameters*, FFmpeg.AutoGen._AVCodecParameters*). This overload is useful when remuxing existing streams or when codec parameters are already available.
Exceptions
| Type | Condition |
|---|---|
| OutOfMemoryException | Thrown if FFmpeg fails to allocate the new stream. |
AddStream(CodecContext)
Adds a new stream to the output container using an existing codec context.
Declaration
public AVStream AddStream(CodecContext codec)
Parameters
| Type | Name | Description |
|---|---|---|
| CodecContext | codec | The codec context whose parameters are copied to the new stream. |
Returns
| Type | Description |
|---|---|
| AVStream | An AVStream representing the newly created stream. |
Remarks
The codec parameters and time base are copied from the supplied CodecContext.
If the selected output format requires global headers, the GlobalHeader flag is automatically enabled on the codec context before encoding begins.
Exceptions
| Type | Condition |
|---|---|
| OutOfMemoryException | Thrown if FFmpeg fails to allocate the new stream. |
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. |
Dispose(bool)
Releases the resources used by the current format context.
Declaration
protected override void Dispose(bool disposing)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | disposing | Indicates whether the method is being called from the Dispose() method or a finalizer. |
Overrides
View SourceFlush()
Flushes the underlying output I/O context, if one exists.
Declaration
public void Flush()
Remarks
This forces any buffered data to be written to the underlying stream or file. It does not finalize the media container; call WriteTrailer() to properly finish the output.
Open(IOContext, OutputFormat)
Opens an output media file using an I/O context for writing.
Declaration
public static MuxerContext? Open(IOContext context, OutputFormat format)
Parameters
| Type | Name | Description |
|---|---|---|
| IOContext | context | The I/O context for output media. |
| OutputFormat | format | The output format. |
Returns
| Type | Description |
|---|---|
| MuxerContext | An instance of MuxerContext or null if the operation fails. |
Open(Stream, OutputFormat, bool)
Opens an output stream for writing.
Declaration
public static MuxerContext? Open(Stream stream, OutputFormat format, bool closeOnDispose = true)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream for output media. |
| OutputFormat | format | The output format. |
| bool | closeOnDispose | If true, when the MuxerContext is closed the stream will be too. |
Returns
| Type | Description |
|---|---|
| MuxerContext | An instance of MuxerContext or null if the operation fails. |
Open(string)
Opens an output media file for writing.
Declaration
public static MuxerContext? Open(string filename)
Parameters
| Type | Name | Description |
|---|---|---|
| string | filename | The filename for the output media. |
Returns
| Type | Description |
|---|---|
| MuxerContext | An instance of MuxerContext or null if the operation fails. |
Open(string?, OutputFormat?)
Opens an output media file for writing. For Output fomats of the no file type, filename may be null.
Declaration
public static MuxerContext? Open(string? filename, OutputFormat? format)
Parameters
| Type | Name | Description |
|---|---|---|
| string | filename | The filename for the output media. |
| OutputFormat? | format | The output format. |
Returns
| Type | Description |
|---|---|
| MuxerContext | An instance of MuxerContext or null if the operation fails. |
ToString()
Returns the long name of the output format.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | The long name of the output format, or |
Overrides
View SourceWriteFrame(IPacket?)
Writes a frame to the output media file.
Declaration
[Obsolete("Renamed to WritePacket")]
public AVResult32 WriteFrame(IPacket? packet)
Parameters
| Type | Name | Description |
|---|---|---|
| IPacket | packet | The packet to write. If null, writes a null packet. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. |
WriteFrameInterleaved(IPacket?)
Writes an interleaved frame to the output media file.
Declaration
[Obsolete("Renamed to WritePacketInterleaved")]
public AVResult32 WriteFrameInterleaved(IPacket? packet)
Parameters
| Type | Name | Description |
|---|---|---|
| IPacket | packet | The packet to write. If null, writes a null packet. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. |
WriteHeader()
Writes the header of the output media file.
Declaration
public AVResult32 WriteHeader()
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. |
WriteHeader(AVDictionary)
Writes the header of the output media file using a specified dictionary.
Declaration
public AVResult32 WriteHeader(AVDictionary dictionary)
Parameters
| Type | Name | Description |
|---|---|---|
| AVDictionary | dictionary | A dictionary of options. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. |
WriteHeader(AVMultiDictionary)
Writes the header of the output media file using a multi-dictionary.
Declaration
public AVResult32 WriteHeader(AVMultiDictionary dictionary)
Parameters
| Type | Name | Description |
|---|---|---|
| AVMultiDictionary | dictionary | A multi-dictionary of options. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. |
WriteHeader(IDictionary<string, string>)
Writes the header of the output media file using a dictionary represented as IDictionary<TKey, TValue>.
Declaration
public AVResult32 WriteHeader(IDictionary<string, string> dictionary)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<string, string> | dictionary | A dictionary of options. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. |
WritePacket(IPacket?)
Writes a frame to the output media file.
Declaration
public AVResult32 WritePacket(IPacket? packet)
Parameters
| Type | Name | Description |
|---|---|---|
| IPacket | packet | The packet to write. If null, writes a null packet. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. |
WritePacketInterleaved(IPacket?)
Writes an interleaved frame to the output media file.
Declaration
public AVResult32 WritePacketInterleaved(IPacket? packet)
Parameters
| Type | Name | Description |
|---|---|---|
| IPacket | packet | The packet to write. If null, writes a null packet. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. |
WriteTrailer()
Writes the trailer of the output media file and finalizes the container.
Declaration
public AVResult32 WriteTrailer()
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. |
Remarks
This method must be called after all packets have been written. It writes any remaining buffered data, updates container metadata such as indexes, and releases internal muxing state maintained by FFmpeg.