How to combine multiple mp4 files easily using ffmpeg

Combining multiple MP4 files into a single file using FFmpeg is quite straightforward. However, it's important to note that for a seamless combination, the MP4 files should have the same resolution, frame rate, and codec parameters. If they differ, you might need to preprocess them to match these properties before combining (read step 4).

Step 1: Install FFmpeg

You can install FFmpeg for your operating system by following instructions HERE

Step 2: Create a file list

Create a text file that lists all the MP4 files you want to combine, one file per line, prefixed with file . For example, save the following as filelist.txt:

file 'input1.mp4'
file 'input2.mp4'
file 'input3.mp4'

Make sure to replace input1.mp4, input2.mp4, input3.mp4, etc., with the actual names of your files.

Step 4: To re-encode or not

  • If your files are not identical in terms of codec parameters, you might need to re-encode them during the concatenation process. This can be done by removing -c copy and letting FFmpeg choose the default encoding options, or by specifying encoding parameters explicitly.
  • The concat demuxer is the preferred method for concatenating files that are similar because it doesn't re-encode the streams. If you need to concatenate files that require re-encoding, you might use the concat filter instead, but that's a more complex process and can reduce the quality due to re-encoding.

Step 3: Combine files using FFmpeg

Open a terminal or command prompt, navigate to the directory containing your MP4 files and the filelist.txt file, and execute the following command:

ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4
  • -f concat: Tells FFmpeg to use the concat demuxer.
  • -safe 0: Allows using absolute paths and is necessary if your paths or filenames contain special characters.
  • -i filelist.txt: Specifies the input file list.
  • -c copy: Copies the video and audio streams without re-encoding, ensuring the process is fast and the quality is preserved.
  • output.mp4: Specifies the name of the combined output file.