Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried several ways like lame.exe but i am unable to get result of it as my files are made up of 'WasapiLoopbackCapture()' out put .i have used lame too but my Program like below
C#
public void mciConvertWavMP3(string fileName, bool waitFlag, string pworkingDir, string pcopyDir)
        {
            DirectoryInfo newDir1 = new DirectoryInfo(pcopyDir);
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            string mpfile = fileName.ToLower().Replace(".wav", ".mp3");

            if (newDir1.GetFiles(mpfile).Length == 0)
            {
                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
                try
                {
                    //Response.Write("Conversion of " + pworkingDir + fileName + " is in progress ...");
                    string outfile = "-b 32 --resample 22.05 -m m \"" + pworkingDir + fileName + "\" \"" + pcopyDir + fileName.Replace(".wav", ".mp3") + "\"";
                    // string outfile = "-V \"" + pworkingDir + fileName + "\" \"" + pcopyDir + fileName.Replace(".wav", ".mp3") + "\"";
                    psi.FileName = @"D:\lame3.99.5\lame.exe";//"\"" + root_dir + "lame.exe" + "\"";
                    psi.Arguments = outfile;
                    psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
                    psi.UseShellExecute = false;// Minimized;
                    p = System.Diagnostics.Process.Start(psi);
                    if (waitFlag)
                    {
                        // Response.Write("Converted to " + pcopyDir + fileName.Replace(".wav", ".mp3") + "successfully");
                        //successmsg.Text = "Successfully convert WAV file to MP3 file.";
                        p.WaitForExit(10000);
                        p.Close();
                    }
                }
                catch (Exception ex)
                {
                    //         Response.Write(ex.Message);
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    p.Close();

                }
            }
        }

But it running fine but no files are getting made what should refrence link for lame exe is LAME MP3 Encoder[^]

What I have tried:

C#
public byte[] ConvertWavToMP3(byte[] bt, uint bitrate)
       {

           MemoryStream ms = new MemoryStream(bt);
           ms.Seek(0, SeekOrigin.Begin);
           var ws = new WaveFileReader(ms);

           byte[] wavdata = null;
           using (MemoryStream wavstrm = new MemoryStream())
           using (WaveFileWriter wavwri = new WaveFileWriter(wavstrm, ws.WaveFormat))
           {
               ws.CopyTo(wavwri);
               wavdata = wavstrm.ToArray();
           }

           WaveLib.WaveFormat fmt = new WaveLib.WaveFormat(ws.WaveFormat.SampleRate, ws.WaveFormat.BitsPerSample, ws.WaveFormat.Channels);

           Yeti.Lame.BE_CONFIG beconf = new Yeti.Lame.BE_CONFIG(fmt, bitrate);

           byte[] srm = null;

           using (MemoryStream mp3strm = new MemoryStream())
           using (Mp3Writer mp3wri = new Mp3Writer(mp3strm, fmt, beconf))
           {
               mp3wri.Write(wavdata, 0, wavdata.Length);
               byte[] mp3data = mp3strm.ToArray();
               return mp3data;
           }
       }

this and this
C#
public static Byte[] WavToMP3(byte[] wavFile)
      {

          using (MemoryStream source = new MemoryStream(wavFile))

          using (NAudio.Wave.WaveFileReader rdr = new NAudio.Wave.WaveFileReader(source))//using(RawSourceWaveStream rdr=new RawSourceWaveStream(source,_wavin.WaveFormat))
          {
              WaveLib.WaveFormat fmt = new WaveLib.WaveFormat(rdr.WaveFormat.SampleRate, rdr.WaveFormat.BitsPerSample, rdr.WaveFormat.Channels);

              // convert to MP3 at 96kbit/sec...
              Yeti.Lame.BE_CONFIG conf = new Yeti.Lame.BE_CONFIG(fmt, 96);

              // Allocate a 1-second buffer
              int blen = rdr.WaveFormat.AverageBytesPerSecond;
              byte[] buffer = new byte[blen];

              // Do conversion
              using (MemoryStream output = new MemoryStream())
              {
                  Yeti.MMedia.Mp3.Mp3Writer mp3 = new Yeti.MMedia.Mp3.Mp3Writer(output, fmt, conf);

                  int readCount;
                  while ((readCount = rdr.Read(buffer, 0, blen)) > 0)
                      mp3.Write(buffer, 0, readCount);

                  mp3.Close();
                  return output.ToArray();
              }
          }
      }


and this
C#
public void mp3towavecodec(string f1,string f2)
       {
           WaveStreamRT InStr = new WaveStreamRT(f1);
           try
           {
               Mp3Writer writer = new Mp3Writer(new FileStream(f2,
                                                   FileMode.Create), InStr.Format);
               try
               {
                   byte[] buff = new byte[writer.OptimalBufferSize];
                   int read = 0;
                   while ((read = InStr.Read(buff, 0, buff.Length)) > 0)
                   {
                       writer.Write(buff, 0, read);
                   }
               }
               finally
               {
                   writer.Close();
               }
           }
           finally
           {
               InStr.Close();
           }
       }


and this
C#
public static byte[] ConvertWavTo8000Hz16BitMonoWavs(byte[] inArray)
{
    using (var mem = new MemoryStream(inArray))
    {
        using (var reader = new WaveFileReader(mem))
        {
            using (var converter = WaveFormatConversionStream.CreatePcmStream(reader))
            {
                using (var upsampler = new WaveFormatConversionStream(new WaveFormat(8000, 16, 1), converter))
                {
                    byte[] data;
                    using (var m = new MemoryStream())
                    {
                        upsampler.CopyTo(m);
                        data = m.ToArray();
                    }
                    using (var m = new MemoryStream())
                    {
                        // to create a propper WAV header (44 bytes), which begins with RIFF
                        var w = new WaveFileWriter(m, upsampler.WaveFormat);
                        // append WAV data body
                        w.Write(data, 0, data.Length);
                        return m.ToArray();
                    }
                }
            }
        }
    }
}
Posted

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900