There are a lot of questions and articles how to concatenate multiple videos into one with FFmpeg. Once upon time we had to deal with that feature.
Due to limits of each concatenation method we implemented it by “filter”. Concat demuxer requires that all files must have the same streams (same codes, time base, etc.) and we need something more flexible. However Concat protocol was easy to use but supports only MPEG based files. Concatenating by filter has restrictions too, but we adapt some features to be more elastic. Full implementation is available on GitHub.
All of concatenation options can be used in Xabe.FFmpeg by Conversion.Start(string) which allows user to pass arguments directly into FFmpeg. But we will focus on our implementation of concatenation.
C# FFmpeg concatenation
Using our implementation is quite easy. Just call one method:
Implementation of this method is a bit complicated (it is available on our GitHub repository) and it doesn’t allow to pass any additional arguments or manipulate particular streams in exchange for flexibility of passing files. Above command generate ffmpeg command like this:
IConversionResult result = await Conversion.Concatenate(output, firstFile, secondFile);
ffmpeg.exe -n -threads 12 -i "C:\Users\tzmuda\Source\Xabe.FFmpeg\Xabe.FFmpeg.Test\bin\Debug\netcoreapp2.0\Resources\SampleVideo_360x240_1mb.mkv" -i "C:\Users\tzmuda\Source\Xabe.FFmpeg\Xabe.FFmpeg.Test\bin\Debug\netcoreapp2.0\Resources\mute.mp4" -t 1 -f lavfi -i anullsrc=r=48000:cl=stereo -filter_complex " [0:v]scale=1280:720,setdar=dar=16:9,setpts=PTS-STARTPTS[v0]; [1:v]scale=1280:720,setdar=dar=16:9,setpts=PTS-STARTPTS[v1]; [v0][0:a] [v1] concat=n=2:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" "C:\Users\tzmuda\AppData\Local\Temp\tmpD0BE.mp4"