Combine multiple MP4 videos into one

Originally posted on 2021-01-05

This is a nasty one-liner but it works. Basically we need to use ffmpeg's concat feature and feed it our file names in a specific format. One issue is that the video filenames are relative to the input list's filename. When using process substitution you'll end up getting an error about your video files not being found in /dev/fd/. After some banging of my head against the wall I ended up with the command below.

NOTE: This only works if your video files are named in such a way that ls | sort will list them in order!

#!/usr/bin/env bash

ffmpeg -f concat -safe 0 -i <(ls | sort | grep -i mp4 | xargs -n1 -I {} sh -c 'echo "$(cd "$(dirname "{}")" && pwd)/$(basename "{}")"' | sed s/^/file\ \'/ | sed s/\$/\'/) -c copy out.mp4