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:
- Windows Media (WMV with WMA)
- MPEG-4 (H.264 with AAC)
- Configure the media session using the high level transcode API.
- Use source reader and sink writer.
The transcode API provides the following benefits:
- The media session controls the operation. Therefore, there is no need to manipulate the samples manually.
- Media samples are not brought to the managed environment which improves speed and memory usage.
Source reader and sink writer provide the following benefits:
- Media sample attributes can be edited individually which gives more control to the developer.
- The media sample raw data can be acquired from the buffers and used for other purposes (for example it is easy to copy the data from a video sample to a System.Drawing.Bitmap object).
When the application is executed the main window will show up (Figure 1).
- Select the media file you want to convert by clicking the “Browse…” button to the right of the “Source” text box. When you do this the source location will be automatically chosen as the output folder.
- Change the output folder by clicking the “Browse…” button to the right of the “Output folder” text box.
- Start playback by clicking on the media frame.
- Seek to different positions by moving the arrow located below the time bar.
- Convert only a selected portion of the media by moving the blue controls in the time bar to adjust the start and end times.
- Select the output media format from the “Format” dropdown list.
- Select the output video size from the “Video size” dropdown list.
- Bring up options for changing the audio codec, video codec, audio bit rate or video bit rate by clicking on “Advanced options” at the bottom-left corner of the window.
- Start the transcode operation by clicking continue. The progress window will show up and the conversion will begin (Figure 2).
10. The conversion window (Figure 2) will be automatically closed when the conversion is complete.
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.
The main C# project generated by Visual Studio. It contains information about the platforms, configurations and project features.
Definitions of the COM classes needed.
Media Foundation constants needed.
Media Foundation enumerations needed.
Definition of the Media Foundation COM interfaces needed to perform the format conversion.
Structures that help interoperating with Media Foundation COM objects.
C# interface used by the encoding classes.
C# class that inherits ISimpleEncode and encodes content using the Windows 7 transcode API.
C# class that inherits ISimpleEncode and encodes content using source reader and sink writer.
C# class that keeps track of the unmanaged memory used by samples and simplifies garbage collection.
Support classes used by the tool.
Exports the required Media Foundation functions from mfplat.dll and mf.dll.
Holds the attributes that control the startup behavior.
Interaction logic for App.xaml.
Graphic design and animations of the user control used to trim media content.
Interaction logic for VideoTrimmer.xaml which handles user input and events such as resizing.
The main application window used to load media files and set the conversion parameters.
Interaction logic for MainWindow.xaml.
The window that is shown during format conversion and displays the progress and estimated remaining time.
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.