private static IMFSinkWriter CreateSinkWriter(string outputFile) { // n.b. could try specifying the container type using attributes, but I think // it does a decent job of working it out from the file extension // n.b. AAC encode on Win 8 can have AAC extension, but use MP4 in win 7 // http://msdn.microsoft.com/en-gb/library/windows/desktop/dd389284%28v=vs.85%29.aspx IMFSinkWriter writer; var attributes = MediaFoundationApi.CreateAttributes(1); attributes.SetUINT32(MediaFoundationAttributes.MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, 1); try { MediaFoundationInterop.MFCreateSinkWriterFromURL(outputFile, null, attributes, out writer); } catch (COMException e) { if (e.GetHResult() == MediaFoundationErrors.MF_E_NOT_FOUND) { throw new ArgumentException("Was not able to create a sink writer for this file extension"); } throw; } finally { Marshal.ReleaseComObject(attributes); } return(writer); }
/// <summary> /// Gets all the available media types for a particular /// </summary> /// <param name="audioSubtype">Audio subtype - a value from the AudioSubtypes class</param> /// <returns>An array of available media types that can be encoded with this subtype</returns> public static MediaType[] GetOutputMediaTypes(Guid audioSubtype) { IMFCollection availableTypes; try { MediaFoundationInterop.MFTranscodeGetAudioOutputAvailableTypes( audioSubtype, _MFT_ENUM_FLAG.MFT_ENUM_FLAG_ALL, null, out availableTypes); } catch (COMException c) { if (c.GetHResult() == MediaFoundationErrors.MF_E_NOT_FOUND) { // Don't worry if we didn't find any - just means no encoder available for this type return(new MediaType[0]); } else { throw; } } availableTypes.GetElementCount(out int count); var mediaTypes = new List <MediaType>(count); for (int n = 0; n < count; n++) { availableTypes.GetElement(n, out object mediaTypeObject); var mediaType = (IMFMediaType)mediaTypeObject; mediaTypes.Add(new MediaType(mediaType)); } Marshal.ReleaseComObject(availableTypes); return(mediaTypes.ToArray()); }
/// <summary> /// Creates the reader (overridable by ) /// </summary> protected virtual IMFSourceReader CreateReader(MediaFoundationReaderSettings settings) { IMFSourceReader reader; MediaFoundationInterop.MFCreateSourceReaderFromURL(file, null, out reader); reader.SetStreamSelection(MediaFoundationInterop.MF_SOURCE_READER_ALL_STREAMS, false); reader.SetStreamSelection(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, true); // Create a partial media type indicating that we want uncompressed PCM audio var partialMediaType = new MediaType(); partialMediaType.MajorType = MediaTypes.MFMediaType_Audio; partialMediaType.SubType = settings.RequestFloatOutput ? AudioSubtypes.MFAudioFormat_Float : AudioSubtypes.MFAudioFormat_PCM; var currentMediaType = GetCurrentMediaType(reader); // mono, low sample rate files can go wrong on Windows 10 unless we specify here partialMediaType.ChannelCount = currentMediaType.ChannelCount; partialMediaType.SampleRate = currentMediaType.SampleRate; // set the media type // can return MF_E_INVALIDMEDIATYPE if not supported reader.SetCurrentMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, IntPtr.Zero, partialMediaType.MediaFoundationObject); Marshal.ReleaseComObject(currentMediaType.MediaFoundationObject); return(reader); }
/// <summary> /// Creates the reader (overridable by ) /// </summary> protected virtual IMFSourceReader CreateReader(MediaFoundationReaderSettings settings) { IMFSourceReader reader; // If the file exists. if (!String.IsNullOrEmpty(file)) { MediaFoundationInterop.MFCreateSourceReaderFromURL(file, null, out reader); } else { IMFByteStream byteStream = MediaFoundationApi.CreateByteStreamFromStream(_inputStream); MediaFoundationInterop.MFCreateSourceReaderFromByteStream(byteStream, null, out reader); } reader.SetStreamSelection(MediaFoundationInterop.MF_SOURCE_READER_ALL_STREAMS, false); reader.SetStreamSelection(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, true); // Create a partial media type indicating that we want uncompressed PCM audio var partialMediaType = new MediaType(); partialMediaType.MajorType = MediaTypes.MFMediaType_Audio; partialMediaType.SubType = settings.RequestFloatOutput ? AudioSubtypes.MFAudioFormat_Float : AudioSubtypes.MFAudioFormat_PCM; // set the media type // can return MF_E_INVALIDMEDIATYPE if not supported reader.SetCurrentMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, IntPtr.Zero, partialMediaType.MediaFoundationObject); return(reader); }
/// <summary> /// Gets all the available media types for a particular /// </summary> /// <param name="audioSubtype">Audio subtype - a value from the AudioSubtypes class</param> /// <returns>An array of available media types that can be encoded with this subtype</returns> // Token: 0x0600093B RID: 2363 RVA: 0x0001AB68 File Offset: 0x00018D68 public static MediaType[] GetOutputMediaTypes(Guid audioSubtype) { IMFCollection imfcollection; try { MediaFoundationInterop.MFTranscodeGetAudioOutputAvailableTypes(audioSubtype, _MFT_ENUM_FLAG.MFT_ENUM_FLAG_ALL, null, out imfcollection); } catch (COMException exception) { if (exception.GetHResult() == -1072875819) { return(new MediaType[0]); } throw; } int num; imfcollection.GetElementCount(out num); List <MediaType> list = new List <MediaType>(num); for (int i = 0; i < num; i++) { object obj; imfcollection.GetElement(i, out obj); IMFMediaType mediaType = (IMFMediaType)obj; list.Add(new MediaType(mediaType)); } Marshal.ReleaseComObject(imfcollection); return(list.ToArray()); }
// Token: 0x06000942 RID: 2370 RVA: 0x0001AFC4 File Offset: 0x000191C4 private static IMFSinkWriter CreateSinkWriter(string outputFile) { IMFAttributes imfattributes = MediaFoundationApi.CreateAttributes(1); imfattributes.SetUINT32(MediaFoundationAttributes.MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, 1); IMFSinkWriter result; try { MediaFoundationInterop.MFCreateSinkWriterFromURL(outputFile, null, imfattributes, out result); } catch (COMException exception) { if (exception.GetHResult() == -1072875819) { throw new ArgumentException("Was not able to create a sink writer for this file extension"); } throw; } finally { Marshal.ReleaseComObject(imfattributes); } return(result); }
protected virtual IMFSourceReader CreateReader(MediaFoundationReader.MediaFoundationReaderSettings settings) { IMFSourceReader imfsourceReader; MediaFoundationInterop.MFCreateSourceReaderFromURL(this.file, null, out imfsourceReader); imfsourceReader.SetStreamSelection(-2, false); imfsourceReader.SetStreamSelection(-3, true); MediaType mediaType = new MediaType(); mediaType.MajorType = MediaTypes.MFMediaType_Audio; mediaType.SubType = (settings.RequestFloatOutput ? AudioSubtypes.MFAudioFormat_Float : AudioSubtypes.MFAudioFormat_PCM); imfsourceReader.SetCurrentMediaType(-3, IntPtr.Zero, mediaType.MediaFoundationObject); return(imfsourceReader); }
public Video(string path, bool loop = false) { _path = path; Loop = loop; // Get the frame size MediaFoundationInterop.MFCreateSourceReaderFromURL(_path, null, out IMFSourceReader reader); reader.GetNativeMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, out IMFMediaType nativeType); nativeType.GetUINT64(MF_MT_FRAME_SIZE, out long size); _videoWidth = (int)((size >> 32) & 0x7FFFFFFF); _videoHeight = (int)(size & 0x7FFFFFFF); _length = GetLength(reader); Marshal.ReleaseComObject(nativeType); Marshal.ReleaseComObject(reader); Pack(); }
private static void InitializeVariables(string[] args) { Console.OutputEncoding = Encoding.UTF8; if (Environment.IsLinux == false) { MediaFoundationInterop.MFStartup(0); } WordsFile ??= @"/tmp/words-list.txt"; OutputMp3File ??= "!result.mp3"; AudioPattern ??= //"EnRu 2..1.1.1.1.1..."; //"RuDe 1..2.2.2.2.2..."; "DeDe 1..2..."; }
public static int[] GetEncodeBitrates(Guid audioSubtype, int sampleRate, int channels) { var bitRates = new HashSet <int>(); IMFCollection availableTypes; MediaFoundationInterop.MFTranscodeGetAudioOutputAvailableTypes( audioSubtype, _MFT_ENUM_FLAG.MFT_ENUM_FLAG_ALL, null, out availableTypes); int count; availableTypes.GetElementCount(out count); for (int n = 0; n < count; n++) { object mediaTypeObject; availableTypes.GetElement(n, out mediaTypeObject); var mediaType = (IMFMediaType)mediaTypeObject; // filter out types that are for the wrong sample rate and channels int samplesPerSecond; mediaType.GetUINT32(MediaFoundationAttributes.MF_MT_AUDIO_SAMPLES_PER_SECOND, out samplesPerSecond); if (sampleRate != samplesPerSecond) { continue; } int channelCount; mediaType.GetUINT32(MediaFoundationAttributes.MF_MT_AUDIO_NUM_CHANNELS, out channelCount); if (channels != channelCount) { continue; } int bytesPerSecond; mediaType.GetUINT32(MediaFoundationAttributes.MF_MT_AUDIO_AVG_BYTES_PER_SECOND, out bytesPerSecond); bitRates.Add(bytesPerSecond * 8); Marshal.ReleaseComObject(mediaType); } Marshal.ReleaseComObject(availableTypes); return(bitRates.ToArray()); }