FFMPEG video merging issue

2

1

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.

Elby

Posted 2012-12-26T10:21:46.897

Reputation: 155

Have you tried playing back the intermediate_all.mpg? – None – 2012-12-26T10:25:19.810

1Ya but it played, but not correct duration – Elby – 2012-12-26T10:49:02.890

That could be the cause. Have you tried to concatenate the videos with ffmpeg itself? – None – 2012-12-26T11:27:28.780

how we can concatenate the videos with ffmpeg? – Elby – 2012-12-26T12:12:15.583

Dunno. Why don't you google it! :) – None – 2012-12-26T12:13:24.903

Like this: ffmpeg -i concat:"intermediate1.mpg|intermediate2.mpg" -c copy intermediate_all.mpg Though I Doubt that’ll help with the audio sync issue. – paulgrav – 2012-12-26T14:56:41.250

ya, I already tried with this no luck.... – Elby – 2012-12-27T09:39:41.360

Answers

3

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.

evilsoup

Posted 2012-12-26T10:21:46.897

Reputation: 10 085