1

Please note This is NOT a duplicate of the other question linked. That uses classes I couldn't find, as detailed in my question below.

I'm trying to convert wma files to mp3. I need a solution that I can integrate into my code base, not rely on an external resource, so using ffmpeg isn't an option. I've been trying NAudio, but without any success.

One problem is that there seem to be two versions of NAudio around, and neither seems complete. The one you get from Nuget doesn't include the WMAFileReader class, so there's no way (that i can see) to read wma files. The version on github includes the WMAFileReader class, but doesn't seem to include the Mp3Writer class, nor the WaveLib class I've seen in many examples.

So, anyone know how I can get something that will do the job? I've wasted hours trying different code samples, but none of them seem to work with either version of NAudio I can find.

Ideally, I would like to do this in memory, but if I have to write to temporary disk files, it's not the end of the world.

Edit I just discovered that there are more NAudio nuget packages that extend the basic one. There is one for Lame and one for WMA, but even after installing them, I can't get any code to work.

Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106
  • possible duplicate of [wma audio stream to mp3 stream using NAudio c#](http://stackoverflow.com/questions/19421460/wma-audio-stream-to-mp3-stream-using-naudio-c-sharp) – Ňɏssa Pøngjǣrdenlarp Jul 05 '15 at 16:02
  • No, not a duplicate. I already looked at that page, but as I mentioned, it uses classes that I can't find. If you can tell me where to find all the classes in the code there, then maybe we could mark this as a duplicate (although it still wouldn't be, as that page doesn't give any clues). Otherwise, my question stil lstands. – Avrohom Yisroel Jul 05 '15 at 16:05
  • convert from wma to wav and then wav to mp3 – M.kazem Akhgary Jul 05 '15 at 17:26
  • @M.kazemAkhgary thx for the suggestion, but how would I do that? I still have the same basic problem that I don't have the right classes to handle wma and mp3 files in the same project. Even if I go via wav, I would still need both sets of classes. – Avrohom Yisroel Jul 05 '15 at 17:38
  • First of all i give you a reason why not directly converting wav to mp3. its possible and its even faster but usually converters Do not Convert files directly. Because there are bunch of audio formats. you need bunch of algorithms to directly Convert from each format to another. so it becomes real hard to find an algorithm to directly convert from wma to mp3. (but i dont say there is nothing). Its Not possible to convert with Naudio since it does not support wma to wav neither wav to mp3.But i thing reading this article will help you. Sorry for late comment because i was sleeping. – M.kazem Akhgary Jul 05 '15 at 22:03
  • [How to convert between (most) audio formats in .NET](http://www.codeproject.com/Articles/501521/How-to-convert-between-most-audio-formats-in-NET) – M.kazem Akhgary Jul 05 '15 at 22:04
  • @M.kazemAkhgary Thanks for the link. I've seen that article, but don't really understand it. It's way beyond my level of knowledge of this stuff, and I don't have the time to learn it all. That's why I was hoping to find a package like NAudio that would wrap up the dirty details for me. – Avrohom Yisroel Jul 06 '15 at 12:55

1 Answers1

6

Seems simple enough...

Create a new console project, use nuget to add the NAudio.Lame package (which I created to encapsulate the LAME MP3 DLLs). I'm using the package direct from nuget myself in this example.

Add the following method somewhere:

static void ConvertToMP3(string sourceFilename, string targetFilename)
{
    using (var reader = new NAudio.Wave.AudioFileReader(sourceFilename))
    using (var writer = new NAudio.Lame.LameMP3FileWriter(targetFilename, reader.WaveFormat, NAudio.Lame.LAMEPreset.STANDARD))
    {
        reader.CopyTo(writer);
    }
}

Call that with the filename of your WMA file (or any other audio file readable by the AudioFileReader class) and the filename you want to save to and let it run:

static void Main(string[] args)
{
    ConvertToMP3(@"C:\temp\test.wma", @"C:\temp\test_transcode.mp3");
}

Where this might run into problems is when your input file is in a format that the MP3 encoder doesn't support. Weird channel counts, sample formats or sample rates can all cause the MP3 writer to fail. My test file was 44.1KHz Mono IEEE-float format when decoded, which the Lame codec is quite happy to work with. If you find one where it doesn't work then you'll have to do some sample conversion to get your input data into a compatible format.

Also you might want to play around with the quality parameter in the LameMP3FileWriter constructor. There are a variety of presets (as defined in the LAME encoder itself) or you can try a direct specification of kilobits per second if you prefer. NAudio.Lame.LAMEPreset.STANDARD produces a 128Kbps file.

Corey
  • 15,524
  • 2
  • 35
  • 68
  • Thanks for the reply, it looks so simple. Problem is, the MameMP3Writer is not recognised. I started a new VS2013 solution, added the NAudio.Lame Nuget package (which also added NAudio) and copied your ConvertToMP3 method, but the LameMP3Writer was highlighted in red, and marked with "Cannot resolve symbol". Any ideas? Looking at packages.config, it looks like I have version 1.7 of NAudio and 1.0.2 of NAudio.Lame. Also, what does the NAudio.Wma package add? Thanks again. – Avrohom Yisroel Jul 06 '15 at 12:51
  • Did you use the full type name (including namespace)? You've got the current version of both `NAudio` and `NAudio.Lame`, same as I used in test earlier. Try adding `using NAudio.Lame;` to your uses section and see if that helps. – Corey Jul 06 '15 at 13:19
  • I copied your code exactly (copy and paste), which included the full namespaces. Resharper automatically added usings for NAudio.Lame and NAudio.Wave. Any ideas? I can send you my sample project if you like so you can see what's gone wrong. Or if you prefer, you could send me one (mryossu@hotmail.com). Seems odd if we did the same thing. Thx again – Avrohom Yisroel Jul 06 '15 at 14:31
  • @AvrohomYisroel Sorry, I'm an idiot. I typed instead of copying, then didn't check back. The class is actually named `LameMP3FileWriter` – Corey Jul 06 '15 at 14:36
  • Yup, I just spotted that and was about to post a comment! Thanks very much again. So what's the NAudio.Wma package for if the NAudio package handles wma files? – Avrohom Yisroel Jul 06 '15 at 14:40
  • Sorry for that. As for `NAudio.Wma`... it adds a `WmaFileReader` class and supporting code to read and write `Wma` files. I haven't used it, since most of what I want to do is simply read the files, and `AudioFileReader` does that quite well. Mark is the one to ask about that stuff, I was only involved with `NAudio.Lame` – Corey Jul 06 '15 at 14:54
  • Thanks for the clarification. As I'm also only interested in reading wma files, I'll stick with what you've shown. I've marked your answer as accepted. Thanks again. – Avrohom Yisroel Jul 06 '15 at 15:20