Search code examples
javaffmpeg

Filter concat has an unconnected output while Video concatenation in ffmpeg wrapper in java


I am trying to stitch variable number of videos using ffmpeg wrapper.

I have this code:

private void videoStich(List<String> videoPaths){
        FFmpeg ffmpeg = null;
        FFprobe ffprobe=null;
        String stitchedOutputPath = outputFolder + "/stitched_output.mp4";

        try {
            ffmpeg = new FFmpeg();
            ffprobe = new FFprobe();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        FFmpegBuilder builder = new FFmpegBuilder();

        for (int i = 0; i < videoPaths.size(); i++) {
            String videoPath = videoPaths.get(i);
            builder.addInput(videoPath);
        }

        // Add complex filter for concatenation
        StringBuilder complexFilter = new StringBuilder();
        for (int i = 0; i < videoPaths.size(); i++) {
            complexFilter.append("[").append(i).append(":v][").append(i).append(":a]");
//            if (i < videoPaths.size() - 1) {
//                complexFilter.append("[");
//            }
        }
        complexFilter.append("concat=n=").append(videoPaths.size()).append(":v=1:a=1[v][a]");

        builder.setComplexFilter(complexFilter.toString())
                .addOutput(stitchedOutputPath)
                .setFormat("mp4")
                .done();

        FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);
        executor.createJob(builder).run();
    }

but getting this error:

2024-02-28T16:30:53.033+05:30  INFO 4711 --- [sample_project] [pool-3-thread-1] net.bramp.ffmpeg.RunProcessFunction      : ffmpeg -version
2024-02-28T16:30:53.126+05:30  INFO 4711 --- [sample_project] [pool-3-thread-1] net.bramp.ffmpeg.RunProcessFunction      : ffmpeg -y -v error -i src/main/resources/cutted_videos/0_trimmed.mp4 -i src/main/resources/cutted_videos/1_trimmed.mp4 -filter_complex [0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[v][a] -f mp4 src/main/resources/cutted_videos/stitched_output.mp4
Filter concat has an unconnected output
java.lang.RuntimeException: java.io.IOException: ffmpeg returned non-zero exit status. Check stdout.

I have been trying to fix this since a long time and didn't find any issue with the filter, it is looking good to me. Can anyone help me with it?

EDIT: I found that when I use this command in terminal:

ffmpeg -i in1.mp4 -i in2.mp4\
            -filter_complex "[0:v] [0:a] [1:v] [1:a] \
concat=n=2:v=1:a=1 [v] [a]" \
            -f mp4  output_test5.mp4

it didn;t work while when i used this:

ffmpeg -i in1.mp4 -i in2.mp4\
            -filter_complex "[0:v] [0:a] [1:v] [1:a] \
concat=n=2:v=1:a=1 [v] [a]" \
            -f mp4 -map "[v]" -map "[a]" output_test4.mp4

it worked. I think i need to add -map "[v]" -map "[a]" somehow in the code.

but when i try to add: builder.setComplexFilter(complexFilter.toString()) .addOutput(stitchedOutputPath) .setFormat("mp4") .map() .done();

or

builder.setComplexFilter(complexFilter.toString()) .addOutput(stitchedOutputPath) .setFormat("mp4") .setMap() .done();

both .set() or setMap() doesn't exist. What to do, how to do?


Solution

  • I got the solution: I had to ad .addExtraArgs("-map", "[v]", "-map", "[a]")

    Thanks