MFManagedEncode.exe
This is a managed tool written in C# and XAML that converts files from one media format to another. The source code is provided to use as a reference on how to interop with source reader, sink writer and the transcode API from a managed environment.
The sample previews media files using WPF’s MediaElement object and a custom control that can be used to trim the output media.
Supported output formats:
Technique
The tool converts media files using the basic functionality of new APIs introduced with Windows 7. Two different methods are implemented by the tool:
Since Media Foundation is built on COM the required interfaces and unmanaged calls were ported to C# using .NET interoperability capabilities.
The transcode API provides the following benefits:
Source reader and sink writer provide the following benefits:
Usage
When the application is executed the main window will show up (Figure 1).
10. The conversion window (Figure 2) will be automatically closed when the conversion is complete.
Limitations
The sample is made using WPF so Visual Studio 2008 or later is required. If you don’t have Visual Studio 2008 the sample can be compiled using Visual C# 2008 Express Edition which can be downloaded for free from the Microsoft Website.
Only the COM interface methods used by the tool have been tested; marshaling of unused methods could be wrong or missing the correct enumerations or object definitions.
Project Structure
\MFManagedEncode.csproj
The main C# project generated by Visual Studio. It contains information about the platforms, configurations and project features.
\MediaFoundation\Interop\Classes.cs
Definitions of the COM classes needed.
\MediaFoundation\Interop\Constants.cs
Media Foundation constants needed.
\MediaFoundation\Interop\Enums.cs
Media Foundation enumerations needed.
\MediaFoundation\Interop\Interfaces.cs
Definition of the Media Foundation COM interfaces needed to perform the format conversion.
\MediaFoundation\Interop\Structures.cs
Structures that help interoperating with Media Foundation COM objects.
\MediaFoundation\Interfaces\ISimpleEncode.cs
C# interface used by the encoding classes.
\MediaFoundation\Classes\SimpleFastEncode.cs
C# class that inherits ISimpleEncode and encodes content using the Windows 7 transcode API.
\MediaFoundation\Classes\SimpleSinklWriterEncode.cs
C# class that inherits ISimpleEncode and encodes content using source reader and sink writer.
\MediaFoundation\Classes\EncapsulatedSample.cs
C# class that keeps track of the unmanaged memory used by samples and simplifies garbage collection.
\MediaFoundation\Common\Classes.cs
Support classes used by the tool.
\MediaFoundation\Common\Helper.cs
Exports the required Media Foundation functions from mfplat.dll and mf.dll.
\App.xaml
Holds the attributes that control the startup behavior.
\App.xaml.cs
Interaction logic for App.xaml.
\GUI\Controls\VideoTrimmer.xaml
Graphic design and animations of the user control used to trim media content.
\GUI\Controls\VideoTrimmer.xaml.cs
Interaction logic for VideoTrimmer.xaml which handles user input and events such as resizing.
\GUI\Windows\MainWindow.xaml
The main application window used to load media files and set the conversion parameters.
\GUI\Windows\MainWindow.xaml.cs
Interaction logic for MainWindow.xaml.
\GUI\Windows\ProgressWindow.xaml
The window that is shown during format conversion and displays the progress and estimated remaining time.
\GUI\Windows\ProgressWindow.xaml.cs
Interaction logic for ProgressWindow.xaml.
Downloading the Sample
You can download MFManagedEncode from http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=mfblog&DownloadId=9174
Follow the instruction at http://code.msdn.microsoft.com/mfblog for tips and instructions to build the sample.
This posting is provided "AS IS" with no warranties and confers no rights.