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

2

12

Can any tell how to combine/merge two media files into one ?

i found a topics about audioInputStream but now it's not supported in android, and all code for java .

And on StackOverflow i found this link here but there i can't find solution - these links only on streaming audio . Any one can tell me ?

P.S and why i can't start bounty ?:(

Disciplinant answered 18/7, 2011 at 9:52 Comment(2)
For to p.s: the question is not yet 2 days old; read more here: meta.stackexchange.com/questions/54994/…Pekingese
Hi, i want two merge two mp3 audio files into one file.If u know how to merge help me.thanks in advanceWaisted
M
5

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.

Maloney answered 18/7, 2011 at 10:8 Comment(9)
no, it's not help me :( i think i will be waiting 2 days, and than start a bounty.. maybe than people can helpDisciplinant
@Peter, can you explain which media file formats you want to merge?Maloney
mp3 . i don't need append one file to other. need two sounds (for example : firts file : 1 minute , second 35 seconds ) combine to one :|Disciplinant
@Disciplinant from what I understand, you want to merge portion of a mp3 files and not complete files?Maloney
and "complite files" (look like video and soundtrack in one file )Disciplinant
from what i understand, if one file is 1 minute and other file is 35 seconds, the result should be 1 minute long containing a merge of 2 sound filesPang
yeye, one minute but containing 2 files . you are right understandDisciplinant
@Peter, What you are looking for is called audio mixing. Unfortunately there is no standard way. You have to use external libraries like FFMPEG or play two audio simultaneously ( See https://mcmap.net/q/537368/-mixing-audio-files)Maloney
@OakBytes please add example for android.Outlay
B
8
import java.io.*;
public class TwoFiles
{
    public static void main(String args[]) throws IOException
    {
        FileInputStream fistream1 = new FileInputStream("C:\\Temp\\1.mp3");  // first source file
        FileInputStream fistream2 = new FileInputStream("C:\\Temp\\2.mp3");//second source file
        SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
        FileOutputStream fostream = new FileOutputStream("C:\\Temp\\final.mp3");//destinationfile

        int temp;

        while( ( temp = sistream.read() ) != -1)
        {
            // System.out.print( (char) temp ); // to print at DOS prompt
            fostream.write(temp);   // to write to file
        }
        fostream.close();
        sistream.close();
        fistream1.close();
        fistream2.close();
    }
}
Bedmate answered 26/7, 2012 at 8:10 Comment(5)
is it append audio with one another or is it mixing the audio ?Gouache
@ArslanAhmad this is for appending audio. The SequenceInputStream is the core part of this code. A SequenceInputStream concatenates the two FileInputStreams as mentioned in the docs. This code will not mix, rather it will join.Hoseahoseia
@Bedmate i have use same code but not working for me here is my question link help me. #35340525Pulsar
I have tried this code so it's work but with exception of that the length of final.mp3 file is same as the first one so the audio player could play the second audio. i would like to ask how can i see the total length of the audio.Farouche
It increase only size,,, but when i open it, only first audio is playing.Huynh
M
5

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.

Maloney answered 18/7, 2011 at 10:8 Comment(9)
no, it's not help me :( i think i will be waiting 2 days, and than start a bounty.. maybe than people can helpDisciplinant
@Peter, can you explain which media file formats you want to merge?Maloney
mp3 . i don't need append one file to other. need two sounds (for example : firts file : 1 minute , second 35 seconds ) combine to one :|Disciplinant
@Disciplinant from what I understand, you want to merge portion of a mp3 files and not complete files?Maloney
and "complite files" (look like video and soundtrack in one file )Disciplinant
from what i understand, if one file is 1 minute and other file is 35 seconds, the result should be 1 minute long containing a merge of 2 sound filesPang
yeye, one minute but containing 2 files . you are right understandDisciplinant
@Peter, What you are looking for is called audio mixing. Unfortunately there is no standard way. You have to use external libraries like FFMPEG or play two audio simultaneously ( See https://mcmap.net/q/537368/-mixing-audio-files)Maloney
@OakBytes please add example for android.Outlay

© 2022 - 2024 — McMap. All rights reserved.