Media Foundation Encoder class allows you to use Media Foundation to encode an IWaveProvider to any supported encoding format.
More...
|
| MediaFoundationEncoder (MediaType outputMediaType) |
| Creates a new encoder that encodes to the specified output media type.
|
|
void | Encode (string outputFile, IWaveProvider inputProvider) |
| Encodes the input audio data from inputProvider into the specified transcodeContainerType and writes the result to the outputStream .
|
|
void | Encode (Stream outputStream, IWaveProvider inputProvider, Guid transcodeContainerType) |
| Encodes a file.
|
|
void | Dispose () |
| Disposes this instance.
|
|
|
static int[] | GetEncodeBitrates (Guid audioSubtype, int sampleRate, int channels) |
| Retrieves the array of encoded bitrates for the specified audio subtype, sample rate, and channel count.
|
|
static MediaType[] | GetOutputMediaTypes (Guid audioSubtype) |
| Retrieves the available output media types for the specified audio subtype.
|
|
static void | EncodeToWma (IWaveProvider inputProvider, string outputFile, int desiredBitRate=192000) |
| Encodes the input audio data to WMA format and writes it to the output stream.
|
|
static void | EncodeToWma (IWaveProvider inputProvider, Stream outputStream, int desiredBitRate=192000) |
| Helper function to simplify encoding Window Media Audio Should be supported on Vista and above (not tested)
|
|
static void | EncodeToMp3 (IWaveProvider inputProvider, string outputFile, int desiredBitRate=192000) |
| Encodes the input audio data to MP3 format and writes it to the specified output stream using the specified bit rate.
|
|
static void | EncodeToMp3 (IWaveProvider inputProvider, Stream outputStream, int desiredBitRate=192000) |
| Helper function to simplify encoding to MP3 By default, will only be available on Windows 8 and above.
|
|
static void | EncodeToAac (IWaveProvider inputProvider, string outputFile, int desiredBitRate=192000) |
| Encodes the input audio data to AAC format and writes the result to the specified output stream.
|
|
static void | EncodeToAac (IWaveProvider inputProvider, Stream outputStream, int desiredBitRate=192000) |
| Helper function to simplify encoding to AAC By default, will only be available on Windows 7 and above.
|
|
static MediaType | SelectMediaType (Guid audioSubtype, WaveFormat inputFormat, int desiredBitRate) |
| Selects the media type that best matches the input audio subtype, format, and desired bit rate.
|
|
|
void | Dispose (bool disposing) |
| Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
Media Foundation Encoder class allows you to use Media Foundation to encode an IWaveProvider to any supported encoding format.
◆ MediaFoundationEncoder()
NAudio.Wave.MediaFoundationEncoder.MediaFoundationEncoder |
( |
MediaType | outputMediaType | ) |
|
|
inline |
Creates a new encoder that encodes to the specified output media type.
- Parameters
-
outputMediaType | Desired output media type |
◆ ~MediaFoundationEncoder()
NAudio.Wave.MediaFoundationEncoder.~MediaFoundationEncoder |
( |
| ) |
|
|
inlineprivate |
◆ BytesToNsPosition()
static long NAudio.Wave.MediaFoundationEncoder.BytesToNsPosition |
( |
int | bytes, |
|
|
WaveFormat | waveFormat ) |
|
inlinestaticprivate |
Converts the specified number of bytes to the corresponding position in nanoseconds based on the provided WaveFormat.
- Parameters
-
bytes | The number of bytes to be converted. |
waveFormat | The WaveFormat object containing information about the audio format. |
- Returns
- The position in nanoseconds corresponding to the specified number of bytes based on the provided WaveFormat.
◆ ConvertOneBuffer()
long NAudio.Wave.MediaFoundationEncoder.ConvertOneBuffer |
( |
IMFSinkWriter | writer, |
|
|
int | streamIndex, |
|
|
IWaveProvider | inputProvider, |
|
|
long | position, |
|
|
byte[] | managedBuffer ) |
|
inlineprivate |
Converts data from the inputProvider to a memory buffer and writes it to the sink writer at the specified position.
- Parameters
-
writer | The sink writer to which the data will be written. |
streamIndex | The index of the stream in the sink writer. |
inputProvider | The input wave provider from which data will be read. |
position | The position in nanoseconds at which the data will be written. |
managedBuffer | The managed buffer used for data conversion. |
- Returns
- The duration of the converted data in nanoseconds.
This method converts data from the inputProvider to a memory buffer, sets the sample time and duration, and writes it to the sink writer at the specified position. If the read operation from the inputProvider is successful, the method returns the duration of the converted data in nanoseconds. If the read operation fails, no data is written to the sink writer, and the method returns 0.
◆ CreateSinkWriter() [1/2]
static IMFSinkWriter NAudio.Wave.MediaFoundationEncoder.CreateSinkWriter |
( |
IStream | outputStream, |
|
|
Guid | TranscodeContainerType ) |
|
inlinestaticprivate |
◆ CreateSinkWriter() [2/2]
static IMFSinkWriter NAudio.Wave.MediaFoundationEncoder.CreateSinkWriter |
( |
string | outputFile | ) |
|
|
inlinestaticprivate |
Creates a sink writer for writing media data to the specified output stream with the given transcode container type.
- Parameters
-
outputStream | The output stream to write the media data to. |
TranscodeContainerType | The GUID representing the transcode container type. |
- Returns
- The sink writer for writing media data to the specified output stream.
- Exceptions
-
System.Runtime.InteropServices.COMException | Thrown if there is an error creating the sink writer. |
◆ Dispose() [1/2]
void NAudio.Wave.MediaFoundationEncoder.Dispose |
( |
| ) |
|
|
inline |
◆ Dispose() [2/2]
void NAudio.Wave.MediaFoundationEncoder.Dispose |
( |
bool | disposing | ) |
|
|
inlineprotected |
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
This method disposes of unmanaged resources and suppresses the finalization of the object.
◆ Encode() [1/2]
void NAudio.Wave.MediaFoundationEncoder.Encode |
( |
Stream | outputStream, |
|
|
IWaveProvider | inputProvider, |
|
|
Guid | transcodeContainerType ) |
|
inline |
Encodes a file.
- Parameters
-
outputStream | Output stream |
inputProvider | Input provider (should be PCM, some encoders will also allow IEEE float) |
transcodeContainerType | One of TranscodeContainerTypes |
◆ Encode() [2/2]
void NAudio.Wave.MediaFoundationEncoder.Encode |
( |
string | outputFile, |
|
|
IWaveProvider | inputProvider ) |
|
inline |
Encodes the input audio data from inputProvider into the specified transcodeContainerType and writes the result to the outputStream .
- Parameters
-
outputStream | The stream to which the encoded audio data will be written. |
inputProvider | The input audio data provider. |
transcodeContainerType | The GUID specifying the container type for the transcoded audio data. |
- Exceptions
-
ArgumentException | Thrown when the input format of inputProvider is not PCM or IEEE float. |
This method encodes the input audio data using the specified transcodeContainerType and writes the result to the outputStream . It first checks if the input format is PCM or IEEE float, and then creates a media type based on the input format. It then creates a sink writer and adds a stream with the output media type, sets the input media type, and performs the encoding. Finally, it releases the allocated COM objects.
◆ EncodeToAac() [1/2]
static void NAudio.Wave.MediaFoundationEncoder.EncodeToAac |
( |
IWaveProvider | inputProvider, |
|
|
Stream | outputStream, |
|
|
int | desiredBitRate = 192000 ) |
|
inlinestatic |
Helper function to simplify encoding to AAC By default, will only be available on Windows 7 and above.
- Parameters
-
inputProvider | Input provider, must be PCM |
outputStream | Output stream |
desiredBitRate | Desired bitrate. Use GetEncodeBitrates to find the possibilities for your input type |
◆ EncodeToAac() [2/2]
static void NAudio.Wave.MediaFoundationEncoder.EncodeToAac |
( |
IWaveProvider | inputProvider, |
|
|
string | outputFile, |
|
|
int | desiredBitRate = 192000 ) |
|
inlinestatic |
Encodes the input audio data to AAC format and writes the result to the specified output stream.
- Parameters
-
inputProvider | The input audio data provider. |
outputStream | The stream to which the encoded AAC data will be written. |
desiredBitRate | The desired bit rate for the AAC encoding (default is 192000). |
- Exceptions
-
InvalidOperationException | Thrown when no suitable AAC encoders are available. |
This method selects the appropriate media type for AAC encoding based on the input audio format and desired bit rate. It then uses the selected media type to create a MediaFoundationEncoder, which is used to encode the input audio data to AAC format. The encoded AAC data is written to the specified output stream.
◆ EncodeToMp3() [1/2]
static void NAudio.Wave.MediaFoundationEncoder.EncodeToMp3 |
( |
IWaveProvider | inputProvider, |
|
|
Stream | outputStream, |
|
|
int | desiredBitRate = 192000 ) |
|
inlinestatic |
Helper function to simplify encoding to MP3 By default, will only be available on Windows 8 and above.
- Parameters
-
inputProvider | Input provider, must be PCM |
outputStream | Output stream |
desiredBitRate | Desired bitrate. Use GetEncodeBitrates to find the possibilities for your input type |
◆ EncodeToMp3() [2/2]
static void NAudio.Wave.MediaFoundationEncoder.EncodeToMp3 |
( |
IWaveProvider | inputProvider, |
|
|
string | outputFile, |
|
|
int | desiredBitRate = 192000 ) |
|
inlinestatic |
Encodes the input audio data to MP3 format and writes it to the specified output stream using the specified bit rate.
- Parameters
-
inputProvider | The input audio data provider. |
outputStream | The output stream to write the encoded MP3 data to. |
desiredBitRate | The desired bit rate for the MP3 encoding, defaults to 192000 bps if not specified. |
- Exceptions
-
InvalidOperationException | Thrown when no suitable MP3 encoders are available. |
This method selects the appropriate media type for MP3 encoding based on the input audio format and desired bit rate. It then uses the selected encoder to encode the input audio data and writes the encoded MP3 data to the output stream.
◆ EncodeToWma() [1/2]
static void NAudio.Wave.MediaFoundationEncoder.EncodeToWma |
( |
IWaveProvider | inputProvider, |
|
|
Stream | outputStream, |
|
|
int | desiredBitRate = 192000 ) |
|
inlinestatic |
Helper function to simplify encoding Window Media Audio Should be supported on Vista and above (not tested)
- Parameters
-
inputProvider | Input provider, must be PCM |
outputStream | Output stream |
desiredBitRate | Desired bitrate. Use GetEncodeBitrates to find the possibilities for your input type |
◆ EncodeToWma() [2/2]
static void NAudio.Wave.MediaFoundationEncoder.EncodeToWma |
( |
IWaveProvider | inputProvider, |
|
|
string | outputFile, |
|
|
int | desiredBitRate = 192000 ) |
|
inlinestatic |
Encodes the input audio data to WMA format and writes it to the output stream.
- Parameters
-
inputProvider | The input audio data provider. |
outputStream | The stream to which the encoded WMA data will be written. |
desiredBitRate | The desired bit rate for the WMA encoding (default is 192000). |
- Exceptions
-
InvalidOperationException | Thrown when no suitable WMA encoders are available. |
This method selects the appropriate media type for WMA encoding based on the input audio format and desired bit rate. It then uses the selected media type to create a MediaFoundationEncoder, which is used to encode the input audio data to WMA format. The encoded data is written to the output stream in ASF container format.
◆ GetEncodeBitrates()
static int[] NAudio.Wave.MediaFoundationEncoder.GetEncodeBitrates |
( |
Guid | audioSubtype, |
|
|
int | sampleRate, |
|
|
int | channels ) |
|
inlinestatic |
Retrieves the array of encoded bitrates for the specified audio subtype, sample rate, and channel count.
- Parameters
-
audioSubtype | The unique identifier for the audio subtype. |
sampleRate | The sample rate of the audio. |
channels | The number of audio channels. |
- Returns
- An array containing the encoded bitrates for the specified audio configuration.
◆ GetOutputMediaTypes()
static MediaType[] NAudio.Wave.MediaFoundationEncoder.GetOutputMediaTypes |
( |
Guid | audioSubtype | ) |
|
|
inlinestatic |
Retrieves the available output media types for the specified audio subtype.
- Parameters
-
audioSubtype | The GUID of the audio subtype for which to retrieve the output media types. |
- Returns
- An array of MediaType objects representing the available output media types for the specified audio subtype.
- Exceptions
-
COMException | Thrown when an error occurs while retrieving the available output media types. |
◆ PerformEncode()
Performs encoding of the input audio data using the specified sink writer and wave provider.
- Parameters
-
writer | The sink writer used for writing the encoded data. |
streamIndex | The index of the stream to write the encoded data to. |
inputProvider | The wave provider containing the input audio data to be encoded. |
This method performs encoding of the input audio data using the specified sink writer and wave provider. It iterates through the input data, converts and writes each buffer to the specified stream index, and finalizes the writing process once all data has been processed.
◆ SelectMediaType()
static MediaType NAudio.Wave.MediaFoundationEncoder.SelectMediaType |
( |
Guid | audioSubtype, |
|
|
WaveFormat | inputFormat, |
|
|
int | desiredBitRate ) |
|
inlinestatic |
Selects the media type that best matches the input audio subtype, format, and desired bit rate.
- Parameters
-
audioSubtype | The audio subtype to be matched. |
inputFormat | The input wave format. |
desiredBitRate | The desired bit rate in bits per second. |
- Returns
- The media type that best matches the input criteria, or null if no match is found.
This method selects the media type that best matches the input audio subtype, wave format, and desired bit rate from the available output media types. It filters the output media types based on sample rate and channel count, calculates the delta between the desired bit rate and the average bytes per second of each media type, orders the filtered media types by delta, and returns the first matching media type or null if no match is found.
◆ disposed
bool NAudio.Wave.MediaFoundationEncoder.disposed |
|
private |
◆ outputMediaType
readonly MediaType NAudio.Wave.MediaFoundationEncoder.outputMediaType |
|
private |
The documentation for this class was generated from the following file: