Synchronizing audio and video using MP4Box / ffmpeg to concatenate files

4

I have two H.264 encoded MPEG-4 files that I need to concatenate. I have been using MP4Box for this task by first ensuring the files are encoded identically (even went so far as to compare output from h264_parse on their video tracks) and then concatenating with this command:

MP4Box -cat file1.mp4 -cat file2.mp4 output_file.mp4

This works and the output file is playable, but on playback in Quicktime or VLC the second video's audio starts too soon, making the entire second part of the concatenated file out of sync.

I have tried reencoding the output through ffmpeg with -vcodec copy and -acodec copy but the sync issue persists.

I have also tried converting to MPEG-2 format first, concatenating with a simple cat file1.mpg file2.mpg > output.mpg and reencoding the result with ffmpeg. This was even worse.

I know that I can pass commands to MP4Box to adjust the start time of the audio track, but I am trying to do this programmatically for any input video (in the same encoding of course). I am looking for possible solutions that would not require human intervention / manual adjustments. Or, at least, an understanding of what is happening to make the second part of the concatenated video go out of sync.

Jesse L

Posted 2009-09-04T18:01:11.330

Reputation: 143

Answers

7

I faced the same problem. For correct synchronization you have to write the MP4Box command as follows:

MP4Box -add file1.mp4 -cat file2.mp4 -new output_file.mp4

instead of using -cat use -add before the first video. The remaining options are correct and you can put -new before the output video parameter.

Ajith S Nair

Posted 2009-09-04T18:01:11.330

Reputation: 86

Thank you! I got a freeze frame where file1 ended and file2 began, but audio kept playing. After changing -cat … -cat to -add … -cat, I get both audio and video, though file2 is out of sync. – Henrik N – 2012-06-30T19:26:24.613

1

Try using the Matroskra video container (MKV).

Firstly, you need mkvtoolnix. Download and install it, then you need to fire up mkvmerge GUI (unless you like command-line tools, which is fine by me - although you might want to look at the file linking section of the mkvmerge documentation).

Add your first file by clicking "add". Then, click on "append", and open your second file. Set the output file, hit "Start muxing" at the bottom, and away you go!

If the container is unsuitable for your needs, you can reencode it, or try to convert it to some other format... Although MKV is a very lovely container!

Breakthrough

Posted 2009-09-04T18:01:11.330

Reputation: 32 927

I had been having problems with MP4Box appending mp4 files. The audio was getting messed up. I tried mkvmerge.exe from this package and it worked great! – cdavidyoung – 2012-08-01T22:00:19.923

»Try using the Makostra video container.« - Isn't it called Matroska, simplified writing for Matryoshka? – Lumi – 2013-03-27T12:20:35.983

0

From the mkvtoolnix package use mkvmerge:

mkvmerge -o "output file" "input file" +"appended file"

cdavidyoung

Posted 2009-09-04T18:01:11.330

Reputation: 101