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\\";
string[] splittedFileName = fileName.Split('/');
string fileNamePart = splittedFileName[splittedFileName.Length - 1];
string pworkingDir = outdir;
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
{
private string sLogFormat;
private string sErrorTime;
public CreateLogFiles()
{
sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " +
DateTime.Now.ToLongTimeString().ToString() + " ==> ";
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);
}
}
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.