c# - How to convert from wma to mp3 using NAudio -
please note not duplicate of other question linked. uses classes couldn't find, detailed in question below.
i'm trying convert wma files mp3. need solution can integrate code base, not rely on external resource, using ffmpeg isn't option. i've been trying naudio, without success.
one problem there seem 2 versions of naudio around, , neither seems complete. 1 nuget doesn't include wmafilereader class, there's no way (that can see) read wma files. version on github includes wmafilereader class, doesn't seem include mp3writer class, nor wavelib class i've seen in many examples.
so, know how can job? i've wasted hours trying different code samples, none of them seem work either version of naudio can find.
ideally, in memory, if have write temporary disk files, it's not end of world.
edit discovered there more naudio nuget packages extend basic one. there 1 lame , 1 wma, after installing them, can't code work.
seems simple enough...
create new console project, use nuget
add naudio.lame
package (which created encapsulate lame
mp3 dlls). i'm using package direct nuget
myself in example.
add 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 filename of wma file (or other audio file readable audiofilereader
class) , filename want save , let run:
static void main(string[] args) { converttomp3(@"c:\temp\test.wma", @"c:\temp\test_transcode.mp3"); }
where might run problems when input file in format mp3 encoder doesn't support. weird channel counts, sample formats or sample rates can cause mp3 writer fail. test file 44.1khz mono ieee-float format when decoded, lame codec quite happy work with. if find 1 doesn't work you'll have sample conversion input data compatible format.
also might want play around quality parameter in lamemp3filewriter
constructor. there variety of presets (as defined in lame encoder itself) or can try direct specification of kilobits per second if prefer. naudio.lame.lamepreset.standard
produces 128kbps file.
Comments
Post a Comment