Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gautamdhameja
Created December 3, 2017 20:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gautamdhameja/e71317748e446bd85bb0875a0e64d6d5 to your computer and use it in GitHub Desktop.
Save gautamdhameja/e71317748e446bd85bb0875a0e64d6d5 to your computer and use it in GitHub Desktop.
Merge two or more mp3 files in C# using naudio library
/// <summary>
/// Creates a mashup of two or more mp3 files by using naudio
/// </summary>
/// <param name="files">Name of files as an string array</param>
/// These files should be existing in a temporay folder
/// <returns>The path of mashed up mp3 file</returns>
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["OutputDirName"],
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["InputDirName"], file + ".mp3");
if (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;
}
}
@Mariohe
Copy link

Mariohe commented Dec 31, 2019

What a pity that I have not used naudio library. But I usually merge videos or audios using Joyoshare Video Joiner. Because it supports various media formats and merging videos/audios in the same format losslessly and seamlessly. What's more, it can allow me to cut and edit my files, which satisfies me well.

@ShiliSaghi
Copy link

I don't have any idea why I couldn't install NAudio package on my C# project. I faced with this error:
"Could not install package 'NAudio.Core 2.0.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.0', but the package does not contain any assembly references or content files that are compatible with that framework..."
Do you know what is the proper version of .NetFramework for this package?
Thank you in advance.

@gautamdhameja
Copy link
Author

gautamdhameja commented Dec 13, 2021

@ShiliSaghi When I created this app, the version I used for .Net was 4.6.1. See here - https://github.com/gautamdhameja/mtf-mashup/blob/master/src/mashup/mtf-mashup.api/mtf-mashup.api.csproj#L18
The NAudio version I was using was 1.8.3 - https://github.com/gautamdhameja/mtf-mashup/blob/master/src/mashup/mtf-mashup.api/mtf-mashup.api.csproj#L95

Also, here if the full code available for reference - https://github.com/gautamdhameja/mtf-mashup/tree/master/src/mashup
Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment