我正在尝试使用FFMPEG在Android中合并两个视频,我一直在关注Android War Zone博客,该博客提供了很好的想法和简单的方法来将FFMPEG集成到我们的项目中。但是,我在合并两个视频时遇到了问题。
命令:
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());
上面命令中的“list”是我遇到问题的那个。当我使用以下方法时,它会抛出以下错误:
代码:
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();
}
错误:
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)
我从this堆栈问题中获取了命令。它被接受但我面临上述问题。我非常确定这些视频存在于各自的位置,所有路径都是正确的,但我似乎无法使其发挥作用。
对此的任何见解都非常感谢。提前致谢。
更新:
调用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);
答案 0 :(得分:1)
我不确定我的代码出了什么问题,我仍然无法找到正确的列表。但是,我发现了另一个似乎运作良好的命令。
命令:
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());
正如您在命令中看到的那样,我已经添加了5个视频用于测试,但我相信我们可以动态添加更多视频,这对我来说没有任何问题。
需要注意的事项:
"-i",path + "/" + "num1.mp4"
表示输入,您可以根据需要追加。
[0:v]scale=640x480,setsar=1:1[v0];
并根据输入的数量将其添加为[0:v] ... [1:v] ..等等。
[v0][0:a]
以及根据输入数量添加此参数。
concat=n=5:v=1:a=1
根据视频数量给出n的值。
所以这些是需要注意的主要事项。