Class MediaFoundationEncoder
Media Foundation Encoder class allows you to use Media Foundation to encode an IWaveProvider to any supported encoding format
public class MediaFoundationEncoder : IDisposable
- Inheritance
-
MediaFoundationEncoder
- Implements
- Inherited Members
Constructors
MediaFoundationEncoder(MediaType)
Creates a new encoder that encodes to the specified output media type
public MediaFoundationEncoder(MediaType outputMediaType)
Parameters
outputMediaTypeMediaTypeDesired output media type
Properties
DefaultReadBufferSize
Default read buffer size
public int DefaultReadBufferSize { get; set; }
Property Value
Methods
Dispose()
Disposes this instance
public void Dispose()
Encode(Stream, IWaveProvider, Guid)
Encodes to a stream. Reads directly into the MF buffer via span for zero-copy encoding. Accepts any IWaveProvider (including WaveStream subclasses).
public void Encode(Stream outputStream, IWaveProvider inputSource, Guid transcodeContainerType)
Parameters
outputStreamStreamOutput stream
inputSourceIWaveProviderInput audio source (should be PCM, some encoders will also allow IEEE float)
transcodeContainerTypeGuidOne of TranscodeContainerTypes
Encode(string, IWaveProvider)
Encodes to a file. Reads directly into the MF buffer via span for zero-copy encoding. Accepts any IWaveProvider (including WaveStream subclasses).
public void Encode(string outputFile, IWaveProvider inputSource)
Parameters
outputFilestringOutput filename (container type is deduced from the filename)
inputSourceIWaveProviderInput audio source (should be PCM, some encoders will also allow IEEE float)
EncodeToAac(IWaveProvider, Stream, int)
Helper function to simplify encoding to AAC By default, will only be available on Windows 7 and above
public static void EncodeToAac(IWaveProvider inputSource, Stream outputStream, int desiredBitRate = 192000)
Parameters
inputSourceIWaveProviderInput audio source, must be PCM
outputStreamStreamOutput stream
desiredBitRateintDesired bitrate. Use GetEncodeBitrates to find the possibilities for your input type
EncodeToAac(IWaveProvider, string, int)
Helper function to simplify encoding to AAC By default, will only be available on Windows 7 and above
public static void EncodeToAac(IWaveProvider inputSource, string outputFile, int desiredBitRate = 192000)
Parameters
inputSourceIWaveProviderInput audio source, must be PCM
outputFilestringOutput file path, should end with .mp4 (or .aac on Windows 8)
desiredBitRateintDesired bitrate. Use GetEncodeBitrates to find the possibilities for your input type
EncodeToFlac(IWaveProvider, string)
Helper function to simplify encoding to FLAC (Free Lossless Audio Codec). Available on Windows 10 and above. FLAC is lossless, so there is no bitrate to choose - the encoder media type is selected to match the input's sample rate, channels, and (where possible) bit depth. If the encoder only offers a different bit depth for the input rate/channels, the closest available is used; supply PCM input at that depth for best results.
public static void EncodeToFlac(IWaveProvider inputSource, string outputFile)
Parameters
inputSourceIWaveProviderInput audio source, must be PCM
outputFilestringOutput file path, should end with .flac
EncodeToMp3(IWaveProvider, Stream, int)
Helper function to simplify encoding to MP3 By default, will only be available on Windows 8 and above
public static void EncodeToMp3(IWaveProvider inputSource, Stream outputStream, int desiredBitRate = 192000)
Parameters
inputSourceIWaveProviderInput audio source, must be PCM
outputStreamStreamOutput stream
desiredBitRateintDesired bitrate. Use GetEncodeBitrates to find the possibilities for your input type
EncodeToMp3(IWaveProvider, string, int)
Helper function to simplify encoding to MP3 By default, will only be available on Windows 8 and above
public static void EncodeToMp3(IWaveProvider inputSource, string outputFile, int desiredBitRate = 192000)
Parameters
inputSourceIWaveProviderInput audio source, must be PCM
outputFilestringOutput file path, should end with .mp3
desiredBitRateintDesired bitrate. Use GetEncodeBitrates to find the possibilities for your input type
EncodeToWma(IWaveProvider, Stream, int)
Helper function to simplify encoding Window Media Audio
public static void EncodeToWma(IWaveProvider inputSource, Stream outputStream, int desiredBitRate = 192000)
Parameters
inputSourceIWaveProviderInput audio source, must be PCM
outputStreamStreamOutput stream
desiredBitRateintDesired bitrate. Use GetEncodeBitrates to find the possibilities for your input type
EncodeToWma(IWaveProvider, string, int)
Helper function to simplify encoding Window Media Audio
public static void EncodeToWma(IWaveProvider inputSource, string outputFile, int desiredBitRate = 192000)
Parameters
inputSourceIWaveProviderInput audio source, must be PCM
outputFilestringOutput file path, should end with .wma
desiredBitRateintDesired bitrate. Use GetEncodeBitrates to find the possibilities for your input type
GetEncodeBitrates(Guid, int, int)
Queries the available bitrates for a given encoding output type, sample rate and number of channels
public static int[] GetEncodeBitrates(Guid audioSubtype, int sampleRate, int channels)
Parameters
audioSubtypeGuidAudio subtype - a value from the AudioSubtypes class
sampleRateintThe sample rate of the PCM to encode
channelsintThe number of channels of the PCM to encode
Returns
- int[]
An array of available bitrates in average bits per second
GetOutputMediaTypes(Guid)
Gets all the available media types for a particular
public static MediaType[] GetOutputMediaTypes(Guid audioSubtype)
Parameters
audioSubtypeGuidAudio subtype - a value from the AudioSubtypes class
Returns
- MediaType[]
An array of available media types that can be encoded with this subtype
SelectMediaType(Guid, WaveFormat, int)
Tries to find the encoding media type with the closest bitrate to that specified
public static MediaType SelectMediaType(Guid audioSubtype, WaveFormat inputFormat, int desiredBitRate)
Parameters
audioSubtypeGuidAudio subtype, a value from AudioSubtypes
inputFormatWaveFormatYour encoder input format (used to check sample rate and channel count)
desiredBitRateintYour desired bitrate
Returns
- MediaType
The closest media type, or null if none available