Synchronizing audio and video using MP4Box / ffmpeg to concatenate files

7,508

Solution 1

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.

Solution 2

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!

Share:
7,508

Related videos on Youtube

Jesse L
Author by

Jesse L

Software sandwich maker.

Updated on September 17, 2022

Comments

  • Jesse L
    Jesse L almost 2 years

    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.

  • Henrik N
    Henrik N almost 12 years
    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.
  • charles young
    charles young almost 12 years
    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!
  • banshee20
    banshee20 over 11 years
    »Try using the Makostra video container.« - Isn't it called Matroska, simplified writing for Matryoshka?