public static bool ConvertM4AToMp3(byte[] m4AFile, string directory, ref Track song) //requires windows 8 or higher { var tempFile = Path.Combine(directory, "tempdata" + _uniqueTempFileCounter + ".m4a"); // try { _uniqueTempFileCounter += 1; File.WriteAllBytes(tempFile, m4AFile); song.LocalPath += ".mp3"; //conversion wil result in an mp3 using (var reader = new MediaFoundationReader(tempFile)) //this reader supports: MP3, AAC and WAV { var aaCtype = AudioSubtypes.MFAudioFormat_AAC; var bitrates = MediaFoundationEncoder.GetEncodeBitrates(aaCtype, reader.WaveFormat.SampleRate, reader.WaveFormat.Channels); MediaFoundationEncoder.EncodeToMp3(reader, song.LocalPath, bitrates[bitrates.GetUpperBound(0)]); } File.Delete(tempFile); return(true); } catch (Exception) { if (File.Exists(tempFile)) { File.Delete(tempFile); } return(false); } }
public static void ConvertM4AToMp3(Stream m4AStream, string directory, ref Track song) //requires windows 8 or higher { using (var reader = new StreamMediaFoundationReader(m4AStream)) //this reader supports: MP3, AAC and WAV { var aaCtype = AudioSubtypes.MFAudioFormat_AAC; var bitrates = MediaFoundationEncoder.GetEncodeBitrates(aaCtype, reader.WaveFormat.SampleRate, reader.WaveFormat.Channels); song.LocalPath += ".mp3"; //conversion wil result in an mp3 MediaFoundationEncoder.EncodeToMp3(reader, song.LocalPath, bitrates[bitrates.GetUpperBound(0)]); } }