The best way to do this currently is with the concat demuxer. First, create a file called inputs.txt
formatted like so:
file '/path/to/input1.mp4'
file '/path/to/input2.mp4'
file '/path/to/input3.mp4'
Then, simply run this ffmpeg command:
ffmpeg -f concat -i inputs.txt -c copy output.mp4
See also concatenation in ffmpeg
FAQ.
I'm keeping the following here for the benefit of anyone using older versions of ffmpeg.
The latest versions of ffmpeg can do this: you'll have to remux the files into mpeg transport streams first (fairly processor-light, as it's only changing the container format):
ffmpeg -i input.mp4 -map 0 -c copy -f mpegts -bsf h264_mp4toannexb middle.ts
If that throws up an error [1] about h264, you may need to use:
ffmpeg -i input.mp4 -map 0 -c copy -f mpegts -bsf:v h264_mp4toannexb middle.ts
You'll have to do this separately with each input file. To concatenate the files together, use:
ffmpeg -i "concat:middle1.ts|middle2.ts|middle3.ts" -c copy output.mp4
If that throws up an error about aac, you may need to use
ffmpeg -i "concat:middle1.ts|middle2.ts|middle3.ts" -c copy -absf aac_adtstoasc output.mp4
If your system supports named pipes, you can do this without creating intermediate files.
mkfifo temp0 temp1
You'll have to do the following in three separate virtual terminals:
ffmpeg -i input0.mp4 -map 0 -c copy -f mpegts -bsf h264_mp4toannexb -y temp0
ffmpeg -i input1.mp4 -map 0 -c copy -f mpegts -bsf h264_mp4toannexb -y temp1
ffmpeg -f mpegts -i "concat:temp0|temp1" -c copy -absf aac_adtstoasc output.mp4
If output.mp4 already exists, the third ffmpeg will ask you whether you want to overwrite it, but it will do this after it has accessed the FIFOs, and this will make the first to ffmpegs close. So make sure that you choose an unused name for your output file.
This may not work if your input files are different - I believe that differences in bit rate are OK, but frame size, frame rate etc have to match up.
Best Answer
How to concatenate flv file into one