无法使用FFMPEG命令合并视频 [英] Unable to merge videos using FFMPEG Commands
问题描述
我正在使用FFMPEG在Android中合并两个视频,并且我一直在追踪 Android War Zone 博客,其中提供了将FFMPEG整合到我们项目中的好主意和简单方法。但是,我正在面临两个合并视频的问题。
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.
命令:
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 :
代码:
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)
我从这个堆栈问题获取了命令。它接受了但我正面临上述问题。我非常确定视频存在于各自的位置,所有路径都是对的,但我似乎无法使其正常工作。
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.
更新:
调用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);
推荐答案
我不知道我的代码出了什么问题,我还没有能够列出正确的名单。但是,我发现另外一个命令似乎很好。
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.
命令:
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个视频,但我相信我们可以动态添加更多视频,这对我没有任何问题。
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.
需要注意的事项:
"-i",path + "/" + "num1.mp4"
代表输入和您可以追加任意数量。
represent the input and you can append as many as you want.
[0:v]scale=640x480,setsar=1:1[v0];
并根据输入的数量相应添加此值作为[0:v] ... [1 :v] ..等等。
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
根据视频。
所以这些是需要照顾的主要事情。
So those are the main things that needs to be taken care of.
这篇关于无法使用FFMPEG命令合并视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!