Video Format – Best Format for Concatenation

audioffmpegvideo

I have been struggling with .mp4's for about 2 days now, trying to concatenate a series of about 20 into 1 single file. Any differences between the files and it is a nightmare. Some files have different fps, and others different ratios.

I wanted to know if mp4 is the format I should be using, or if there is a better, easy to use format for concatenation. Time isn't an issue, I simply need a relatively high-quality format that concatenates under most circumstances.

If this doesn't exist, any suggestions on how to deal with massively varying properties of mp4 files (I'm not entirely sure of what can differ aside from fps and ratio between videos that may cause a problem) would be great.

I am using ffmpeg to try and concatenate the files. First I tried to use the demuxer, and then the concat filter, however I find the concat filter very daunting and am completely at a loss as to how to use it.

Each video has audio, some have still images while others have moving.

Best Answer

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.

Related Question