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

2

Nb: I incorrectly asked this question at stackoverflow

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?

Just a note: I was using a 2013 version of ffmpeg. I just updated that to latest 2.6 but the issue remains.

bcsteeve

Posted 2015-03-11T20:34:02.337

Reputation: 197

2Next time please include the full uncut command line output of the conversion command. – slhck – 2015-03-12T17:03:21.337

Answers

1

From the documentation, the -ss flag does different things depending on where it is in the command

-ss position (input/output) When used as an input option (before -i), seeks in this input file to position. Note the in most formats it is not possible to seek exactly, so ffmpeg will seek to the closest seek point before position. When transcoding and -accurate_seek is enabled (the default), this extra segment between the seek point and position will be decoded and discarded. When doing stream copy or when -noaccurate_seek is used, it will be preserved.

When used as an output option (before an output filename), decodes but discards input until the timestamps reach position.

position may be either in seconds or in hh:mm:ss[.xxx] form.

So, from your own answer, the first command applies the logic on the output and the second command applies it on the input

Matthew Steeples

Posted 2015-03-11T20:34:02.337

Reputation: 2 130

1

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 -vcoded 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.

bcsteeve

Posted 2015-03-11T20:34:02.337

Reputation: 197

It really shouldn't make a difference, and if at all, I would have assumed that the first command worked better. If you can supply a sample file I will check and create a bug report if necessary. – slhck – 2015-03-12T17:04:36.577

Agree that it shouldn't make a difference, but it certainly does. I'll try to get you a sample file so you can recreate. – bcsteeve – 2015-03-12T21:37:54.743

Thanks for this. Would never have figured that out by myself – Matthew Steeples – 2015-07-09T17:05:19.543

Did some digging now that I know the answer to try and find the reason, and have posted my own answer with what I've found – Matthew Steeples – 2015-07-09T17:15:12.413