I am trying to convert wav file into mp3 using following code.
using (var wavRdr = new WaveFileReader(item))
using (var mp3Writer = new LameMP3FileWriter(outputPath.Replace(".wav", ".mp3"), wavRdr.WaveFormat, _bitRate, tag))
{
wavRdr.CopyTo(mp3Writer);
}
LameMP3FileWriter does not have any parameter to set Channels and Sample Rate for MP3 format.
After some research, i have got an idea that i have to set the channel and sample rate before calling the conversion method but i do not know how to do this.
I used this for conversion which i have done successfully but now i need to set channels and sample rate for mp3 file. Please help!
Here i have solved the problem.
var tempFile = Path.Combine(destFolder, Path.GetFileNameWithoutExtension(sourceFile.SourceFilePath) + ".wav");
try
{
var reader = new WaveFileReader(sourceFile.SourceFilePath);
var newFormat = new WaveFormat(SampleRate, BitsPerSec, Channels);
var conversionStream = new WaveFormatConversionStream(newFormat, reader);
WaveFileWriter.CreateWaveFile(tempFile, conversionStream);
var destFile = Path.Combine(destFolder, Path.GetFileNameWithoutExtension(sourceFile.DestiFileName) + ".mp3");
using (var wavRdr = new WaveFileReader(tempFile))
{
using (var mp3Writer = new LameMP3FileWriter(destFile, newFormat, BitRate, sourceFile))
{
wavRdr.CopyTo(mp3Writer);
}
}
}
catch
{
//Ignore
}
finally
{
if (File.Exists(tempFile))
File.Delete(tempFile);
}