fcberg
fcberg

Reputation: 774

Android - Concat 2 videos using com.netcompss.loader.LoadJNI

I'm using com.netcompss.loader.LoadJNI (FFmpeg4Android) and it was working fine until I try to concat 1 video with audio track and other video without audio track.

I was using this command line:

ffmpeg -y -i /storage/emulated/0/app/1.mp4 -i /storage/emulated/0/DCIM/2.mp4 -strict experimental -filter_complex [0:v]scale=640x360,setsar=1:1[v0];[1:v]scale=1280x720,setsar=1:1[v1];[v0][0:a][v1][1:a] concat=n=2:v=1:a=1 -ab 48000 -ac 2 -ar 22050 -s 640x360 -r 30 -vcodec libx264 -acodec aac -crf 18 /storage/emulated/0/app/out.mp4

1.mp4 has resolution at 640x360 and has audio track.

2.mp4 has resolution at 1280x720 and has no audio track.

On vk.log it was showing this:

Setting 'n' to value '2'
Setting 'v' to value '1'
Setting 'a' to value '1'
Stream specifier ':a' in filtergraph description [0:v]scale=1280x720,setsar=1:1[v0];[1:v]scale=1280x720,setsar=1:1[v1];[v0][0:a][v1][1:a] concat=n=2:v=1:a=1 matches no streams.
exit_program: 1 
Cannot find a matching stream for unlabeled input pad 3 on filter Parsed_concat_4

I'm not good with ffmpeg, so I did some changes to the command line without success.

When all video being concatenated have audio track, there's no problem. But when one of the videos has no audio track, it fails.

What would be the correct command line in this case?

Upvotes: 0

Views: 197

Answers (2)

fcberg
fcberg

Reputation: 774

The answer from Mulvya works fine and answer what I asked. But it will keep only the first audio from first video. In my case a user may want to concat, for example, 4 videos and only one of it has no audio track. The user want to keep other videos audio.

My solution was to check each video before concat. If a video has no audio track, I add a silent audio track. Then, when running my concat command line it won't face any problem.

Upvotes: 0

Gyan
Gyan

Reputation: 93329

The concat filter requires all segments to have the same number of (matching) streams, and in this case, since you only have two files, where the first one has the audio, you can skip the audio concat.

ffmpeg -y -i /storage/emulated/0/app/1.mp4 -i /storage/emulated/0/DCIM/2.mp4 \
       -strict experimental -filter_complex \
       [0:v]scale=640x360,setsar=1[v0];[1:v]scale=640x360,setsar=1[v1]; \
       [v0][v1]concat=n=2[v] -map "[v]" -r 30 -vcodec libx264 -crf 18 \
      -map 0:a -acodec aac -ab 48000 -ac 2 -ar 22050  /storage/emulated/0/app/out.mp4

Since you are scaling the final video to 640x360, there's no point scaling the 2nd video to 1280x720; just scale it directly to final size. And once you do that, there's no need to insert another scaler using the -s option.

Upvotes: 2

Related Questions