如何使用内存流、NAudio 和 LameMP3 将音频 aiff 转换为 MP3
How convert audio aiff to MP3 using Memory stream, NAudio and LameMP3
我尝试将 aiff 音频文件从 IFormFile 转换为 MP3 并将其上传到 Azure 存储。我不明白内存流如何与 NAudio 一起工作...我不想将文件复制到临时文件中,而只想使用流。可能吗?
using (var retMs = new MemoryStream())
{
file.CopyTo(retMs);
var fileBytes = retMs.ToArray();
string s = Convert.ToBase64String(fileBytes);
using (var ms = new MemoryStream(fileBytes))
using (var reader = new AiffFileReader(ms))
using (var writer = new LameMP3FileWriter(retMs, reader.WaveFormat, 128))
{
reader.CopyTo(writer);
var result = await _blobService.UploadFileBlobAsync(
reader,
"audio/mpeg3",
"fr2.mp3");
toReturn = result.AbsoluteUri;
}
}
我认为下面的演示代码可能对您有所帮助。
using Azure.Storage.Blobs;
using NAudio.Lame;
using NAudio.Wave;
using System;
using System.IO;
namespace blobUpload
{
class Program
{
static void Main(string[] args)
{
string containerName = "<container name>";
string blobName = "test.mp3";
string storageConnStr = "<storage account conn str>";
var blobServiceClient = new BlobServiceClient(storageConnStr);
var blobClient = blobServiceClient.GetBlobContainerClient(containerName).GetBlobClient(blobName);
using (var retMs = new MemoryStream())
{
FileStream inputfileStream = new FileStream(@"<aiff file path>", FileMode.Open, FileAccess.Read);
inputfileStream.CopyTo(retMs);
var fileBytes = retMs.ToArray();
var bytes = ConvertWavToMp3(fileBytes);
blobClient.UploadAsync(new MemoryStream(bytes)).GetAwaiter().GetResult();
}
}
public static byte[] ConvertWavToMp3(byte[] wavFile)
{
using (var retMs = new MemoryStream())
using (var ms = new MemoryStream(wavFile))
using (var rdr = new AiffFileReader(ms))
using (var wtr = new LameMP3FileWriter(retMs, rdr.WaveFormat, 128))
{
rdr.CopyTo(wtr);
return retMs.ToArray();
}
}
}
结果:
我这边测试过,这个.MP3文件可以完美播放
我尝试将 aiff 音频文件从 IFormFile 转换为 MP3 并将其上传到 Azure 存储。我不明白内存流如何与 NAudio 一起工作...我不想将文件复制到临时文件中,而只想使用流。可能吗?
using (var retMs = new MemoryStream())
{
file.CopyTo(retMs);
var fileBytes = retMs.ToArray();
string s = Convert.ToBase64String(fileBytes);
using (var ms = new MemoryStream(fileBytes))
using (var reader = new AiffFileReader(ms))
using (var writer = new LameMP3FileWriter(retMs, reader.WaveFormat, 128))
{
reader.CopyTo(writer);
var result = await _blobService.UploadFileBlobAsync(
reader,
"audio/mpeg3",
"fr2.mp3");
toReturn = result.AbsoluteUri;
}
}
我认为下面的演示代码可能对您有所帮助。
using Azure.Storage.Blobs;
using NAudio.Lame;
using NAudio.Wave;
using System;
using System.IO;
namespace blobUpload
{
class Program
{
static void Main(string[] args)
{
string containerName = "<container name>";
string blobName = "test.mp3";
string storageConnStr = "<storage account conn str>";
var blobServiceClient = new BlobServiceClient(storageConnStr);
var blobClient = blobServiceClient.GetBlobContainerClient(containerName).GetBlobClient(blobName);
using (var retMs = new MemoryStream())
{
FileStream inputfileStream = new FileStream(@"<aiff file path>", FileMode.Open, FileAccess.Read);
inputfileStream.CopyTo(retMs);
var fileBytes = retMs.ToArray();
var bytes = ConvertWavToMp3(fileBytes);
blobClient.UploadAsync(new MemoryStream(bytes)).GetAwaiter().GetResult();
}
}
public static byte[] ConvertWavToMp3(byte[] wavFile)
{
using (var retMs = new MemoryStream())
using (var ms = new MemoryStream(wavFile))
using (var rdr = new AiffFileReader(ms))
using (var wtr = new LameMP3FileWriter(retMs, rdr.WaveFormat, 128))
{
rdr.CopyTo(wtr);
return retMs.ToArray();
}
}
}
结果:
我这边测试过,这个.MP3文件可以完美播放