How to convert wav file to mp3 in memory?

烂漫一生 提交于 2021-02-07 09:12:49

问题


I have an application written using c# on the top of Asp.Net MVC 5 framework.

My objective is to make a call to 3rd party service to download a wave file. Then I want to convert this file into mp3. Finally, I want to return the mp3 file as (byte[]) to allow the user to download it directly from memory.

Here is how my code will end up which allow the user to download the converted file from the memory

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"); 
}

But I am having issue with the ConvertToMp3 method.

I am using NAudio library to convert the file conversion. Here is my code so far

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);
    }
}

The above method converts the file. But this method writes the wave file to desk, converts it to mp3 then delete the wave file. Additionally this method writes the mp3 file to the desk instead of returning byte[] content.

How can I convert the file in memory without having to write to desk? Also, how can I convert the mp3 file to byte[]?


回答1:


Maybe something like this.

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

Sync

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();
      }
   }
}

Async

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();
      }
   }
}


来源:https://stackoverflow.com/questions/50614636/how-to-convert-wav-file-to-mp3-in-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!