Click here to Skip to main content
Click here to Skip to main content
Go to top

Wave to MP3 Converter

, 23 Oct 2013
Rate this:
Please Sign up or sign in to vote.
Simple wav2mp3 converter console application.

Introduction

At work, I needed to convert wave files to MP3 files on a regular basis. So I developed this console program in C# and placed it in Windows Scheduler so that it runs on a regular basis. Actually I just searched the internet and found many solutions and then compiled all that into a console application. You can download the source code from the link.

Using the code

lame.exe is used here to convert a Wave file to MP3.

class WaveToMP3
{
    public void mciConvertWavMP3(string fileName, bool waitFlag, string outdir)
    {
        string converterExeDir = AppDomain.CurrentDomain.BaseDirectory + 
                 "Converter\\"; //location of lame.exe file
        //remember, i used forward slash in app.config file
        //for input and output directory so '/' is used here
        string[] splittedFileName = fileName.Split('/');
        string fileNamePart = splittedFileName[splittedFileName.Length - 1];//get the last part as the file name
        string pworkingDir = outdir;
        //string outfile = "-b 32 --resample 22.05 -m m \"" + pworkingDir + 
          fileName + "\" \"" + pworkingDir + 
          fileName.Replace(".wav", ".mp3") + "\"";
        string outfile = "-b 32 --resample 22.05 -m m \"" + fileName + 
          "\" \"" + pworkingDir + 
          fileNamePart.Replace(".wav", ".mp3") + "\"";
        System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
        psi.FileName = "\"" + converterExeDir + "lame.exe" + "\"";
        psi.Arguments = outfile;
        psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
        System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
        if (waitFlag)
        {
            p.WaitForExit();
        }
    }
}  

A text based logging system is used to keep the error log in the filesystem.

class CreateLogFiles
{

    //text log added by rayhan
    private string sLogFormat;
    private string sErrorTime;

    public CreateLogFiles()
    {
        //sLogFormat used to create log files format :
        // dd/mm/yyyy hh:mm:ss AM/PM ==> Log Message
        sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + 
          DateTime.Now.ToLongTimeString().ToString() + " ==> ";

        //this variable used to create log filename format "
        //for example filename : ErrorLogYYYYMMDD
        string sYear = DateTime.Now.Year.ToString();
        string sMonth = DateTime.Now.Month.ToString();
        string sDay = DateTime.Now.Day.ToString();
        sErrorTime = sYear + sMonth + sDay;
    }

    public void ErrorLog(string sPathName, string sErrMsg)
    {
        StreamWriter sw = new StreamWriter(sPathName + sErrorTime, true);
        sw.WriteLine(sLogFormat + sErrMsg);
        sw.Flush();
        sw.Close();
    }
}

In my requirements, it is required to delete the file after conversion but I commented that line in the code. If you need to delete the file, then just uncomment the line.

class Program
{
    static void Main(string[] args)
    {
        string inputPath = ConfigurationManager.AppSettings["inputpath"];

        string outputPath = ConfigurationManager.AppSettings["outputpath"];

        string[] fileNames = Directory.GetFiles(inputPath);

        WaveToMP3 p = new WaveToMP3();
        try
        {
            foreach (string fileName in fileNames)
            {
                p.mciConvertWavMP3(fileName, true, outputPath);
                System.Threading.Thread.Sleep(1000);//just some wait
                //File.Delete(fileName);
            }
        }
        catch (Exception ex)
        {
            CreateLogFiles Err = new CreateLogFiles();
            Err.ErrorLog(AppDomain.CurrentDomain.BaseDirectory, ex.ToString() + 
              " " + ex.StackTrace + "  " + ex.Source);
        }
    }
}

Your input and output directory path should be placed in the app.config file. In my case, they are D:/in/ and D:/out/. Remember to use the forward slash for the path because I used forward slash in the program to split the filename and path.

Conclusion

This is my first writing on CodeProject.com. Hope this will meet your needs. If you have any questions, feel free to ask.

License

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

Share

About the Author

M Rayhan
Software Developer (Senior)
Bangladesh Bangladesh
Microsoft Certified Professional Developer (MCPD) having about 4 years of experience.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
Question"Converter"? Pinmemberdandy7224-Oct-13 11:31 
AnswerRe: "Converter"? PinmemberM Rayhan24-Oct-13 19:11 
GeneralRe: "Converter"? Pinmemberdandy7225-Oct-13 4:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 23 Oct 2013
Article Copyright 2013 by M Rayhan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid