Programowa konwersja wav na wma (bezstratny) w c#

Przykład zastosowania Windows Media Encoder w programie c#.
Konwertuje .wav na bezstratnte  .wma.
 
(Windows Media Encoder  – an example of use in c#. This program converts .wav into losless .wma )
 
 
 

using System;
using System.Text;
//
using WMEncoderLib;
//
using System.IO;
using System.Runtime.InteropServices;

namespace Wav2Wma
{
    class Program
    {
        //
        static readonly string PROFILE_NAME = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"Windows Media ComponentsEncoderSettingsd6_qvbr_100_audio.prx";
        //
        static Boolean bDone;
        //
        static WMEncoder enc = null;
        static IWMEncSourceGroup srcGrp = null;
        static IWMEncSource srcAud = null;
        static IWMEncProfile2 profile2 = null;
        //static IWMEncAudienceObj audience = null;
        //
        //
        //
        static int Main(string[] args)
        {
            //
            string wavPathName = "";
            string wmaPathName = "";
            //
            bDone = false;
            //
            if (!File.Exists(PROFILE_NAME))
            {
                Console.WriteLine("{0}: ERROR - Windows Media Encoder not installed!", Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]));

                return 2;
            }
            //
            if ((args.Length < 1) || (args.Length > 2))
            {
                Usage();
                return 1;
            }
            //
            if (1 == args.Length)
            {
                wavPathName = args[0];
                wmaPathName = Path.Combine(Path.GetDirectoryName(wavPathName), Path.GetFileNameWithoutExtension(wavPathName) + ".wma");
            }
            else if (1 == args.Length)
            {
                wavPathName = args[0];
                wmaPathName = args[1];
            }
            return Do(wavPathName, wmaPathName) ? 0 : -1;
        }//Main
        //
        static void Usage()
        {
            Console.WriteLine("Usage:");
            Console.WriteLine("{0} wavPathName wmaPathName", Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]));
            Console.WriteLine("or");
            Console.WriteLine("{0} wavPathName", Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]));
        }
        //
        static bool Do(string wavPathName, string wmaPathName)
        {
            //
            bool retVal = true;
            //
            try
            {
                profile2 = new WMEncProfile2();
                enc = new WMEncoder();
                //
                profile2.LoadFromFile(PROFILE_NAME);
                profile2.ValidateMode = true;
                //
                srcGrp = enc.SourceGroupCollection.Add("SG_1");

                srcAud = srcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
                srcAud.SetInput(Path.GetFullPath(wavPathName), "", "");
                //
                enc.File.LocalFileName = Path.GetFullPath(wmaPathName);
                //
                enc.DisplayInfo.Title = Path.GetFileNameWithoutExtension(wavPathName);
                //
                profile2.Validate();
                //
                srcGrp.set_Profile((IWMEncProfile)profile2);
                //
                //start event handler
                enc.OnStateChange += new _IWMEncoderEvents_OnStateChangeEventHandler(OnStateChange);
                enc.PrepareToEncode(true);
                //
                {
                    ConsoleColor fg = Console.ForegroundColor;
                    //
                    Console.WriteLine();
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Profile={0}", profile2.ProfileName);
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("Input= [{0}]", wavPathName);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Output=[{0}]", wmaPathName);
                    Console.WriteLine();
                    //
                    Console.ForegroundColor = fg;
                }
                //
                enc.Start();
                //
                do
                {
                    System.Threading.Thread.Sleep(1000);
                }
                while (!bDone);
                //
                //stop event handler
                enc.OnStateChange -= new _IWMEncoderEvents_OnStateChangeEventHandler(OnStateChange);
                //
                enc.Flush(); System.Threading.Thread.Sleep(200);
                //
                enc.PrepareToEncode(false);
                enc.Stop();
                //
            }
            catch (Exception e)
            {
                ConsoleColor fg = Console.ForegroundColor;
                //
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("*** ERROR: {0}", e.Message);
                //
                Console.ForegroundColor = fg;
                System.Threading.Thread.Sleep(5000);
                retVal = false;
            }
            finally
            {
                if (enc != null) Marshal.ReleaseComObject(enc);
                if (srcGrp != null) Marshal.ReleaseComObject(srcGrp);
                if (srcAud != null) Marshal.ReleaseComObject(srcAud);
                if (profile2 != null) Marshal.ReleaseComObject(profile2);
                //if (audience != null) Marshal.ReleaseComObject(audience);
                //
            }
            //
            return retVal;
        }
        //
        //
        //
        static void OnStateChange(WMENC_ENCODER_STATE enumState)
        {
            switch (enumState)
            {
                case WMENC_ENCODER_STATE.WMENC_ENCODER_RUNNING:
                    // TODO: Handle running state.
                    Console.WriteLine("Running...");
                    break;

                case WMENC_ENCODER_STATE.WMENC_ENCODER_STOPPED:
                    // TODO: Handle stopped state.
                    Console.WriteLine("Stopped...");
                    bDone = true;
                    break;

                case WMENC_ENCODER_STATE.WMENC_ENCODER_STARTING:
                    // TODO: Handle starting state.
                    Console.WriteLine("Starting...");
                    break;

                case WMENC_ENCODER_STATE.WMENC_ENCODER_PAUSING:
                    // TODO: Handle pausing state.
                    Console.WriteLine("Pausing...");
                    break;

                case WMENC_ENCODER_STATE.WMENC_ENCODER_STOPPING:
                    // TODO: Handle stopping state.
                    Console.WriteLine("Stopping...");
                    break;

                case WMENC_ENCODER_STATE.WMENC_ENCODER_PAUSED:
                    // TODO: Handle paused state.
                    Console.WriteLine("Paused...");
                    break;

                case WMENC_ENCODER_STATE.WMENC_ENCODER_END_PREPROCESS:
                    // TODO: Handle end preprocess state.
                    Console.WriteLine("Preprocessing...");
                    break;
            }
        }
        //
        //
    }//class
}//namespace

Reklamy
Ten wpis został opublikowany w kategorii Komputery i Internet. Dodaj zakładkę do bezpośredniego odnośnika.

Skomentuj

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj / Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj / Zmień )

Facebook photo

Komentujesz korzystając z konta Facebook. Wyloguj / Zmień )

Google+ photo

Komentujesz korzystając z konta Google+. Wyloguj / Zmień )

Connecting to %s