Click here to Skip to main content
15,890,947 members
Articles / Programming Languages / C#
Tip/Trick

Wave to MP3 Converter

Rate me:
Please Sign up or sign in to vote.
4.75/5 (5 votes)
23 Oct 2013CPOL1 min read 20.7K   692   4   3
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.

C#
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.

C#
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.

C#
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)


Written By
Software Developer (Senior)
Bangladesh Bangladesh
Microsoft Certified Professional Developer (MCPD) having about 5+ years of experience.

Comments and Discussions

 
Question"Converter"? Pin
dandy7224-Oct-13 11:31
dandy7224-Oct-13 11:31 
AnswerRe: "Converter"? Pin
M Rayhan24-Oct-13 19:11
M Rayhan24-Oct-13 19:11 
GeneralRe: "Converter"? Pin
dandy7225-Oct-13 4:16
dandy7225-Oct-13 4:16 

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

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