如何将wav文件转换为内存中的mp3? [英] How to convert wav file to mp3 in memory?

查看:133
本文介绍了如何将wav文件转换为内存中的mp3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的目标是致电3rd party服务以下载wave文件.然后,我想将此文件转换为mp3.最后,我想将mp3文件返回为(byte []),以允许用户直接从内存中下载它.

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

但是我在 ConvertToMp3 方法上遇到问题.

But I am having issue with the ConvertToMp3 method.

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

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

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

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.

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

How can I convert the file in memory without having to write to desk? Also, how can I convert the mp3 file to 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();
      }
   }
}

这篇关于如何将wav文件转换为内存中的mp3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆