Compress mp3 file using Lame.exe in C#

We can compress mp3 file using Lame.exe easily as follows. The following code will help us to reduce the mp3 file’s size without losing much more of quality. For that we need to put lame.exe file in our folder. Find lame.exe and download it.

Code:

private void mp3Encode(string srcFileName, string targetFileName)
{
if (!string.IsNullOrEmpty(srcFileName) &&
!string.IsNullOrEmpty(targetFileName))
{
try
{
string workingDir = AppDomain.CurrentDomain.BaseDirectory + "
\\";
string outfile = "-V9 -q0 --resample 32 -B56 --verbose " + '"' + srcFileName + "\" \"" +

targetFileName + "\"";
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = workingDir + "lame.exe";
psi.Arguments = outfile;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p = System.Diagnostics.Process.Start(psi);
while (p.HasExited == false)
{
Thread.Sleep(10);
Application.DoEvents();
}
p.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}

private void frmConvertor_FormClosing(object sender, FormClosingEventArgs e)
{
if (p != null)
{
p.Dispose();
}
}



Hope this will help us to reduce our mp3 file’s size…

....S.VinothkumaR.

3 comments:

IAMXVIJAY said...

Hi Vinoth,
Rare post for lame. Good.
But i want to know one thing vinoth,
How can we use this for web application(asp.net c#)?

IAMXVIJAY said...

Vinoth, what the type of the variable p. "p = System.Diagnostics.Process.Start(psi);
".
Getting compile err in this line
please help me.

Hira said...

File is converted successfully but the quality is not good. how can i improve the quality.