// Create an in-memory MP3 file with the supplied ID3v2 tag, then read the tag back from the MP3 file private static ID3TagData GetTagAsWritten(ID3TagData tag) { var waveFormat = new WaveFormat(); using (var ms = new MemoryStream()) { using (var writer = new LameMP3FileWriter(ms, waveFormat, LAMEPreset.STANDARD, tag)) { byte[] empty = new byte[8192]; writer.Write(empty, 0, 8192); writer.Flush(); } ms.Position = 0; return(ID3Decoder.Decode(ReadID3v2Tag(ms))); } }
/// <summary>Convert WAV file to MP3 using libmp3lame library, setting ID3 tag</summary> /// <param name="waveFileName">Filename of WAV file to convert</param> /// <param name="mp3FileName">Filename to save to</param> /// <param name="tag">ID3 tag data to insert into file</param> /// <param name="bitRate">MP3 bitrate in kbps, defaults to 128kbps</param> /// <remarks>Uses NAudio to read, can read any file compatible with <see cref="NAudio.Wave.AudioFileReader"/></remarks> public static void WaveToMP3(string waveFileName, string mp3FileName, ID3TagData tag, int bitRate = 128) { byte[] tagBytes = null; using (var reader = new AudioFileReader(waveFileName)) using (var writer = new LameMP3FileWriter(mp3FileName, reader.WaveFormat, 128, tag)) { reader.CopyTo(writer); tagBytes = writer.GetID3v2TagBytes(); } if (tagBytes != null) { var dectag = ID3Decoder.Decode(tagBytes); } }