简体   繁体   中英

Android Merging two video with different (sizes,codec,frames,aspect raito) using FFMPEG

I'm making an app which merges two or more than two video files which I'm getting from another activity. After choosing the files we pass the files to another activity where the merging happens. I've followed this link to do the same : AndroidWarZone FFMPEG

Here I found the way on how to merge the two files only with different qualities. The command is given below :

String[] complexCommand = {"ffmpeg","-y","-i","/storage/emulated/0/videokit/sample.mp4",
"-i","/storage/emulated/0/videokit/in.mp4","-strict","experimental",
"-filter_complex",
"[0:v]scale=640x480,setsar=1:1[v0];[1:v]scale=640x480,setsar=1:1[v1];[v0][0:a][v1][1:a] concat=n=2:v=1:a=1",
"-ab","48000","-ac","2","-ar","22050","-s","640x480","-r","30","-vcodec","mpeg4","-b","2097k","/storage/emulated/0/vk2_out/out.mp4"}

Since I have a list of selected videos inside my array which I'm passing to the next page, I've done some changes in my command, like this :

private void mergeVideos() {
    String savingPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/video.mp4";

    ArrayList<File> fileList = mList;

    List<String> filenames = new ArrayList<String>();

    for (int i = 0; i < fileList.size(); i++) {
        filenames.add("-i");
        filenames.add(fileList.get(i).toString());
    }

    Log.e("Log===",filenames.toString());

    String joined = TextUtils.join(", ",filenames);

    Log.e("Joined====",joined);

    String complexCommand[] = {"-y", joined,
            "-filter_complex",
            "[0:v]scale=640x480,setsar=1:1[v0];[1:v]scale=640x480,setsar=1:1[v1];[v0][0:a][v1][1:a] concat=n=2:v=1:a=1",
            "-ab","48000","-ac","2","-ar","22050","-s","640x480","-r","30","-vcodec","mpeg4","-b","2097k", savingPath};
    Log.e("RESULT====",Arrays.toString(complexCommand));

   execFFmpegBinary(complexCommand);  }

In the log this is the the output I'm getting :

This one is for received data which I have added in the mList

E/RECEIVED DATA=====: [/mnt/m_external_sd/DCIM/Camera/VID_31610130_011933_454.mp4, /mnt/m_external_sd/DCIM/Camera/VID_23120824_054526_878.mp4]
E/RESULT====: [-y, -i, /mnt/m_external_sd/DCIM/Camera/VID_31610130_011933_454.mp4, -i, /mnt/m_external_sd/DCIM/Camera/VID_23120824_054526_878.mp4, -filter_complex, [0:v]scale=640x480,setsar=1:1[v0];[1:v]scale=640x480,setsar=1:1[v1];[v0][0:a][v1][1:a] concat=n=2:v=1:a=1, -ab, 48000, -ac, 2, -ar, 22050, -s, 640x480, -r, 30, -vcodec, mpeg4, -b, 2097k, /storage/emulated/0/video.mp4]

Here result is the complexCommand that is going inside the exeFFMPEGBinary() but not working.

This is my exceFFMPEGBinary()

private void execFFmpegBinary(final String[] combine) {
    try{
    fFmpeg.execute(combine, new ExecuteBinaryResponseHandler() {
        @Override
        public void onFailure(String s) {
            Log.d("", "FAILED with output : " + s);
        }

        @Override
        public void onSuccess(String s) {
            Log.d("", "SUCCESS with output : " + s);
            Toast.makeText(getApplicationContext(),"Success!",Toast.LENGTH_SHORT)
                    .show();
        }

        @Override
        public void onProgress(String s) {
            Log.d("", "progress : " + s);
        }

        @Override
        public void onStart() {
            progressDialog.setMessage("Processing...");
            progressDialog.show();
        }

        @Override
        public void onFinish() {
            progressDialog.dismiss();
        }
    });
} catch (FFmpegCommandAlreadyRunningException e) {
    // do nothing for now
}
}

I've done this and run my project, now the problem is it is not merging/concatenating anything, just a progressDialog comes up for a fraction of second and all I'm getting is this in my log :

E/FFMPEG====: ffmpef : coorect loaded

This means that ffmpeg is loading and nothing is getting implemented. I don't get any log for onFailur, onSuccess(), onStart().

Any suggestions would help me achieve my goal. Thanks.

Note : I have done this merging with the use of Mp4Parser but there is a glitch inside it, it requires the file with same specification. So this is not my requirement.

EDITS

I did some more research and got this to concatenate, but this is not working either, here is the link : Concatenating two files

I've found this stuff also from a link : FFMPEG Merging/Concatenating and found that his piece of code is working fine. But not mine.

I've used that command also but it is not working nor giving me any log results. Except the FFMPEG Loading.

Here is the command :

complexCommand = new String[]{"-y", "-i", file1.toString(), "-i", file2.toString(), "-strict", "experimental", "-filter_complex",
            "[0:v]scale=1920x1080,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", rootPath + "/output.mp4"};

Please use the demo first, make sure it runs on your device. If it does, take the merge command from the blog, and use it in the demo. Now, try a simple command in your app, if it works try the merge command. Contact me directly if any of this steps fails.

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