Encoding WMV file in C# .Net Objective: Encoding WMV files for streaming in C# .NET
Steps:
1. Create a windows class library project.
2. You should install windows Media Encoder.
3. Add the windows Media Encoder component reference.
4. Copy and paste the below method inside the class file.
If You call this method with inputfilename,outputFilename,encodingProfileName, You will get the encoded WMV file.
private void DoEncode(string inputFileName, string outputFilename, string encodingProfileName)
{
IWMEncSourceGroupCollection srcGrpColl;
IWMEncSourceGroup srcGrp;
IWMEncSource srcAud;
IWMEncVideoSource2 srcVid;
IWMEncFile2 outFile;
IWMEncProfile2 pro;
if (File.Exists(inputFileName) && File.Exists(encodingProfileName)))
{
encoder = new WMEncoder();
srcGrpColl = encoder.SourceGroupCollection;
srcGrp = srcGrpColl.Add("WmvSrc");
srcAud = srcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
srcVid = (IWMEncVideoSource2)srcGrp.AddSource(WMENC_SOURCE_ TYPE.WMENC_VIDEO);
srcAud.SetInput(inputFileName, "", "");
srcVid.SetInput(inputFileName, "", "");
pro = new WMEncProfile2();
pro.LoadFromFile(inputFileName);
srcGrp.set_Profile(pro);
outFile = (IWMEncFile2)encoder.File;
outFile.LocalFileName = outputFilename;
encoder.PrepareToEncode(true);
encoder.Start();
}
else
{
throw new ApplicationException("File not found.");
}
} |