What's the best video format for concatenation

Solution 1:

Multimedia files are complex, so concatenation has several requirements:

  • All segments to be concatenated must have the same number and type of streams.
  • All streams must have the same parameters.

Since each input may vary in any arbitrary parameter I recommend using the concat filter. You will have to conform each input to a common set of parameters using filters. The input format doesn't matter so much because you're going to re-encode everything anyway.

Basic, generic example where main.mp4 has video and audio and end.mp4 has video and no audio. All other parameters are assumed to be different. The filters used here are: anullsrc, scale, pad, setsar, fps, format, aformat, and concat.

ffmpeg -i main.mp4 -i end.mp4 -f lavfi -t 0.1 -i anullsrc=channel_layout=stereo:sample_rate=44100 -filter_complex "[0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=25,format=yuv420p[v0];[1:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=25,format=yuv420p[v1];[0:a]aformat=channel_layouts=stereo:sample_rates=44100[a0];[v0][a0][v1][2:a]concat=n=2:v=1:a=1[v][a]" -map "[v]" -map "[a]" -c:v libx264 -c:a aac -movflags +faststart output.mp4

There are several ways to do this, and you may have to make adjustments based on your inputs or to fit your requirements.