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.

  • 4
    The first thing is to know the difference between codecs and containers; research it. MP4 is a container. See this and this, especially All corresponding streams must have the same parameters in all segments; the filtering system will automatically select a common pixel format for video streams, and a common sample format, sample rate and channel layout for audio streams, but other settings, such as resolution, must be converted explicitly by the user. – Kamil Maciorowski Mar 21 at 6:58

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.

  • Thanks for this. This command worked for my files, but I found that the main reason they weren't concatenating was because I was creating some of the mp4's using a .wav file format, with a default frequency of not 44100 (I think it was 22100?), so once creating the mp4 I forced the frequency to be 44100, and ensured the aspect ration of every video was identical before concatenation and was able to use the demuxer. I found that mkvmerge was super useful here because it told you exactly why it was unable to concat. – Recessive Mar 22 at 4:12
  • The frequency before forcing 44100 was 22050 – Recessive Mar 22 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.