Inquiridor
Juntar áudios com em ASP.NET Core

Discussão Geral
-
Crei um metódo "Combine" para juntar três arquivos mp3, onde ficaram duas dúvidas:
-
O que tem de errado com método que ele não une os arquivos;
-
Como posso realizar o download o arquivo resultante dessa junção (que por enquanto estão na variável "output").
public static void Combine(string[] inputFiles, Stream output) { foreach (string file in inputFiles) { Mp3FileReader reader = new Mp3FileReader(file); if ((output.Position == 0) && (reader.Id3v2Tag != null)) { output.Write(reader.Id3v2Tag.RawData, 0, reader.Id3v2Tag.RawData.Length); } Mp3Frame frame; while ((frame = reader.ReadNextFrame()) != null) { output.Write(frame.RawData, 0, frame.RawData.Length); } } }
- Tipo Alterado welington jrModerator terça-feira, 4 de agosto de 2020 13:39 discursao
-
Todas as Respostas
-
Ola, encontrei alguns artigos que podem te ajudar com a resolucao do seu problema:
https://www.gautamdhameja.com/merge-mp3-files-programmatically-in-csharp-eb95d8c3c59f/
Eu tambem recomendo usar "BASS Audio Library"
Esta biblioteca tem diferentes plugins como "BASSmix" e "BASS FX" que ajudam em algumas funcionalidades que voce esta tentando alcancar.
-
Muito obrigado pela atenção, estou com um pouco de dificuldade na forma de chama-la não sei a forma correta de passar os arquivos e receber o retorno. Como estou fazendo:
string[] files = { @"1.wav", @"2.wav", @"3.wav" }; string resultfile = CreateMashup(files); using (FileStream fileStream = new FileStream(resultfile, FileMode.Create, FileAccess.Write, FileShare.Write)) { fileStream.Close(); }
Método que concatena:
public static string CreateMashup(string[] files) { // because there is no mash up with less than 2 files if (files.Count() < 2) { throw new Exception("Not enough files selected!"); } try { // Create a mixer object // This will be used for merging files together var mixer = new WaveMixerStream32 { AutoStop = true }; // Set the path to store the mashed up output file var outputFile = Path.Combine(Path.GetTempPath(), ConfigurationManager.AppSettings["AudienceDirName"], Guid.NewGuid().ToString() + ".mp3"); foreach (var file in files) { // for each file - // check if it exists in the temp folder var filePath = Path.Combine(Path.GetTempPath(), ConfigurationManager.AppSettings["PerfLabDirName"], file + ".mp3"); if (System.IO.File.Exists(filePath)) { // create mp3 reader object var reader = new Mp3FileReader(filePath); // create a wave stream and a channel object var waveStream = WaveFormatConversionStream.CreatePcmStream(reader); var channel = new WaveChannel32(waveStream) { //Set the volume Volume = 0.5f }; // add channel as an input stream to the mixer mixer.AddInputStream(channel); } } // convert wave stream from mixer to mp3 var wave32 = new Wave32To16Stream(mixer); var mp3Writer = new LameMP3FileWriter(outputFile, wave32.WaveFormat, 128); wave32.CopyTo(mp3Writer); // close all streams wave32.Close(); mp3Writer.Close(); // return the mashed up file path return outputFile; } catch (Exception) { // TODO: handle exception throw; } }