This article explains how to configure encoding by selecting a container format, video/audio codecs, and codec/format attributes.
Encoding Workflow
Select a format (container).
Select video and audio codecs.
Configure codec/format attributes (for example, bitrate).
Generate an encoding configuration string.
Start encoding.
⚠️ Codecs can only be enumerated after a format is selected, because available codecs depend on the container.
Encoding formats
A format defines the target container (for example, MP4, MKV, MXF) and limits the set of available audio/video codecs to those supported by the selected container.
List available formats
MFormats SDK (MFWriter)
Use WriterOptionGetCount and WriterOptionGetByIndex to enumerate formats.
int count;
m_objWriter.WriterOptionGetCount(eMFWriterOption.eMFWO_Format, out count);
for (int i = 0; i < count; i++)
{
string shortName, longName;
m_objWriter.WriterOptionGetByIndex(eMFWriterOption.eMFWO_Format, i, out shortName, out longName);
// shortName: "mp4"
// longName: "MP4 (MPEG-4 Part 14)"
}WriterOptionGetByIndex returns a short format name (for example, mp4) and a full display name (for example, MP4 (MPEG-4 Part 14)).
MPlatform SDK (MWriter)
Use ConfigGetCount and ConfigGetByIndex to enumerate formats.
int count;
m_objWriter.ConfigGetCount("format", out count);
for (int i = 0; i < count; i++)
{
string displayName;
m_objWriter.ConfigGetByIndex("format", i, out displayName);
// displayName: "MP4 (MPEG-4 Part 14)"
}
ConfigGetByIndex("format", ...) returns the format display name (for example, MP4 (MPEG-4 Part 14)).
Display name vs id (MPlatform)
In MPlatform, a format has:
display name — used when listing formats (returned by
ConfigGetByIndex)id — a short internal name (for example,
mp4), available via format attributes after callingConfigSet
Tip: Attribute order is not guaranteed. When you need a specific attribute (like
id), query it by name (see examples below).
Set the format
MFormats SDK (MFWriter)
Use WriterOptionSetByIndex to select a format by index. The returned props contains available parameters for that format.
IMFProps props;
m_objWriter.WriterOptionSetByIndex(eMFWriterOption.eMFWO_Format, targetIndex, out props);MPlatform SDK (MWriter)
Use ConfigSet to select a format. ConfigSet reliably accepts the value returned by ConfigGetByIndex (the display name).
IMAttributes myAttributes;
m_objWriter.ConfigSet("format", "mp4", out myAttributes);If you need the short internal id (for example, mp4), read it from the returned attributes:
int have;
string idValue;
formatAttrs.AttributesHave("id", out have, out idValue);
if (have != 0)
{
// idValue == "mp4"
}Available Video and Audio Codecs
After a format is selected, you can enumerate and set video/audio codecs supported by that format.
List available codecs
MFormats SDK (MFWriter)
// Video codecs
int vCount;
m_objWriter.WriterOptionGetCount(eMFWriterOption.eMFWO_VideoCodec, out vCount);
for (int i = 0; i < vCount; i++)
{
string shortName, longName;
m_objWriter.WriterOptionGetByIndex(eMFWriterOption.eMFWO_VideoCodec, i, out shortName, out longName);
}
// Audio codecs
int aCount;
m_objWriter.WriterOptionGetCount(eMFWriterOption.eMFWO_AudioCodec, out aCount);
for (int i = 0; i < aCount; i++)
{
string shortName, longName;
m_objWriter.WriterOptionGetByIndex(eMFWriterOption.eMFWO_AudioCodec, i, out shortName, out longName);
}MPlatform SDK (MWriter)
// Video codecs
int vCount;
m_objWriter.ConfigGetCount("format::video", out vCount);
for (int i = 0; i < vCount; i++)
{
string codecName;
m_objWriter.ConfigGetByIndex("format::video", i, out codecName);
}
// Audio codecs
int aCount;
m_objWriter.ConfigGetCount("format::audio", out aCount);
for (int i = 0; i < aCount; i++)
{
string codecName;
m_objWriter.ConfigGetByIndex("format::audio", i, out codecName);
}Set the codecs
MFormats SDK (MFWriter)
IMFProps videoProps;
m_objWriter.WriterOptionSetByIndex(eMFWriterOption.eMFWO_VideoCodec, videoCodecIndex, out videoProps);
IMFProps audioProps;
m_objWriter.WriterOptionSetByIndex(eMFWriterOption.eMFWO_AudioCodec, audioCodecIndex, out audioProps);
MPlatform SDK (MWriter)
IMAttributes videoAttrs;
m_objWriter.ConfigSet("format::video", "mpeg4", out videoAttrs);
IMAttributes audioAttrs;
m_objWriter.ConfigSet("format::audio", "libmp3lame", out audioAttrs);Setting Attributes
Formats and codecs expose attributes such as bitrate, profiles, GOP settings, or container options (for example, enabling captions).
You can access attributes immediately after selecting a format/codec, or later by requesting the current configuration.
Get current codec attributes
MFormats SDK (MFWriter)
IMFProps videoProps;
string videoName;
m_objWriter.WriterOptionGet(eMFWriterOption.eMFWO_VideoCodec, out videoName, out videoProps);MPlatform SDK (MWriter)
string videoName;
IMAttributes videoProps;
m_pConfigRoot.ConfigGet("format::video", out videoName, out videoProps)Enumerate available attributes
MFormats SDK
int nCount = 0;
videoProps.PropsGetCount("", out nCount);
for (int i = 0; i < nCount; i++)
{
string strName, strValue;
int isNode;
videoProps.PropsGetByIndex("", i, out strName, out strValue, out isNode);
}MPlatform SDK
int nCount = 0;
videoProps.AttributesGetCount(out nCount);
for (int i = 0; i < nCount; i++)
{
string strName, strValue;
videoProps.AttributesGetByIndex("", i, out strName, out strValue);
}Set an attribute
MFormats SDK
videoProps.PropsSet("b", "10M"); // to specify a bitrateMPlatform SDK
videoProps.AttributesStringSet("b", "10M");A full list of supported parameters is available in Formats and Codecs.
Using a Ready-to-Use String
Once format, codecs, and attributes are configured, you can obtain a single encoding configuration string and reuse it later.
Get the full configuration string
MFormats SDK
string targetPath, encodingConfig;
m_objWriter.WriterGet(out targetPath, out encodingConfig);MPlatform SDK
string encodingConfig;
m_objWriter.ConfigGetAll(1, out encodingConfig);Example result:
format='mp4' video::codec='n264' video::b='10M' audio::codec='aac'
You can save the string to use it as a template later.
Use the configuration string for encoding
MFormats SDK
m_objWriter.WriterSet(@"c:\testEncoding.mp4", 1, "format='mp4' video::codec='mpeg4' video::b='10M' audio::codec='libmp3lame'");Use 1 in WriterSet to override the previous configuration.
MPlatform SDK
m_objWriter.WriterNameSet(@"c:\testEncoding.mp4", "format='mp4' video::codec='mpeg4' video::b='10M' audio::codec='libmp3lame'");Validating the Configuration
If the config.auto_check property is enabled, the writer reports an error when an invalid configuration is applied (for example, unsupported or misspelled codec names).
m_objWriter.PropsSet("config.auto_check", "true");
m_objWriter.WriterSet(@"c:\testEncoding.mp4", 1, "format='mp4' video::codec='wrongCodecName'
video::b='10M' audio::codec='libmp3lame'");
This is useful for scenarios such as NVIDIA NVENC encoding, where hardware encoder initialization may fail (for example, due to concurrent session limits). With auto-check enabled, you can detect initialization failures early and fall back to another encoding option.
Encoding Multiple Audio Tracks with Different Codecs
When you enable split_channels, the writer creates multiple audio tracks. You can then assign a different codec to each track.
For example, if the source has 4 channels and you want 2 audio tracks:
track 0: AAC
track 1: AC-3
Use:
"format='mp4' split_channels='2' video::codec='mpeg2video' video::b='5M' audio::codec='aac' audio.1::codec='ac3'"Note: In MFormats SDK, set the
_bResetOptionparameter ofWriterSettotruefor this feature to work correctly.
Example (MPlatform): Show current config after user selections
This pattern is useful for UI scenarios: each time the user changes format or codecs, refresh the full config string.
// user selects format
IMAttributes formatAttrs;
writer.ConfigSet("format", selectedFormatDisplayName, out formatAttrs);
// user selects codecs
IMAttributes videoAttrs;
writer.ConfigSet("format::video", selectedVideoCodec, out videoAttrs);
IMAttributes audioAttrs;
writer.ConfigSet("format::audio", selectedAudioCodec, out audioAttrs);
// show resulting config string
string cfg;
writer.ConfigGetAll(1, out cfg);
Console.WriteLine(cfg);