Joining videos in Xabe.FFmpeg

There are a lot of questions and articles how to concatenate multiple videos into one with FFmpeg. Once upon time we hadto deal with that feature.

    1. FFmpeg offers few ways of concatenation:
  1. Concat demuxer
  2. Concat protocol
  3. Concat filters

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 somethingmore flexible. HoweverConcat 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 onGitHub.

All of concatenation options can be used in Xabe.FFmpeg byConversion.Start(string) which allows user to pass arguments directly into FFmpeg. But we will focus on our implementationof concatenation.

C# FFmpeg concatenation

Using our implementation is quite easy. Just call one method:


IConversionResult result = await Conversion.Concatenate(output, firstFile, secondFile);
                

Implementation of this method is a bit complicated (it is available on our GitHub repository) and it doesn't allow topass any additional arguments or manipulate particular streams in exchange for flexibility of passing files. Above commandgenerate ffmpeg command like this:


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"
                

More info in Xabe.FFmpeg documentation