Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C#

Convert MP3, MPEG, AVI to Windows Media Formats

Rate me:
Please Sign up or sign in to vote.
4.34/5 (27 votes)
30 May 2005CPOL8 min read 382.6K   11.8K   113  
Video conversion has never been this simple. Use the new Windows Media Encoder to convert different video and audio formats to stream Microsoft audio video standard formats like WMV and WMA.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using WMEncoderLib;
using WMPREVIEWLib;
using System.IO;

namespace JoinTwoFile
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
	
		public static string profileName = @"C:\Program Files\Windows Media Components\Encoder\Profiles\scmeda.prx";
		public static string movie_source1 =Environment.CurrentDirectory + "\\source1.avi";
		public static string movie_source2	 = Environment.CurrentDirectory +"\\source2.avi";
		public static string movie_dest = Environment.CurrentDirectory +"\\joinedFile.wmv";
		bool glbboolStartNext= false;
		WMEncoder glbEncoder = new WMEncoder();
		IWMEncSourceGroupCollection SrcGrpColl=null;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Enabled = false;
			this.Name = "Form1";
			this.Opacity = 0;
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			if ( args.Length >0 && !"".Equals(args[0]) )
				movie_source1 =args[0];
			if ( args.Length >1 && !"".Equals(args[1]) )
				movie_source2 =args[1];
			if (args.Length >2 && !"".Equals(args[2]) )
				movie_dest =args[2]; 
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			this.Visible = false;
			EncodeFromConsole();
			this.Visible = false;
			
		}
		
		private bool sEncodeFile(string source1,string source2 ,string destination)
		{

			WMEncProfile2 Pro = new WMEncProfile2();
			Int32 intProContentType=0;
			int intProVBRModeAudio=0;
			int intProVBRModeVideo=0;
			try 
			{
				Pro.LoadFromFile(profileName);
				intProContentType = Pro.ContentType;
				intProVBRModeAudio = (int)Pro.get_VBRMode(WMENC_SOURCE_TYPE.WMENC_AUDIO, 0);
				intProVBRModeVideo = (int)Pro.get_VBRMode(WMENC_SOURCE_TYPE.WMENC_VIDEO, 0);
			} 
			catch (Exception ex) 
			{
				Console.Error.WriteLine ( ex.Message);
				return false;
			}
			IWMEncSourceGroup2 SrcGrp=null;
			IWMEncSourceGroup2 SrcGrp2=null;
			try 
			{
				SrcGrpColl = glbEncoder.SourceGroupCollection;
				SrcGrp = (IWMEncSourceGroup2)SrcGrpColl.Add("SG1");
				SrcGrp2 = (IWMEncSourceGroup2)SrcGrpColl.Add ("SG2");
			} 
			catch (Exception ex) 
			{
				Console.Error.WriteLine ( ex.Message);
				return false;
			}
			IWMEncAudioSource SrcAud=null;
			IWMEncVideoSource2 SrcVid=null;
			IWMEncAudioSource SrcAud2=null;
			IWMEncVideoSource2 SrcVid2=null;
			string strDestExtension = sReturnExtension(destination);
			try 
			{
				SrcAud = (WMEncoderLib.IWMEncAudioSource )SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
				SrcAud.SetInput(source1,"","");
				SrcVid = (WMEncoderLib.IWMEncVideoSource2 )SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
				SrcVid.SetInput(source1,"","");
				
				SrcAud2 = (WMEncoderLib.IWMEncAudioSource )SrcGrp2.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
				SrcAud2.SetInput(source2,"","");
				SrcVid2 = (WMEncoderLib.IWMEncVideoSource2 )SrcGrp2.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
				SrcVid2.SetInput(source2,"","");
			} 
			catch (Exception ex) 
			{
				Console.Error.WriteLine ( ex.Message);
				return false;
			}
			try 
			{

				SrcGrp.set_Profile(Pro);
				SrcGrp2.set_Profile(Pro);
				SrcGrp.SetAutoRollover (-1, "SG2");
			} 
			catch (Exception ex) 
			{
				Console.Error.WriteLine ( ex.Message);
				return false;
			}

			SrcVid.Optimization =(WMEncoderLib.WMENC_VIDEO_OPTIMIZATION ) WMENC_VIDEO_OPTIMIZATION.WMENC_VIDEO_STANDARD;
			
			SrcAud.PreProcessPass = 0;
			SrcVid.PreProcessPass = 0;

				
			IWMEncFile2 File =(IWMEncFile2) glbEncoder.File;
			try 
			{
				File.LocalFileName = destination;
			} 
			catch (Exception ex) 
			{
				Console.Error.WriteLine ( ex.Message);
				return false;	
			}
			try 
			{
				glbEncoder.PrepareToEncode(true);
			} 
			catch (Exception ex) 
			{
				Console.Error.WriteLine ( ex.Message);
				return false;
			}
			try 
			{
				glbEncoder.Start();
			} 
			catch (Exception ex) 
			{
				Console.Error.WriteLine ( ex.Message);
				return false;
			}
			return true;
		}

		public void EncodeFromConsole()
		{
			glbEncoder.OnStateChange+=new _IWMEncoderEvents_OnStateChangeEventHandler(this.Encoder_OnStateChange); 
			bool boolEncodeStart = sEncodeFile(movie_source1,movie_source2  ,movie_dest );
			if(boolEncodeStart)
				Console.WriteLine("Joining Two Files, "+movie_source1+" and "+movie_source2+" ..........");
			glbboolStartNext= !boolEncodeStart;
			FileInfo f = new FileInfo(movie_dest );
			while (!glbboolStartNext ) 
			{
				Application.DoEvents ();
					
					
			}
				
			Console.WriteLine  ("Finished - Files "+movie_source1+" and "+movie_source2+" have joined to form " + movie_dest );
			Application.Exit ();
		}

		private string sReturnExtension(string strValue)
		{
			try 
			{
				return strValue.Substring (strValue.Length - 3).ToLower ();
			} 
			catch (Exception ex) 
			{
				Console.Error.WriteLine(ex.Message );
			}
			return "";
		}

		private void Encoder_OnStateChange(WMEncoderLib.WMENC_ENCODER_STATE enumState)
		{
			string strRunState="";
			if (enumState == WMENC_ENCODER_STATE.WMENC_ENCODER_STARTING) 
			{
				strRunState = "Encoder Starting";
			} 
			else if (enumState == WMENC_ENCODER_STATE.WMENC_ENCODER_RUNNING) 
			{
				strRunState = "Encoder Running Pass 1 of 1";
			} 
			else if (enumState == WMENC_ENCODER_STATE.WMENC_ENCODER_END_PREPROCESS) 
			{
				strRunState = "Encoder End Preprocess";
			} 
			else if (enumState == WMENC_ENCODER_STATE.WMENC_ENCODER_PAUSING) 
			{
				strRunState = "Encoder Pausing";
			} 
			else if (enumState == WMENC_ENCODER_STATE.WMENC_ENCODER_PAUSED) 
			{
				strRunState = "Encoder Paused";
			} 
			else if (enumState == WMENC_ENCODER_STATE.WMENC_ENCODER_STOPPING) 
			{
				strRunState = "Encoder Stopping";
			} 
			else if (enumState == WMENC_ENCODER_STATE.WMENC_ENCODER_STOPPED) 
			{
				strRunState = "Encoder Stopped";
				sRemoveSrcGrpColl();
				glbboolStartNext = true;
			}
		}
			
		private void sRemoveSrcGrpColl()
		{
			try
			{			
				SrcGrpColl.Remove(0);
			} 
			catch (Exception ex) 
			{
				Console.Error.WriteLine (ex.Message );
			}
		}
	}

	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions