Eslake
Eslake

Reputation: 131

How to find a "safe" point for -SS using FFMPEG to avoid breaking A/V sync?

I need to find a way to cut video using -SS without breaking the audio/video sync.

Once in a while it works perfectly fine, but most of the time the audio falls slightly out of synch. Clearly there is something akin to a keyframe for audio, but I do not know how to find them.

The problem happens when using -SS in conjunction with Any specification for a codec, including -c copy.

For my purposes, I can't lose quality so those are necessary.

ffmpeg -i src.mp4 -ss 1:00 -t 30 -c copy result.mp4

tends to break the sync.

ffmpeg -i src.mpr -ss 1:00 -t 30 result.mp4

produces matching a/v but with significant quality loss from using the default compressions.

I need a way to find the times at which a cut will result in matching a/v.

Upvotes: 8

Views: 3231

Answers (1)

Brad
Brad

Reputation: 163313

The issue actually has to do with video keyframes. The audio/video are effectively starting at different times in your MP4 output.

For what it's worth, I've found that using Matroska for the output works fine, as it seemingly supports these differing timestamps better than MP4.

In any case, you might find this discussion helpful: https://github.com/mifi/lossless-cut/pull/13

Here is the relevant FFmpeg command that will probably work for you. Please give it a try!

ffmpeg -ss 1:00 -i src.mp4 -t 30 -avoid_negative_ts make_zero -c copy result.mp4

The key here is avoid_negative_ts, which I believe allows for outputting the previous GOP to the file, but setting their timestamps to all happen at zero (up until they shouldn't, of course) so that way they can be used for decoding, allowing you to arbitrarily cut the file without transcoding.

Upvotes: 6

Related Questions