Skip to content

Instantly share code, notes, and snippets.

@mikegoatly
Last active December 26, 2017 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikegoatly/59abec3415201433c76a9d0afdee63f7 to your computer and use it in GitHub Desktop.
Save mikegoatly/59abec3415201433c76a9d0afdee63f7 to your computer and use it in GitHub Desktop.
Merge MP3 files

MergeMp3.exe

A very simple program using the nAudio library to merge multiple MP3s into a single MP3.

Usage: MergeMP3.exe "Input file pattern" "Output filename"

e.g.

MergeMP3.exe "*Track 1*" "Track 1 Merged.mp3"

Known limitations:

  • The program works exclusively in the current working folder that it is launched in - attempting to provide a full path to the output/input files probably won't work. It's not hard to change though :)
namespace MergeMP3
{
#region Using statements
using System;
using System.IO;
using NAudio.Wave;
#endregion
public class Program
{
public static void Main(string[] args)
{
var dir = Directory.GetCurrentDirectory();
var inputs = Directory.GetFiles(dir, args[0]);
var output = Path.Combine(dir, args[1]);
if (File.Exists(output))
{
throw new Exception("File exists.");
}
using (var fileStream = File.OpenWrite(output))
{
Combine(inputs, fileStream);
fileStream.Flush();
}
}
public static void Combine(string[] inputFiles, Stream output)
{
foreach (var file in inputFiles)
{
var 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);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment