ffmpeg video-concatenation - audio-/videotrack Synchronisation

1.5k Views Asked by At

I'm want to concatenate different video-files of random format. First I convert them to flv, adapt the framerate of each video to lowest of the involved videos. Next step is to decode them and remove the header (but not the header of the first video in the sequence, then I concatenate them with cat and finally I encode the resultsequence.

But the video- and audio-track are going more and more out of sync from sequence-part to sequence-part.

Would be glad for any idea.

1

There are 1 best solutions below

0
On

Your problem then is staying in sync while going through concat process. Rule of thumb, the less steps the better. This should solve your problem. First, use mpg instead of flv for intermediate format. Second, add -copyts to each command (copies timestamps to retain sync). Also, there's no need to go to the lowest framerate. You can retain the quality of the highest video through this process. Try this:

$ ffmpeg -i a.mp4 -qscale:v 1 -copyts a.mpg
$ ffmpeg -i b.flv -qscale:v 1 -copyts b.mpg

Now you should have two (large) intermediate files of compatible video quality (variable: -qscale:v 1). You're ready to glue them together.

$ ffmpeg -i concat:"a.mpg|b.mpg" -c copy all.mpg

If you're on Windows, you'll be fine as is. On UNIX flavors, you'll need a backslash before the pipe. (On my version I get a flood of buffer underflow / packet too large, ignoring buffer limits to mux it errors. They seem to be harmless.) Now you can convert to whatever more useful format you want with more realistic quality.

$ ffmpeg -i all.mpg -qscale:v 2 -copyts all.mp4

I've had success keeping the concatenated videos in sync but the quality does not seem as good as it should be. Maybe tweak the intermediate file parameters.