5

I am developing an app to capture video and download audio. I am able to save these files separately but could not find a way to combine these files to make a new video with audio.

Video file is in .mp4 format and audio is in .mp3 format. They have exact same length.

Is there any way to combine these files? I have tried but could not find a way to combine two files.

Also I can do it with FFMPEG but didn't get any best way how to implement this. Please help me out, I'm new in android.

Andrew Ryan
  • 51
  • 1
  • 4

1 Answers1

4

I think MP4Parser library is apt for this scenario. I'd found the below sample code to merge audio and video into single mp4 file in this link Video Recording And Processing In Android .

public class Mp4ParserAudioMuxer implements AudioMuxer {
    @Override
    public boolean mux(String videoFile, String audioFile, String outputFile) {
        Movie video;
        try {
            video = new MovieCreator().build(videoFile);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        Movie audio;
        try {
            audio = new MovieCreator().build(audioFile);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } catch (NullPointerException e) {
            e.printStackTrace();
            return false;
        }

        Track audioTrack = audio.getTracks().get(0);
        video.addTrack(audioTrack);

        Container out = new DefaultMp4Builder().build(video);

        FileOutputStream fos;

        try {
            fos = new FileOutputStream(outputFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        }

        BufferedWritableFileByteChannel byteBufferByteChannel = 
            new BufferedWritableFileByteChannel(fos);

        try {
            out.writeContainer(byteBufferByteChannel);
            byteBufferByteChannel.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }
}

If you are thinking of doing it via ffmpeg, you can use static ffmpeg binaries or you need to build it.

blizzard
  • 5,275
  • 2
  • 34
  • 48
  • I'm not getting this properly, how can I merge both file to create new mp4. One more thing, I think FFMPEG only work through NDK, or we can integrate this via SDK too in Eclipse? Please help me as I'm just a fresher in Android Development. – Andrew Ryan Jan 28 '15 at 06:52
  • @AndrewRyan Had you tried the code?. Regarding FFMpeg we can integrate via SDK too. Check out this link http://stackoverflow.com/questions/9605757/using-ffmpeg-with-android-ndk/9681231#9681231 – blizzard Jan 28 '15 at 10:13
  • 2
    yes I've tried all that stuff, I save file from camera as .mp4 now I've two files 1 is .mp4 and another 1 .mp3, now what else I need to do for creating new mp4? even I've used that above code but not able to convert or create a new mp4 file. – Andrew Ryan Jan 29 '15 at 07:51
  • @AndrewRyan :- Have you got Result?, actually i want to create new mp4 file , when merge 1 is mp4 and another 1 .mp3 .. – Mayank Sugandhi Mar 16 '16 at 07:52
  • @b1izzard in this code i got Exception like this.. :-"java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.coremedia.iso.boxes.MovieBox.getBoxes(java.lang.Class)' on a null object reference –" how can i Resolve? – Mayank Sugandhi Mar 16 '16 at 07:53
  • @AndrewRyan :- in this code i got Exception like this.. :-"java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.coremedia.iso.boxes.MovieBox.getBoxes(java.lang.Class)' on a null object reference –" how can i Resolve? – Mayank Sugandhi Mar 16 '16 at 07:54