Вопрос

I am using this code. I need to merge two videos. It saved all videos in temp folder but not in merged condition. Append and DoAppend are my functions which I want for merging the videos.

public String append(ArrayList<String> trimVideos) {

        for (int i = 0; i < trimVideos.size() - 1; i++) {

      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
     if (i == 0) {                      
            String OutPutFileName = Constants.STORAGE_VIDEO_TEMP_PATH +                           
            File.separator + "APPEND" + "_" + timeStamp + ".mp4";
            doAppend(trimVideos.get(0), trimVideos.get(i + 1),OutPutFileName);
            Log.e(Constants.TAG, "In First: " + i + " " +   OutPutFileName);

                  } else {

        String OutPutFileName = Constants.STORAGE_VIDEO_TEMP_PATH
    + File.separator + "APPEND" + i + "_" + timeStamp + ".mp4";
                    doAppend(lastAppendOut, trimVideos.get(i + 1), OutPutFileName);
                    Log.e(Constants.TAG, "In Second: " + i + " " + OutPutFileName);
                }
            }
            Log.e(Constants.TAG, "In End: "  + " " + lastAppendOut);
            return lastAppendOut;
        }

This Method Crashed my application on add track.

private String doAppend(String _firstVideo, String _secondVideo,String _newName) {
        try {

            Log.e("test", "Stage1");
            FileInputStream fis1 = new FileInputStream(_firstVideo);
            FileInputStream fis2 = new FileInputStream(_secondVideo);

            Movie[] inMovies = new Movie[] {
                    MovieCreator.build(fis1.getChannel()),MovieCreator.build(fis2.getChannel()) };

            List<Track> videoTracks = new LinkedList<Track>();
            List<Track> audioTracks = new LinkedList<Track>();
    //It returns one item of video and 2 item of video.

            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);
                    }
                }
            }
            Log.e("test", "Stage2");
            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);
            Log.e("test", "Stage3");
            String filename = _newName;
            lastAppendOut = filename;
            Log.e(Constants.TAG, "In Append: "  + " " + lastAppendOut);

            FileOutputStream fos = new FileOutputStream(filename);
            FileChannel fco = fos.getChannel();

            fco.position(0);
            out.getBox(fco);
            fco.close();
            fos.close();
            fis1.close();
            fis2.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("check", e.getMessage());
        }
        return _newName;
    }
Это было полезно?

Решение

Code For Merging Multiple Video

Gradle Dependency

implementation 'com.googlecode.mp4parser:isoparser:1.1.9'

Code

private String appendTwoVideos(String firstVideoPath, String secondVideoPath)
{
    try {
        Movie[] inMovies = new Movie[2];

        inMovies[0] = MovieCreator.build(firstVideoPath);
        inMovies[1] = MovieCreator.build(secondVideoPath);

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

        @SuppressWarnings("resource")
        FileChannel fc = new RandomAccessFile(Environment.getExternalStorageDirectory() + "/wishbyvideo.mp4", "rw").getChannel();
        out.writeContainer(fc);
        fc.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    String mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
    mFileName += "/wishbyvideo.mp4";
    return mFileName;
}

You might wanna call this function from a background thread.

Другие советы

The above answer is perfectly right but It will only work if your media encoder is H264.....

mediaRecorder.setVideoEncoder(VideoEncoder.H264);

happy Coding :) :D

The above answer will only work when your codec, framerate, and bitrate are the same.

Gradle Dependency(This library will work in nearly every case)

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

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

    }

Gradle Dependency

implementation "com.writingminds:FFmpegAndroid:0.3.2"

Code

Command to concate two videos side by side into one

val cmd : arrayOf("-y", "-i", videoFile!!.path, "-i", videoFileTwo!!.path, "-filter_complex", "hstack", outputFile.path)

Command to append two videos (one after another) into one

  val cmd : arrayOf("-y", "-i", videoFile!!.path, "-i", videoFileTwo!!.path, "-strict", "experimental", "-filter_complex",
                        "[0:v]scale=iw*min(1920/iw\\,1080/ih):ih*min(1920/iw\\,1080/ih), pad=1920:1080:(1920-iw*min(1920/iw\\,1080/ih))/2:(1080-ih*min(1920/iw\\,1080/ih))/2,setsar=1:1[v0];[1:v] scale=iw*min(1920/iw\\,1080/ih):ih*min(1920/iw\\,1080/ih), pad=1920:1080:(1920-iw*min(1920/iw\\,1080/ih))/2:(1080-ih*min(1920/iw\\,1080/ih))/2,setsar=1:1[v1];[v0][0:a][v1][1:a] concat=n=2:v=1:a=1",
                        "-ab", "48000", "-ac", "2", "-ar", "22050", "-s", "1920x1080", "-vcodec", "libx264", "-crf", "27",
                        "-q", "4", "-preset", "ultrafast", outputFile.path)

Note :

"videoFile" is your first video path.
"videoFileTwo" is your second video path.
"outputFile" is your combined video path which is our final output path

To create output path of video

fun createVideoPath(context: Context): File {
        val timeStamp: String = SimpleDateFormat(Constant.DATE_FORMAT, Locale.getDefault()).format(Date())
        val imageFileName: String = "APP_NAME_"+ timeStamp + "_"
        val storageDir: File? = context.getExternalFilesDir(Environment.DIRECTORY_MOVIES)
        if (storageDir != null) {
            if (!storageDir.exists()) storageDir.mkdirs()
        }
        return File.createTempFile(imageFileName, Constant.VIDEO_FORMAT, storageDir)
    }

Code to execute command

try {
            FFmpeg.getInstance(context).execute(cmd, object : ExecuteBinaryResponseHandler() {
                override fun onStart() {

                }

                override fun onProgress(message: String?) {
                    callback!!.onProgress(message!!)
                }

                override fun onSuccess(message: String?) {
                    callback!!.onSuccess(outputFile)
                }

                override fun onFailure(message: String?) {
                    if (outputFile.exists()) {
                        outputFile.delete()
                    }
                    callback!!.onFailure(IOException(message))
                }

                override fun onFinish() {
                    callback!!.onFinish()
                }
            })
        } catch (e: Exception) {
            
        } catch (e2: FFmpegCommandAlreadyRunningException) {
           
        }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top