Table of Contents

Class MediaFoundationEncoder

Namespace
NAudio.Wave
Assembly
NAudio.Wasapi.dll

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

outputMediaType MediaType

Desired output media type

Properties

DefaultReadBufferSize

Default read buffer size

public int DefaultReadBufferSize { get; set; }

Property Value

int

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

outputStream Stream

Output stream

inputSource IWaveProvider

Input audio source (should be PCM, some encoders will also allow IEEE float)

transcodeContainerType Guid

One 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

outputFile string

Output filename (container type is deduced from the filename)

inputSource IWaveProvider

Input 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

inputSource IWaveProvider

Input audio source, must be PCM

outputStream Stream

Output stream

desiredBitRate int

Desired 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

inputSource IWaveProvider

Input audio source, must be PCM

outputFile string

Output file path, should end with .mp4 (or .aac on Windows 8)

desiredBitRate int

Desired 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

inputSource IWaveProvider

Input audio source, must be PCM

outputFile string

Output 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

inputSource IWaveProvider

Input audio source, must be PCM

outputStream Stream

Output stream

desiredBitRate int

Desired 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

inputSource IWaveProvider

Input audio source, must be PCM

outputFile string

Output file path, should end with .mp3

desiredBitRate int

Desired 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

inputSource IWaveProvider

Input audio source, must be PCM

outputStream Stream

Output stream

desiredBitRate int

Desired 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

inputSource IWaveProvider

Input audio source, must be PCM

outputFile string

Output file path, should end with .wma

desiredBitRate int

Desired 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

audioSubtype Guid

Audio subtype - a value from the AudioSubtypes class

sampleRate int

The sample rate of the PCM to encode

channels int

The 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

audioSubtype Guid

Audio 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

audioSubtype Guid

Audio subtype, a value from AudioSubtypes

inputFormat WaveFormat

Your encoder input format (used to check sample rate and channel count)

desiredBitRate int

Your desired bitrate

Returns

MediaType

The closest media type, or null if none available