简体   繁体   English

Android使用FFMPEG合并两个具有不同(大小,编解码器,帧,长宽比)的视频

[英]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 我已经按照以下链接进行了操作: 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 这是我在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. 在这里,结果是在exeFFMPEGBinary()内部运行但无法正常工作的complexCommand

This is my exceFFMPEGBinary() 这是我的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 : 我已经完成并运行了我的项目,现在的问题是它没有合并/连接任何东西,只有一秒钟的ProgressDialog出现了,而我所得到的只是我的日志中的内容:

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

This means that ffmpeg is loading and nothing is getting implemented. 这意味着ffmpeg正在加载,并且什么也没有实现。 I don't get any log for onFailur, onSuccess(), onStart(). 我没有任何有关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. 注意:我已经使用Mp4Parser完成了合并,但是里面有一个小故障,它需要具有相同规格的文件。 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. 我也从以下链接找到了这些东西: FFMPEG合并/连接 ,发现他的代码段工作正常。 But not mine. 但不是我的。

I've used that command also but it is not working nor giving me any log results. 我也使用了该命令,但是它不起作用,也没有给我任何日志结果。 Except the FFMPEG Loading. FFMPEG加载除外。

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. 如果是这样,请从博客中获取merge命令,并在演示中使用它。 Now, try a simple command in your app, if it works try the merge command. 现在,在您的应用程序中尝试一个简单命令,如果可行,请尝试合并命令。 Contact me directly if any of this steps fails. 如果以上任何步骤失败,请直接与我联系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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