Concatenate - FFmpeg
Concatenate - FFmpeg
If you have media files with exactly the same codec and codec parameters you can
concatenate them as described in "Concatenation of files with same codecs". If you have
media with different codecs you can concatenate them as described in "Concatenation of
files with different codecs" below.
There are two methods within ffmpeg that can be used to concatenate files of the same type:
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.
You can read about the concat demuxer in the documentation. This demuxer reads a list of files and other directives from a text file
and demuxes them one after the other, as if all their packets had been muxed together. All files must have the same streams (same
codecs, same time base, etc.) but can be wrapped in different container formats.
Instructions
Create a file mylist.txt with all the files you want to have concatenated in the following form (lines starting with a # are
ignored):
# this is a comment
file '/path/to/file1.wav'
file '/path/to/file2.wav'
file '/path/to/file3.wav'
Note that these can be either relative or absolute paths. Then you can stream copy or re-encode your files:
It is possible to generate this list file with a bash for loop, or using printf . Either of the following would generate a list file
containing every *.wav in the working directory:
On Windows Command-line:
Note: if you do this, you need to open the text file after its creation and 'Save As...' with UTF-8 encoding in order for the
demuxer to work, otherwise you will get an error when FFmpeg tries reading the file because Windows by default saves with
UTF-16 encoding.
ffmpeg -f concat -safe 0 -i <(for f in ./*.wav; do echo "file '$PWD/$f'"; done) -c copy output.wav
ffmpeg -f concat -safe 0 -i <(printf "file '$PWD/%s'\n" ./*.wav) -c copy output.wav
ffmpeg -f concat -safe 0 -i <(find . -name '*.wav' -printf "file '$PWD/%p'\n") -c copy output.wav
You can also loop a video. This example will loop input.mkv 10 times:
The concat demuxer opens the referenced files only when they are needed. This allows us to swap the referenced files atomically
behind the demuxers back to be able to use the concat demuxer as a changeable live source. Check out the following example file
list.txt:
dummy.mxf is referenced twice to make sure the concat demuxer reopens the file when it reaches it. Combine this with infinite
looping and you are done:
Now you can change the looping clip by a simple move command:
mv next_clip.mxf dummy.mxf
Concatenation does not work if the next clip for does not exist at the moment, because decoding won't start until the whole list is
read. However, it is possible to refer to another list at the end of the current list. The following script provides an example for this
mechanism:
#!/bin/bash
fn_concat_init() {
echo "fn_concat_init"
concat_pls=`mktemp -u -p . concat.XXXXXXXXXX.txt`
concat_pls="${concat_pls#./}"
echo "concat_pls=${concat_pls:?}"
mkfifo "${concat_pls:?}"
echo
}
fn_concat_feed() {
echo "fn_concat_feed ${1:?}"
{
>&2 echo "removing ${concat_pls:?}"
rm "${concat_pls:?}"
concat_pls=
>&2 fn_concat_init
echo 'ffconcat version 1.0'
echo "file '${1:?}'"
echo "file '${concat_pls:?}'"
} >"${concat_pls:?}"
echo
}
fn_concat_end() {
echo "fn_concat_end"
{
>&2 echo "removing ${concat_pls:?}"
rm "${concat_pls:?}"
# not writing header.
} >"${concat_pls:?}"
echo
}
fn_concat_init
echo "launching ffmpeg ... all.mkv"
timeout 60s ffmpeg -y -re -loglevel warning -i "${concat_pls:?}" -pix_fmt yuv422p all.mkv &
ffplaypid=$!
fn_concat_end
wait "${ffplaypid:?}"
Note that recursively referencing playlist files will cause ffmpeg to eventually run out of file descriptors (or other resources) because
ffmpeg only closes the playlist file when the playlist has finished, but in the example above because of the recursive chaining none of
the playlist files actually end.
While the demuxer works at the stream level, the concat protocol works at the file level. Certain files (MPEG-2 transport streams,
possibly others) can be concatenated. This is analogous to using cat on UNIX-like systems or copy on Windows.
Instructions
The following command concatenates three MPEG-2 TS files and concatenates them without re-encoding:
If you have MP4 files, these could be losslessly concatenated by first transcoding them to MPEG-2 transport streams. With H.264
video and AAC audio, the following can be used:
If you're using a system that supports named pipes, you can use those to avoid creating intermediate files. This sends stderr (to
which ffmpeg sends all the written data) to /dev/null , to avoid cluttering up the command-line:
The additional -y switch is needed to force ffmpeg to write to existing files temp1 and temp2 , which are the named pipes.
Without the switch, the first two ffmpeg programs running in the background will not produce any output because they wait for
interactive yes/no answers to the questions whether to overwrite existing files.
All MPEG codecs (MPEG-4 Part 10 / AVC, MPEG-4 Part 2, MPEG-2 Video, MPEG-1 Audio Layer II, MPEG-2 Audio Layer III (MP3),
MPEG-4 Part III (AAC)) are supported in the MPEG-TS container format, although the commands above would require some
alteration (e.g., the -bsf bitstream filters will have to be changed).
In many cases, input files will have different codecs or different codec properties, which makes it impossible to use any of the above
methods.
See the concat filter documentation for more info. The filter works on segments of synchronized video and audio streams. All
segments must have the same number of streams of each type, and that will also be the number of streams at output.
Note: Filters are incompatible with stream copying; you can't use -c copy with this method. Since you have to re-encode
the video and audio stream(s), and since re-encoding may introduce compression artifacts, make sure to add proper target
bitrate or quality settings. See the encoding guides? for more info.
For the concat filter to work, the inputs have to be of the same frame dimensions (e.g., 1920⨉1080 pixels) and should have the same
framerate. Therefore, you may at least have to add a scale or scale2ref filter before concatenating videos. A handful of other
attributes have to match as well, like the stream aspect ratio. Refer to the documentation of the filter for more info.
Instructions
Let's say we have three files that we want to concatenate – each of them with one video and audio stream. The concat filter
command would look like this:
Now, let's dissect that command. We first specify all the input files, then instantiate a -filter_complex filtergraph – this is needed
instead of -filter:v because it has multiple inputs and outputs.
[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]
tells ffmpeg which streams to take from the input files and send as input to the concat filter. In this case, video stream 0 [0:v:0] and
audio stream 0 [0:a:0] from input 0 ( input1.mp4 in this example), and video stream 0 [1:v:0] and audio stream 0 [1:v:0] from input 1
( input2.webm ), etc.
concat=n=3:v=1:a=1[outv][outa]'
This is the concat filter itself. n=3 is telling the filter that there are three input segments; v=1 is telling it that there will be one
video stream per segment; a=1 is telling it that there will be one audio stream per segment. The filter then concatenates these
segments and produces two output streams. [outv] and [outa] are names for these output streams. Note that the quotes
around the filter section are required.
The following image shows the stream mapping to and from the filter in the above example:
You can then either re-use these streams in other filters, or map them to the output file:
This tells ffmpeg to use the results of the concat filter rather than the streams directly from the input files.
There is a Bash script called mmcat which was useful for older versions of ffmpeg that did not include the concat filter.
Tags Last modified on Jan 18, 2023, 11:02:02 PM
concat
Attachments (2)