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

Hussain
  • 5,552
  • 4
  • 40
  • 50
Peter
  • 2,480
  • 6
  • 39
  • 60
  • 1
    For to p.s: the question is not yet 2 days old; read more here: http://meta.stackexchange.com/questions/54994/what-would-prevent-me-from-adding-a-bounty-to-a-question/54996#54996 – Adinia Jul 18 '11 at 10:16
  • 1
    Hi, i want two merge two mp3 audio files into one file.If u know how to merge help me.thanks in advance – sandeep Mar 26 '13 at 07:04

2 Answers2

9
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();
    }
}
Tisho
  • 8,320
  • 6
  • 44
  • 52
shiva
  • 99
  • 1
  • 2
  • is it append audio with one another or is it mixing the audio ? – Ahmad Arslan Jul 09 '14 at 06:49
  • @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. – Advait Saravade Oct 14 '15 at 08:18
  • @shiva i have use same code but not working for me here is my question link help me. http://stackoverflow.com/questions/35340025/how-to-merge-two-or-more-mp3-audio-file-in-android – Nisar Ahmad Feb 12 '16 at 07:06
  • 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. – kamboj Dec 31 '16 at 21:44
  • 2
    It increase only size,,, but when i open it, only first audio is playing. – SRB Bans Sep 25 '17 at 15:06
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.

Manish Dubey
  • 4,206
  • 8
  • 36
  • 65
Oak Bytes
  • 4,649
  • 4
  • 36
  • 53
  • no, it's not help me :( i think i will be waiting 2 days, and than start a bounty.. maybe than people can help – Peter Jul 18 '11 at 13:20
  • @Peter, can you explain which media file formats you want to merge? – Oak Bytes Jul 18 '11 at 14:56
  • 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 :| – Peter Jul 19 '11 at 07:41
  • @Peter from what I understand, you want to merge portion of a mp3 files and not complete files? – Oak Bytes Jul 19 '11 at 08:54
  • and "complite files" (look like video and soundtrack in one file ) – Peter Jul 19 '11 at 09:57
  • 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 files – Alex K Jul 19 '11 at 09:58
  • yeye, one minute but containing 2 files . you are right understand – Peter Jul 19 '11 at 11:38
  • @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 http://stackoverflow.com/questions/2191408/mixing-audio-files) – Oak Bytes Jul 19 '11 at 14:54
  • @OakBytes please add example for android. – Fortran Oct 31 '17 at 13:45