Mahesh
Mahesh

Reputation: 2892

How to concat or merge two or more video files in Android?

I want to merge two or more video files (they may be two mp4 or two 3gp, or any other format).

Upvotes: 12

Views: 25966

Answers (3)

Shivam Tripathi
Shivam Tripathi

Reputation: 648

I will share both Java & Kotlin Code

Internally it uses FFmpeg but its lightweight.The easiest way to Add two videos of different types or codec, framerate, and bitrate is to use EpMedia librabry.

Grade Dependency

implementation 'com.github.yangjie10930:EpMedia:v0.9.5'

Kotlin Code

    val epVideos = ArrayList<EpVideo>()
    epVideos.add(EpVideo("/storage/emulated/0/Contact/1.mp4")) // Video 1 Example
    epVideos.add(EpVideo("/storage/emulated/0/Contact/2.mp4")) // Video 2 Exmaple
    val outputOption = EpEditor.OutputOption ("/storage/emulated/0/merge.mp4"); //Output
    outputOption.setWidth(720) // output video width, default 480
    outputOption.setHeight(1280)
    outputOption.frameRate =  25 ; // output video frame rate, default 30

    EpEditor.merge(epVideos,outputOption,object:OnEditorListener{
        override fun onSuccess() {

        }

        override fun onFailure() {

        }


        override fun onProgress(progress: Float) {
            Log.d("Progress","$progress")
        }

    })

Java Code

 private void mergeVideos() {
    ArrayList<EpVideo> epVideos =  new  ArrayList<>();
    epVideos.add(new EpVideo (file2)); // Video 1
    epVideos.add(new EpVideo (file1)); // Video 2
    EpEditor. OutputOption outputOption =new EpEditor.OutputOption(fileOutput);
    outputOption.setWidth(720);
    outputOption.setHeight(1280);
    outputOption.frameRate = 25 ;
    outputOption.bitRate = 10 ;
    EpEditor.merge(epVideos, outputOption, new  OnEditorListener() {
        @Override
        public  void  onSuccess () {
            Log.d("Status","Success");

        }

        @Override
        public  void  onFailure () {

        }

        @Override
        public  void  onProgress ( float  progress ) {
            // Get processing progress here
            Log.d("Progress",""+progress);
        }
    });

}

Upvotes: 5

Marlon
Marlon

Reputation: 1473

You can try INDE Media for Mobile, tutorials are here: https://software.intel.com/en-us/articles/intel-inde-media-pack-for-android-tutorials

It has transcoding\remuxing functionality in MediaComposer class and a possibility to join file\file segments. Since it uses MediaCodec API inside encoding is done on GPU so is very battery friendly and works as fast as possible.

Sample code showing how to enable join or other functionality is on github: https://github.com/INDExOS/media-for-mobile

enter image description here enter image description here enter image description here

Upvotes: 5

onon15
onon15

Reputation: 3630

The most generic tool you can use is ffmpeg (as noted by @Jeremy above), but using it on the mobile handset will require some work; also it is LGPL licensed and some of its encoders (notably x264) are GPL.

A simpler solution, if both files you want to concatenate are using similar encoding, and are contained in file formats derived from MP4 (3GP is such), is to use a pure-java MP4 parser and concatenate the videos without touching the media stream itself. Have a look at mp4parser, an open-source parser that is pure-java, licensed under Apache license and even has an example for concatenating videos in its wiki.

Upvotes: 14

Related Questions