简体   繁体   中英

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

我想合并两个或多个视频文件(它们可能是两个 mp4 或两个 3gp,或任何其他格式)。

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.

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

在此处输入图片说明在此处输入图片说明在此处输入图片说明

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);
        }
    });

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM