Post

Concat mp4 file with ffmpeg

How to concatenate multiple MP4 video files using ffmpeg with a text-based file list and a bash loop.

Concatenate video files (e.g. MP4) specified in a text file using ffmpeg’s concat demuxer.

Example: If you have MP4 files numbered from 1 to 46, each text file references a sound intro, the rendered segment, and a sound outro.

1
2
3
file sound.mp4
file 46-Con-xyz-rendered.mp4
file sound.mp4

This loop iterates over all 46 files, generates a text file listing the sound and rendered segments, and uses ffmpeg -f concat to concatenate them into a final MP4 file.

1
2
3
4
5
6
for i in {1..46}; do
    file=$(echo ${i}-*-rendered.mp4);
    touch ${file};
    echo -e "file sound.mp4\nfile ${file}\nfile sound.mp4" > ${file%.mp4}.txt;
    ffmpeg -f concat -safe 0 -i ${file%.mp4}.txt -c copy ${file%.mp4}-final.mp4;
done
This post is licensed under CC BY 4.0 by the author.