Unable to merge videos using FFMPEG Commands

2019-07-10 16:21发布

I am trying to merge two videos in Android using FFMPEG and I have been following the Android War Zone blog which gives great ideas and simple methods to integrate FFMPEG in our project. However, I am facing issues in merging two videos.

Command :

  vk.run(new String[]{
   "ffmpeg",
    "-f",
     "concat",
      "-i",
        list,
         "-s",
          "hd720",
            "-c",
              "copy",
                "-b",
                 br_from_db + "k",
                   path + "/" + "merged_video_3.mp4"
                    }, work_path, getActivity());

And the "list" in the above command is the one where I am facing a issue.It throws me the following error when I use the following method :

Code :

private String generateList(String[] inputs) {
        File list;
        Writer writer = null;
        try {
            list = File.createTempFile("ffmpeg-list", ".txt");
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(list)));
            for (String input : inputs) {
                writer.write("file '" + input + "'\n");
                Log.d(TAG, "Writing to list file: file '" + input + "'");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "/";
        } finally {
            try {
                if (writer != null)
                    writer.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        Log.d(TAG, "Wrote list file to " + list.getAbsolutePath());
        return list.getAbsolutePath();
    }

Error :

12-16 19:49:57.416    5437-5437/? E/ffmpeg4android﹕ Command validation failed.
12-16 19:49:57.416    5437-5437/? E/ffmpeg4android﹕ Check if input file exists: /data/data/com.family45.golive.family45v1/cache/ffmpeg-list-1803386407.txt/storage/emulated/0/DCIM/Camera/dec24.mp4 /storage/emulated/0/DCIM/Camera/vid2.mp4
12-16 19:49:57.416    5437-5437/? W/System.err﹕ com.netcompss.ffmpeg4android.CommandValidationException
12-16 19:49:57.416    5437-5437/? W/System.err﹕ at com.netcompss.loader.LoadJNI.run(LoadJNI.java:34)
12-16 19:49:57.416    5437-5437/? W/System.err﹕ at com.netcompss.loader.LoadJNI.run(LoadJNI.java:49)

I obtained the command from this stack question. Its accepted but I am facing the above issue. I am very sure that the videos are present in their respective locations and all the paths are right but I cant seem to make it work.

Any insights on this is highly appreciated. Thanks in advance.

Update :

Call to generateList:

ArrayList<String> paths_to_merge = new ArrayList<String>();
  paths_to_merge.add(path + "/" + "dec24.mp4");
  paths_to_merge.add(path + "/" + "vid2.mp4");
  LoadJNI vk = new LoadJNI();
  String[] v12 = new String[paths_to_merge.size()];
  v12 = paths_to_merge.toArray(v12);
  String list = generateList(v12);

1条回答
祖国的老花朵
2楼-- · 2019-07-10 16:47

I am not sure what went wrong in my code, I am still not able to come with the right list. However, I found another command which seems to be working good.

Command :

   vk.run(new String[]{"ffmpeg","-y","-i",path + "/" + "num1.mp4","-i",path + "/" + "num2.mp4","-i",path + "/" + "num3.mp4","-i",path + "/" + "num4.mp4",
                        "-i",created_folder + "/" + "created_video2.mp4","-strict","experimental",
                        "-filter_complex",
                        "[0:v]scale=640x480,setsar=1:1[v0];[1:v]scale=640x480,setsar=1:1[v1];[2:v]scale=640x480,setsar=1:1[v2];[3:v]scale=640x480,setsar=1:1[v3];" +
                                "[4:v]scale=640x480,setsar=1:1[v4];[v0][0:a][v1][1:a][v2][2:a][v3][3:a][v4][4:a] concat=n=5:v=1:a=1",
                        "-ab","48000","-ac","2","-ar","22050","-s","640x480","-r","30","-vcodec","mpeg4","-b","2097k",path + "/" + "numbers_video_m.mp4"},path,getActivity());

As you can see in the command, I have appended 5 videos for the purpose of testing but I believe that we can add more videos dynamically and this works without any issues for me.

Things to be noted :

"-i",path + "/" + "num1.mp4" 

represent the input and you can append as many as you want.

[0:v]scale=640x480,setsar=1:1[v0];

and add this according to the number of inputs accordingly as [0:v]...[1:v].. and so on.

[v0][0:a]

and also this parameter to be added according the number of inputs.

concat=n=5:v=1:a=1

Give the value of n according to the number of videos.

So those are the main things that needs to be taken care of.

查看更多
登录 后发表回答