Joining MP4 Files with FFmpeg
# Joining MP4 Files with FFmpeg
## Requirements
Install FFmpeg and make sure it is available:
```bash
ffmpeg -version
```
---
## Join MP4 files without re-encoding (recommended)
This method is the fastest because the video and audio streams are copied directly without quality loss.
It requires all files to have the same codec, resolution, FPS, and audio format.
### Create a file list
Create `files.txt`:
```txt
file 'video1.mp4'
file 'video2.mp4'
file 'video3.mp4'
```
The order in this file determines the order in the final video.
### Merge files
Run:
```bash
ffmpeg -f concat -safe 0 -i files.txt -c copy output.mp4
```
---
## Automatically create the file list (Linux/macOS)
For all MP4 files in the current directory:
```bash
for f in *.mp4; do
echo "file '$f'"
done > files.txt
```
Then merge:
```bash
ffmpeg -f concat -safe 0 -i files.txt -c copy merged.mp4
```
---
## Join MP4 files with re-encoding
Use this if the files have different codecs, resolutions, FPS, or you get errors with `-c copy`.
Example:
```bash
ffmpeg \
-i video1.mp4 \
-i video2.mp4 \
-i video3.mp4 \
-filter_complex \
"[0:v][0:a][1:v][1:a][2:v][2:a]concat=n=3:v=1:a=1[outv][outa]" \
-map "[outv]" \
-map "[outa]" \
output.mp4
```
This method:
- converts the video streams
- normalizes different input files
- takes longer
---
## Merge files directly from the command line
```bash
printf "file '%s'\n" *.mp4 > files.txt
ffmpeg -f concat -safe 0 -i files.txt -c copy merged.mp4
```
---
## Troubleshooting
### Non-monotonous DTS error
Try:
```bash
ffmpeg -fflags +genpts -f concat -safe 0 -i files.txt -c copy output.mp4
```
---
### Check video information
Use:
```bash
ffprobe video1.mp4
```
If the files have different parameters, use the re-encoding method.
---
## Removing source files
FFmpeg does not delete the original files.
After checking the output:
```bash
rm video1.mp4 video2.mp4 video3.mp4
```