How to merge multiple video/audio streams using FFmpeg?

I’ve created some educational videos and audio for them in various languages; however, the problem is that the video and audio streams are in separate files, which I need to merge into a single mp4 file without causing any audio or video syncing issues; I use FFmpeg for my video transcoding tasks; is there any way to solve my issue using FFmpeg?

Hey Komal,

Well, merging several videos and audio streams is a breeze. But, if you want to include numerous audio streams in your video, all you have to do is utilize multiple input files and use the -map argument to map them into a single output file.

Secondly, for merging video streams sequentially, FFmpeg provides concat filter which takes video and audio streams as input and gives output streams by joining them together one after the other.

Basically, the filter works on segments of synchronized video and audio streams. All segments must have the same number of streams of each type, and that will also be the number of streams at the output. Segments are video and audio stream combinations that you map from input files into a concat filter.
Concat filter takes three kinds of parameters,
• n is the number of segments.
• v is the number of output video streams, which is also the number of video streams in each segment.
• a is the number of output audio streams, which is also the number of audio streams in each segment.

Assuming you want to concatenate an opening, an episode, and an ending, all in a bilingual version then you can use the following FFmpeg command.
ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv -filter_complex \ ‘[0:0] [0:1] [0:2] [1:0] [1:1] [1:2] [2:0] [2:1] [2:2] concat=n=3:v=1:a=2 [v] [a1] [a2]’ \ -map ‘[v]’ -map ‘[a1]’ -map ‘[a2]’ output.mkv

I get the following results when I do this:

Stream specifier ‘2:a’ in filtergraph description [0:a][1:v][2:a][3:v][4:a][5:v]concat=n=3:v=1:a=1[v][a] matches no streams.

What should I do now?

Hey Komal, it seems that your third file used in concatenation does not contain audio stream that’s why you are receiving the mentioned error, just remove [2:a] as the input from your filter graph and i should work fine.

Great it works!

Thanks, Gumlet community.