Search code examples
ffmpegmp4video-editing

ffmpeg to split mp4 file into segments... after first segment, audio unsynced


I've used the ffmpeg command line shown in this question to split MKV files perfectly for a long time. Now i have some MP4 files that i'd like to split and at first it seemed to work, but every subsequent segment after the first has the audio not synced! And by several seconds.

I've tried forcing keyframes (advice I found on some other sites) and that didn't help.

I tried a different program entirely (Avidemux) and it was able to split the file with proper output, but it was a LOT slower, taking upwards of 3 minutes vs less than 2 seconds with ffmpeg. With Avidemux I was able to determine the exact position of the i-frame where I wanted to split, so thinking perhaps that was the syncing problem I tried that exact position (ie. 00:12:17.111 instead of 00:12:16 or whatever) but that didn't help either.

Is there an option I'm missing with ffmpeg to get it to properly sync audio to the video when splitting?


Solution

  • I'm not sure I understand WHY, but the issue was order of parameters.

    In the linked example, the command is as follows:

    ffmpeg -i input.avi -vcodec copy -acodec copy -ss 00:30:00 -t 00:30:00 output2.avi
    

    Of course, I'm using mp4 instead of avi, but otherwise I was entering the command exactly as above and (with mp4) I was getting an out-of-sync audio result. I accidentally stumpled onto this "fix"... if I instead enter the command thusly:

    ffmpeg -ss 00:30:00 -i input.mp4 -vcodec copy -acodec copy -t 00:30:00 output2.mp4
    

    I don't get the sync issues. Why? No idea. But it works. I've tried it a few times to confirm... making only that order of parameters change corrects the issue.