How to trim a video file at a precise position without re-encoding using FFMPEG [closed]
Asked Answered
O

0

6

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.

Oocyte answered 23/12, 2019 at 19:5 Comment(4)
You see, most video codecs work by creating key frames at intervals throughout the video, with a dependency relationship that (generally) goes to the left. That is, if you "drop" a frame, all the frames to its right become undecodable, until you reach the next keyframe. You can't just replace them with black frames.Endurant
@ greeble31, thanks for pointing out the mistake in the post. I have edited the question to show the correct dependency relationship direction (right to left).Oocyte
@ greeble31, Some other post (I cant find the link to it) mentioned that some advanced players do use the frames with the negative timestamp for decoding but not for presentation; this way, the A/V stay in sync.What I meant by black frames in the beginning of the video is that; if there is a way to have ffmpeg not include the frames with the negative timestamp in the output, regular players might just play audio and video in sync with the first few frames being black frames since they would be missing the information they depend on during decoding.Oocyte
Don't know too much about ffmpeg or negative timestamp semantics, so I won't comment on that. Android's MediaMuxer will let you use whatever timestamps you want, but you'd have to write the code. And MediaCodec 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

© 2022 - 2024 — McMap. All rights reserved.