| 1 | | If you have media files with exactly the same codec and codec parameters you can concatenate them as described in "1. Concatenation of files with same codecs". If you have media with different codecs you can concatenate them as described in "2. Concatenation of files with different codecs" below. |
| 2 | | |
| 3 | | |
| 4 | | = 1. Concatenation of files with same codecs = |
| 5 | | |
| 6 | | == Description == |
| 7 | | |
| 8 | | The concat demuxer was added to ffmpeg 1.1 . You can read about it in the [http://ffmpeg.org/ffmpeg-formats.html#concat documentation] |
| 9 | | |
| 10 | | == Instructions == |
| | 1 | = Concatenating media files = |
| | 2 | |
| | 3 | [[PageOutline(2-3, Contents)]] |
| | 4 | |
| | 5 | If you have media files with exactly the same codec and codec parameters you can concatenate them as described in "[#samecodec Concatenation of files with same codecs]". If you have media with different codecs you can concatenate them as described in "[#differentcodec Concatenation of files with different codecs]" below. |
| | 6 | |
| | 7 | |
| | 8 | == Concatenation of files with same codecs ==#samecodec |
| | 9 | |
| | 10 | There are two methods within ffmpeg that can be used to concatenate files of the same type: [#demuxer the concat ''demuxer''] and [#protocol the concat ''protocol'']. The demuxer is more flexible - it requires the same codecs, but different container formats can be used; and it can be used with any container formats, while the protocol only works with a select few containers. However, the concat protocol is available in older versions of ffmpeg. |
| | 11 | |
| | 12 | === Concat demuxer ===#demuxer |
| | 13 | |
| | 14 | The concat demuxer was added to ffmpeg 1.1 . You can read about it in the [http://ffmpeg.org/ffmpeg-formats.html#concat documentation]. |
| | 15 | |
| | 16 | ==== Instructions ==== |
| 26 | | = 2. Concatenation of files with different codecs = |
| 27 | | |
| 28 | | == Description == |
| | 32 | === Concat protocol ===#protocol |
| | 33 | |
| | 34 | While the demuxer works at the stream level, the concat protocol works at the file level. Certain files (mpg and mpeg transport streams, possibly others) can be concatenated. This is analogous to using cat on UNIX-like systems or copy on Windows. In general, the demuxer is the better option. |
| | 35 | |
| | 36 | ==== Instructions ==== |
| | 37 | |
| | 38 | {{{ |
| | 39 | ffmpeg -i "concat:input1.mpg|input2.mpg|input3.mpg" -c copy output.mpg |
| | 40 | }}} |
| | 41 | |
| | 42 | If you have MP4 files, these could be losslessly concatenated by first transcoding them to mpeg transport streams. With h.264 video and AAC audio, the following can be used: |
| | 43 | |
| | 44 | {{{ |
| | 45 | ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts |
| | 46 | ffmpeg -i input2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts |
| | 47 | ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -c copy -bsf:a aac_adtstoasc output.mp4 |
| | 48 | }}} |
| | 49 | |
| | 50 | If you're using a system that supports named pipes, you can use those to avoid creating intermediate files - this sends stderr (which ffmpeg sends all the written data to) to /dev/null, to avoid cluttering up the command-line: |
| | 51 | |
| | 52 | {{{ |
| | 53 | mkfifo temp1 temp2 |
| | 54 | ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp1 2> /dev/null & \ |
| | 55 | ffmpeg -i input2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp2 2> /dev/null & \ |
| | 56 | ffmpeg -f mpegts -i "concat:temp1|temp2" -c copy -bsf:a aac_adtstoasc output.mp4 |
| | 57 | }}} |
| | 58 | |
| | 59 | All MPEG codecs (h.264, MPEG4/divx/xvid, MPEG2; MP2, MP3, AAC) are supported in the mpegts container format, though the commands above would require some alteration (the -bsf bitstream filters will have to be changed). |
| | 60 | |
| | 61 | == Concatenation of files with different codecs ==#differentcodec |
| | 62 | |
| | 63 | === Concat filter ===#filter |