I am having trouble trimming video files without causing the Video/Audio to go out of sync.
From what I understand, using the seek argument -ss
before or after the input file results in two different behaviors.
For Example:
ffmpeg -ss 00:00:00.600 -i in.mp4 -c copy out.mp4
This produces a trim with accurate A/V sync but a rough audio trim (trim happens at a video key frame and not the precise seek value)
ffmpeg -i in.mp4 -ss 00:00:00.600 -c copy out.mp4
This produces a more accurate audio trim but causes A/V to go out of sync. Frames after the trim position are dependent on frames before the trim position.These frames are assigned negative timestamps and copied to output file, which results in video being out of sync with audio during playback.
On the other hand,
ffmpeg -i "in.mp4" -ss 00:00:00.600 -strict -2 out.mp4
This produces a more precise trimming and A/V sync. The trim task takes a long time to run and results in quality loss.
My Question is: Is there a way to get an accurate trim without re-encoding the video? maybe by discarding the extra frames at the beginning that cause the A/V to get out of sync. In my case, I can live with a few black frames in the beginning, as long as the audio is trimmed at the precise position and A/V sync is preserved. Is it possible to accomplish this using FFMPEG? if not, can MediaCodec on Android handle such precision when trimming?
Thanks for your time.
MediaMuxer
will let you use whatever timestamps you want, but you'd have to write the code. AndMediaCodec
wouldn't come into play, unless you wanted to re-encode the first group-of-pictures (e.g. to snip the left edge off of it) -- then you could include the remaining frames unmodified, which Android can definitely do (at least for H.264 baseline, given certain constraints). – Endurant