Sachin Suthar Sachin Suthar - 2 months ago 19
Android Question

How to merge multiple .mp4 file in single .mp4 ffmpeg android

I am merging multiple .mp4 file into single .mp4 after merging only first .mp4 is displayed properly and other video is not displayed.

I used this

ffmpeg
command

ffmpeg -f concat -i " + strTextPath + " -c copy " + strFinalMergedVideoPath;

Answer

First store all the MP4 file which you want to merge in Arraylist like below.

private ArrayList<String> arrFilePaths = new ArrayList<>();

arrFilePaths .add(file1path);

arrFilePaths .add(file2path);

arrFilePaths .add(file3path);

Set path for output file like below.

String strVideoFolderPath = Environment.getExternalStorageDirectory();

After that Run below Asynctask class.

public class MergeVideo extends AsyncTask<String, Integer, String> {
        ProgressDialog progressDialog;    

        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(MotionFlyerTabActivity.this,"",
                    "Please wait...", true);
            // do initialization of required objects objects here
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                Movie[] inMovies = new Movie[arrFilePaths.size()];
                ArrayList<Movie> _clips = new ArrayList<>();
                for (int i = 0; i < arrFilePaths.size(); i++) {
                    inMovies[i] = MovieCreator.build(
                            arrFilePaths.get(i));
                }
                List<Track> videoTracks = new LinkedList<>();
                List<Track> audioTracks = new LinkedList<>();
                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()])));
                }

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



                FileChannel fc = new RandomAccessFile(String.format(strVideoFolderPath + "/output.mp4"), "rw").getChannel();
                out.writeContainer(fc);
                fc.close();

                File file = new File(strVideoFolderPath + "/output.mp4");
                String mFileName = file.getAbsolutePath();

                return mFileName;


            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String value) {
            super.onPostExecute(value);
            progressDialog.dismiss();

        }
    }
Comments