Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to concatenation 2 or more videos, using FFMPG but after join videos it has audio/video syncing issue, i used following code for that

ffmpeg -i input1.flv -qscale:v 1 intermediate1.mpg
ffmpeg -i input2.flv -qscale:v 1 intermediate2.mpg
ffmpeg -i input3.flv -qscale:v 1 intermediate3.mpg
ffmpeg -i input4.flv -qscale:v 1 intermediate4.mpg
cat intermediate1.mpg intermediate2.mpg intermediate3.mpg intermediate4.mpg > intermediate_all.mpg
ffmpeg -i intermediate_all.mpg -qscale:v 2 output.avi

The audio track in the output.avi is preceding the video track.

share|improve this question

migrated from Dec 31 '12 at 3:12

This question came from our site for professional and enthusiast programmers.

Have you tried playing back the intermediate_all.mpg? – Jack Dec 26 '12 at 10:25
Ya but it played, but not correct duration – Elby Dec 26 '12 at 10:49
That could be the cause. Have you tried to concatenate the videos with ffmpeg itself? – Jack Dec 26 '12 at 11:27
how we can concatenate the videos with ffmpeg? – Elby Dec 26 '12 at 12:12
Dunno. Why don't you google it! :) – Jack Dec 26 '12 at 12:13
up vote 3 down vote accepted

Using cat like that can cause unpredictable problems. Fortunately, there are two really useful ways to concatenate files with ffmpeg.

Concat demuxer

If all your inputs are the same type of file (same codecs etc), then you can use the concat demuxer. This is probably the more useful answer to your question, since it is compatible with -c copy (and so should be lossless, and take an almost trivially short time to complete).

First, create a file called inputs.txt containing the following:

file '/path/to/input1.flv'
file '/path/to/input2.flv'
file '/path/to/input3.flv'
file '/path/to/input4.flv'

Then use the following command:

ffmpeg -f concat -i inputs.txt -c copy output.flv

If that doesn't work, or you want to combine videos of different types, you can use the

Concat filter

ffmpeg -i input1.flv -i input2.flv -i input3.flv -i input4.flv \
-filter_complex '[0:0] [0:1] [1:0] [1:1] [2:0] [2:1] [3:0] [3:1] concat=n=4:v=1:a=1 [v] [a]' \
-map '[v]' -map '[a]' -c:v libx264 -crf 22 -preset veryfast -c:a libfdk_aac -vbr 3 -afterburner 1 output.mp4

This example uses my preferred encoding settings, obviously change up the last line to use whatever you want to.

I'll break apart the -filter_complex section, to make it easier to follow.

'[0:0] [0:1] [1:0] [1:1] [2:0] [2:1] [3:0] [3:1] 

This fragment tells ffmpeg what streams to send to the concat filter; in this case, streams 0 and 1 from input 0 (ffmpeg starts counting from 0, so that's the first and second streams from the first input file, input1.flv in this example), and streams 0 and 1 from every input up to the fourth (input 3 according to ffmpeg, which starts counting from 0).

concat=n=4:v=1:a=1 [v] [a]'

This is the concat filter itself. n=4 is telling the filter that there are four input files; v=1 is telling it that there will be one video stream; a=1 is telling it that there will be one audio stream (I know I said that ffmpeg starts counting from 0, but apparently the writer of this filter decided to do this instead).

[v] and [a] are names for the output streams, to allow the rest of the ffmpeg line to use the output of the concat filter. I think they can have arbitrary names; which one is video and which one is audio is probably determined by their relative positions, but I haven't tested that out.

Note that the single quotes ' ' around the whole filter section are required.

-map '[v]' -map '[a]'

This tells ffmpeg to use the results of the concat filter rather than the streams directly from the input files.

Note that filters are incompatible with stream copying; you can't use -c copy with this method. I also think that it can't handle soft subtitles, though I haven't tested that: there's no hint about it in the documentation, but ffmpeg documentation is often incomplete or obfuscated, so that's not a sure sign either way.

This can concatenate files encoded in different formats (I've tested with a h264/aac MP4 and a vpx/vorbis WEBM, worked perfectly), though they need to be the same video frame size and audio depth (and possibly other things).

See also here.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.