How to Merge Two Mp3 Files into One (Combine/Join)

How to merge two mp3 files into one (combine/join)

Consider two cases for .mp3 files:

  • Files with same sampling frequency and number of channels

In this case, we can just append the second file to end of first file. This can be achieved using File classes available on Android.

  • Files with different sampling frequency or number of channels.

In this case, one of the clips has to be re-encoded to ensure both files have same sampling frequency and number of channels. To do this, we would need to decode MP3, get PCM samples,process it to change sampling frequency and then re-encode to MP3. From what I know, android does not have transcode or reencode APIs. One option is to use external library like lame/FFMPEG via JNI for re-encode.

What is the best way to merge mp3 files?

As Thomas Owens pointed out, simply concatenating the files will leave multiple ID3 headers scattered throughout the resulting concatenated file - so the time/bitrate info will be wildly wrong.

You're going to need to use a tool which can combine the audio data for you.

mp3wrap would be ideal for this - it's designed to join together MP3 files, without needing to decode + re-encode the data (which would result in a loss of audio quality) and will also deal with the ID3 tags intelligently.

The resulting file can also be split back into its component parts using the mp3splt tool - mp3wrap adds information to the IDv3 comment to allow this.

How do I merge/join MP3 files with C#?

MP3 files consist of "frames", that each represent a short snippet (I think around 25 ms) of audio.

So yes, you can just concatenate them without a problem.

C# : concatenate 2 MP3 files

I am willing to bet you are only hearing the second song. (and that either both files are the same length or the first is shorter)

You are copying the second song data over the first. And MP3 data is streaming so you can just append the files to each other without worrying about bitrates (while they may glitch) the bitrate should automaticly adjust.

Detail on MP3 Frame headers

... try this...

Array.Copy(files[0], 0, a, 0, files[0].Length);
Array.Copy(files[1], 0, a, files[0].Length, files[1].Length);

... or better still...

using (var fs = File.OpenWrite(Path.Combine(path, "3.mp3")))
{
var buffer = File.ReadAllBytes(Path.Combine(path, "1.mp3"));
fs.Write(buffer, 0, buffer.Length);
buffer = File.ReadAllBytes(Path.Combine(path, "2.mp3"));
fs.Write(buffer, 0, buffer.Length);
fs.Flush();
}

Combine two audio files into one in objective c

Look at AVFoundation framework ... There're many ways, but the simplest one for you can be ...

  • create AVAsset for both files (use AVURLAsset subclass),
  • alloc AVMutableComposition (composition),
  • add AVMutableCompositionTrack with type AVMediaTypeAudio to composition

[composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

  • get track(s) from the first AVAsset and add it AVMutableCompositionTrack,
  • get track(s) from the second AVAsset and append it to AVMutableCompositionTrack,
  • then create AVAssetExportSession with your composition and export it.

Simplified description, but you get a clue. Depends on how many tracks do you have, what kind of effects do you want to use, etc.

If you do want to see some real code, open AVMovieExporter example, copy this code and remove video stuff and leave audio there only.



Related Topics



Leave a reply



Submit