MFCopy.exe - This is a command line tool showcasing the Windows 7 Media Foundation source reader and sink writer APIs. This tool copies multimedia files from one multimedia container to another. Copying can involve simply remultiplexing the streams, transcoding to convert the streams to a different format. The source code is provided to use as a reference for learning about the source reader and sink writer.
Transcoding is converting from one media format to another. The previous blog post described three mechanisms that Media Foundation provides to perform transcoding, taking a detailed look at the transcode API.
- Configure the Media Foundation media session directly.
- Use the higher level transcode API to configure the media session.
- Use source reader and sink writer.
Figure 1. Three ways to perform transcoding
We will now describe the source reader and sink writer objects, and how they can be used for transcoding.
The source reader is a Media Foundation component that provides direct access to multimedia data. Applications can use the source reader to extract sample data in the native format (usually compressed, but not necessarily), or can request that the source reader decode the multimedia data and output uncompressed samples. The source reader provides a consistent API regardless or container or format. Since it is built on the Media Foundation platform, the source reader supports any of the media sources and codecs registered with the platform.
How to use the Source Reader:
- Create the reader object from either a URL, a byte stream, or an existing media source instance.
- Select or deselect streams.
- Configure the desired output format for the streams.
- Seek to the starting position if desired.
- Process multimedia data by calling IMFSourceReader::ReadSample until reaching the end of the stream, or until you have processed as much data as you want. The source reader supports both synchronous and asynchronous processing.
To read more about the source reader: https://msdn.microsoft.com/en-us/library/dd389281(VS.85).aspx
The sink writer is a Media Foundation component that provides a simple way to generate multimedia files. The application configures the target container and format for each stream and then gives each sample received from the source reader to the writer object. Like the source reader, the sink writer provides a consistent API regardless of the destination container or format.
How to use the Sink Writer:
- Create the writer object from either a URL, a byte stream, or an existing media sink instance.
- Add output streams, specifying the format of the data that will be written to the file.
- Configure the input format, specifying the format of the data that will be passed to sink writer.
- Call IMFSinkWriter::BeginWriting.
- Process multimedia data by calling IMFSinkWriter::WriteSample until the end of the stream.Call IMFSinkWriter::Finalize to complete the file authoring process. This method can either block or call an asynchronous callback method when it completes.
To read more about the sink writer: https://msdn.microsoft.com/en-us/library/dd374642(VS.85).aspx
The Visual Studio project file
Contains the application entry point and all user interaction, including command-line parsing and progress notification.
Implements the copying engine that configures the source reader and sink writer and processes multimedia samples.
mfcopy.exe [-?] [-a audio_format] [-nc num_channels] [-sr sample_rate] [-v video format] [-fs resolution] [-fr framerate] [-im interlace_mode] [-br video_bitrate] [-q] [-s milliseconds] [-d milliseconds] [-d milliseconds] [-hw] [-xa] [-xv] [-xo] [-xm] input_filename output_filename
Display usage information.
Set the output audio format. Defaults to the native format from the input file.
Examples: AAC, WMAudioV8, WMAudioV9, WMAudio_Lossless
Set the number of audio channels.
Resample the audio to the specified rate.
Examples: 44100, 48000
Set the output video format. Defaults to the native format from the input file.
Examples: H264, WMV2, WMV3, WVC1
Resize the video to the specified width and height.
Convert the video frame rate to the specified rate.
Set the video interlace mode.
Set the average encoded bit rate for the video stream.
Specify a start position in milliseconds.
Specify the duration to copy in milliseconds.
Enable hardware transforms.
Exclude audio streams.
Exclude video streams.
Exclude non audio/video streams (for example, text or script streams).
The path to the source file.
The path to the destination file.
- Copy 20 seconds of a WMV file without re-encoding: mfcopy.exe -d 20000 input.wmv output.wmv
- Resize the video from a WMV file while preserving the original audio stream: mfcopy.exe -fs 320x240 input.wmv output.wmv
- Transcode WMV to MP4 ensuring progressive video: mfcopy.exe -a AAC -v H264 -im Progressive input.wmv output.mp4
- Extract the audio stream from a WMV clip: mfcopy.exe -xv input.wmv output.wma
- The in-box Media Foundation file archive sinks support a maximum of one audio stream and one video stream in the output file.
- The Media Foundation source reader and sink writer do not support DRM transcoding.
- This tool cannot transcode to the MP3 format unless a third-party MP3 encoder MFT is installed. This is because the inbox MP3 encoder MFT is licensed for Microsoft applications only and will not work when loaded by third-party applications.
- Remuxing to a different container type might fail due to the specific container requirements.
Downloading the Sample
You can download MFCopy from https://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=mfblog&DownloadId=8574
Follow the instruction at https://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.