如何将wav文件转换为内存中的mp3?

How to convert wav file to mp3 in memory?

我有一个在 Asp.Net MVC 5 框架之上使用 c# 编写的应用程序。

我的objective是调用第三方服务下载一个wave文件。然后我想把这个文件转换成mp3。最后,我想return mp3 文件为(byte[]) 以允许用户直接从内存中下载它。

这是我的代码的最终结果,它允许用户从内存中下载转换后的文件

public ActionResult Download(int? id)
{

    // Download and convert the file
    // the variable "someUri" is generated based on the provided id value
    byte[] filedata = ConvertToMp3(someUri);

    var cd = new System.Net.Mime.ContentDisposition
    {
        FileName = "filename.mp3",
        Inline = true,
    };

    Response.AppendHeader("Content-Disposition", cd.ToString());

    return File(filedata, "audio/mpeg"); 
}

但我对 ConvertToMp3 方法有疑问。

我正在使用NAudio库来转换文件转换。到目前为止,这是我的代码

public void ConvertToMp3(Uri uri)
{
    using (var client = new WebClient())
    {
        byte[] file = client.DownloadData(uri);
        string wavFileFullName = GetFullDirectory() + "filename.wav";
        string mp3FileFullName = GetFullDirectory() + "filename.mp3";
        WaveFormat target = new WaveFormat(8000, 16, 1);
        using (WaveStream stream = new WaveFileReader(new MemoryStream(file)))
        using (WaveFormatConversionStream str = new WaveFormatConversionStream(target, stream))
        {
            WaveFileWriter.CreateWaveFile(wavFileFullName, str);

            using (var reader = new WaveFileReader(wavFileFullName))
            using (var writer = new LameMP3FileWriter(mp3FileFullName, reader.WaveFormat, bitRate))
            {
                reader.CopyTo(writer);
            }
        }
        File.Delete(wavFileFullName);
    }
}

以上方法转换文件。但是这种方法是将wave文件写入桌面,将其转换为mp3,然后删除wave文件。此外,此方法将 mp3 文件写入桌面,而不是 returning byte[] 内容。

如何在不写入桌面的情况下转换内存中的文件?另外,如何将 mp3 文件转换为 byte[]

也许是这样的。

Note : Totally untested, i just merely looked at the source.

同步

public byte[] ConvertToMp3(Uri uri)
{
   using (var client = new WebClient())
   {
      var file = client.DownloadData(uri);
      var target = new WaveFormat(8000, 16, 1);
      using (var outPutStream = new MemoryStream())
      using (var waveStream = new WaveFileReader(new MemoryStream(file)))
      using (var conversionStream = new WaveFormatConversionStream(target, waveStream))
      using (var writer = new LameMP3FileWriter(outPutStream, conversionStream.WaveFormat, 32, null))
      {
         conversionStream.CopyTo(writer);

         return outPutStream.ToArray();
      }
   }
}

异步

public async Task<byte[]> ConvertToMp3Async(Uri uri)
{
   using (var client = new WebClient())
   {
      var file = await client.DownloadDataTaskAsync(uri);
      var target = new WaveFormat(8000, 16, 1);
      using (var outPutStream = new MemoryStream())
      using (var waveStream = new WaveFileReader(new MemoryStream(file)))
      using (var conversionStream = new WaveFormatConversionStream(target, waveStream))
      using (var writer = new LameMP3FileWriter(outPutStream, conversionStream.WaveFormat, 32, null))
      {
          await conversionStream.CopyToAsync(writer);

          return outPutStream.ToArray();
      }
   }
}