Android - merge two MP4 files

I am writing an Android project where I am recording several audio files. Therefore, I set the following parameters. Recording works great.

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

My problem is that every time I write, the output is written to a separate file. Now I need to merge these files into one file. Does anyone have an idea to combine multiple MPEG 4 files in an Android project?

Thanks for your help....

+6
source share
2 answers

Android has no built-in functions for combining two audio files. If you did this through any operation with a file, then this will also work, because it does not work like the headers of audio files.

I recommended using the external FFMPEG library for your Android application.

+1
source

I would suggest using my mp4parser library, then you don't have to deal with native libs. Take a look at AppendExample. He does exactly what you want to do. Stay tuned for the latest version. See below AppendExample to understand 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(); 
+8
source

Source: https://habr.com/ru/post/918360/


All Articles