Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?:(

like image 540
Peter Avatar asked Jul 18 '11 09:07

Peter


2 Answers

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();
    }
}
like image 167
shiva Avatar answered Oct 02 '22 14:10

shiva


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.

like image 31
Oak Bytes Avatar answered Oct 02 '22 15:10

Oak Bytes