Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - combining two MP4 files

I'm writing an Android project where I'm recording several audio files. Therefore, I'm setting the following parameters. The recording works fine.

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

My problem is that each time I record, the output is written in a separate file. Now, I need to combine these files to one file. Does anyone have an idea how to combine several MPEG 4 files in an Android project?

Thanks for your help....

like image 422
user1463683 Avatar asked Jun 18 '12 12:06

user1463683


People also ask

How do I combine 2 MP4 files?

To merge MP4 files, select multiple MP4 files in the Media Library while holding down Ctrl or Shift key, and then drag and drop to the video track on the Timeline. All MP4 files will be played one after another without a gap after saving to your computer.

Can I put two videos together on android?

PowerDirector is an intuitive, easy-to-follow app to put videos together. For those who want to know how to combine two videos on iPhone or Android, PowerDirector is a good starting place. It also gives you plenty of options to produce a professional quality project. You can download it today for iOS or Android.

Can VLC combine MP4 files?

Once you have installed VLC on your computer, follow these steps to combine or merge videos using it: First, open VLC. Then click on the Media menu and select Open Multiple Files… from the dropdown menu. In the Open Media popup window, under the File tab, click on the +Add button to import the videos you want to merge.


1 Answers

I would suggest using my mp4parser library then you don't have to deal with native libs. Have a look at the AppendExample. It does exactly what you want to do. Look out for the latest version. See below for AppendExample to get an idea how it works.

    Movie[] inMovies = new Movie[]{MovieCreator.build(Channels.newChannel(AppendExample.class.getResourceAsStream("/count-deutsch-audio.mp4"))),
            MovieCreator.build(Channels.newChannel(AppendExample.class.getResourceAsStream("/count-english-audio.mp4")))};

    List<Track> videoTracks = new LinkedList<Track>();
    List<Track> audioTracks = new LinkedList<Track>();

    for (Movie m : inMovies) {
        for (Track t : m.getTracks()) {
            if (t.getHandler().equals("soun")) {
                audioTracks.add(t);
            }
            if (t.getHandler().equals("vide")) {
                videoTracks.add(t);
            }
        }
    }

    Movie result = new Movie();

    if (audioTracks.size() > 0) {
        result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
    }
    if (videoTracks.size() > 0) {
        result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
    }

    IsoFile out = new DefaultMp4Builder().build(result);

    FileChannel fc = new RandomAccessFile(String.format("output.mp4"), "rw").getChannel();
    fc.position(0);
    out.getBox(fc);
    fc.close();
like image 67
Sebastian Annies Avatar answered Oct 14 '22 00:10

Sebastian Annies