Search code examples
androidffmpegandroid-ffmpeg

Merging/concat videos in android through FFMPEG


I am working on video merging application through FFMPEG. My code is working if i select two same videos. but if i select two different video with different bit-rate and resolution then video merging is not working. I am using this commands.

List<String> cmdList = new ArrayList<>();
cmdList.add("-y");
StringBuilder sb = new StringBuilder();

for (int i = 0; i < videoList.size(); i++) {
    cmdList.add("-i");
    cmdList.add(videoList.get(i).getThumbnailPath());
    sb.append("[").append(i).append(":0] [").append(i).append(":1]");
}

sb.append(" concat=n=").append(videoList.size()).append(":v=1:a=1 [v] [a]");
cmdList.add("-filter_complex");
cmdList.add(sb.toString());
cmdList.add("-map");
cmdList.add("[v]");
cmdList.add("-map");
cmdList.add("[a]");
cmdList.add("-preset");
cmdList.add("ultrafast");
cmdList.add("storage/emulated/0/abcfs.mp4");

sb = new StringBuilder();
for (String str : cmdList) {
    sb.append(str).append(" ");
}

String[] cmd = cmdList.toArray(new String[cmdList.size()]);

I also used this command.

String[] complexCommand = {
    "-y", "-i", 
    pathArraylist.get(0),
    "-i", pathArraylist.get(1), 
    "-strict", 
    "experimental", 
    "-filter_complex",
    "[0:v]scale=hd720,setsar=1:1[v0];[1:v]scale=hd720,setsar=1:1[v1];[v0][0:a][v1][1:a] concat=n=2:v=1:a=1",
    "-ab", 
    "48000", 
    "-ac", 
    "2", 
    "-ar", 
    "22050", 
    "-s", 
    "hd720", 
    "-vcodec", 
    "mpeg4", 
    "-b:v", 
    "600" +"k", 
    "storage/emulated/0/outputvideo.mp4"
};

Please let me know which command I used if I concatenate two videos with different bit-rate and resolution.


Solution

  • You will have to bring both the videos to be concatenated to same resolutions. This can either be done independently first by using ffprobe to determine WxH of videos and scaling one video to the resolution of another video.

    This can also be done altogether by:

    1. Determine WxH of first video using ffprobe.
    2. Concatenating videos using the following:

    ffmpeg -y -i input-1.mp4 -i input-2.mp4 -filter_complex "[0:v]setsar=1:1[v0];[1:v]scale=W:H,setsar=1:1[v1];[v0][0:a][v1][1:a] concat=n=2:v=1:a=1" out.mp4

    [0:v]setsar=1:1[v0], sets Sample aspect ratio of 1st video stream to 1:1, it can also be different but similar to the one being used in other input's video stream as in [1:v]scale=W:H,setsar=1:1[v1]. This would scale [1:v] stream to [0:v] video's resolution. You can also modify this command to take care of aspect ratio of 2nd video and by including black stripe paddings which are beyond the scope of this question.

    And to take care of bit rates, you can explicitly specify encoder parameters for creating your concatenated video and it should take care of everything as both video's video streams will be re-encoded again. You can use something like:

    ffmpeg -y -i input-1.mp4 -i input-2.mp4 -filter_complex "[0:v]setsar=1:1[v0];
    [1:v]scale=W:H,setsar=1:1[v1];[v0][0:a][v1][1:a] concat=n=2:v=1:a=1" -
    c:v libx264 -b:v 600K -c:a libfdk_aac -b:a 128K out.mp4